Naming Conventions


Giving name to Variables and Functions is one of the most important tasks in coding , yet it doesn’t get the attention of many of us . It doesn’t affect out code’s efficiency , but it certainly makes our code more readable and easily understandable . Here is a comprehensive guide about naming conventions , specially in context of JavaScript but i don’t doubt that it is equally helpful in other languages too .
Common Naming Conventions
Camel Case
Pascal Case
Snake Case
Kebab Case
Let’s discuss each of them briefly .
camelCase : -
It is a variable naming convention used almost everywhere .
Description : - first word starts with lowercase letter and then every upcoming word starts with Uppercase letter without any space between them .
Use camelCase for naming variable .
Use prefix like is , has or can to indicate Boolean .
To declare a private variable use underscore ( “_” ) as prefix .
Function name should be prefixed with verb like getData , calculateTotal etc …
Examples :- firstName , phoneNumber , totalSum , isAvailable , isPrime , _accountBalance etc …
Note : - Always prefer meaningful names that clarify the role of variable , avoid starting a name with special characters and Numbers .
PascalCase :-
Description : - Similar to camelCase with a slight difference , every word starts with UpperCase letter including first word .
Also known as “ Upper Camel Case “
This is mostly used in namig Classes and Constructor functions , it dfferentiates them from variables and functions .
Examples : - FirstName , PhoneNumber , TotalSum etc …
snake_case :-
Description :- In this naming convention , all the words are written in lowercase and are separated by underscore ( ”_” ) instead of spaces .
For naming constants UPPER_SNAKE_CASE is widely followed convention . [ upper snake case indicates immutability . ]
This naming convention and kebab-case convention is widely used for naming files [ Avoid upperCase letter in filenames . ]
Examples : - first_name , phone_number , total_sum , DAYS_IN_A_WEEK etc …
kebab-case :-
Description :- this is almost similar to snake_case , the only thing which differentiate it from snake_case is that in kebab-case we use Hyphen ( ”-” ) instead of underscore ( ”_” ) .
Examples : - first-name , phone-number , total-sum etc …
Concise Table :-
camelCase | firstName |
PascalCase | FirstName |
snake_case | first_name |
kebab-case | first-name |
Following these naming conventions ensures readability , consistency , maintainability , aligning with widely accepted community .
Subscribe to my newsletter
Read articles from Samir Malik directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
