
A Guide to Variable Naming Conventions
Throughout the history of programming, there have been many languages and conventions we have used as software developers and naming things has always been a confusing thing to do ๐คทโโ๏ธ
Want this article in a quick and concise form? Check out the ๐งต below
https://twitter.com/vsnthdev/status/1396718684488409089
Lets see what are different types of variable naming conventions along with where & when we use them.
1. Flat case
It is the most basic way of naming variables in programming. It is also the easiest to read, but only when the name consists only of a single word.
If the variable name consists of more than a word, using flat case is not recommended as it won't remain readable anymore.
Flat case is used in almost all programming languages due to it's simplicity but as it is not enough to cover all the needs, so lets explore more conventions ๐
2. Dash case
An all lowercase letters separated by dashes or hyphens.
In certain programming languages, a dash (-) isn't a valid character for naming variables. Because it could have a special meaning in those languages, for example in JavaScript and Python.
CSS classes are one of the use cases where Dash case (also called as Kebab case) variable naming convention is commonly used.
3. Camel case
Lowercase first word followed by title case words without spaces.
It is the most popular variable naming convention used in many popular high-level programming languages like C#, JavaScript, Java, Kotlin, Go etc.
Camel case text is readable and easy to write with requiring no extra characters to be added in the middle.
4. Snake case
Lowercase words joined by an underscore.
Also known as under_score
case, it is the 2nd most popular naming convention. Predominantly used in popular langauges like Python, PHP, C++, Rust, Erlang.
Many developers believe that Snake case offers better readability compared to Camel case.
5. Pascal case
Title case words joined without spaces.
It is the 3rd most popular convention used in coding after Snake case.
Various languages have different meaning for Pascal case variables. In most object-oriented programming languages, Pascal case is commonly used to indicate an un-initialized class.
Permutations & Hybrids
The above ones are the most commonly used. Below are a few more variable naming conventions that are used in special circumstances.
6. Constant Case
All capitals with underscores joining the words.
Found within most programming languages when declaring constants, especially used in C/C++ for this purpose.
7. Cobol Case
All capital words separated by dashes.
As discussed above, in most modern languages a dash is not considered a valid identifier (cannot be within a variable name).
An exception to this rule is COBOL which allows the usage of dashes in variable names while disallowing underscores. As a result Cobol case has become the nomenclature for naming variables in COBOL language.
8. Underscore Notation
Starts with an underscore and follows any of the above mentioned naming conventions (Camel case in the above picture).
Variables or files names starting with an underscore denote that they're used internally and shouldn't be accessed out of that scope.
9. Train Case
Title case words separated by dashes.
This type of naming convention is used mostly in naming files in many types of projects.
10. Upper Flat Case
All capital words with no spaces, this naming convention also shares the same problem as Flat case.
It becomes hard to read and is rarely used except for certain primitive programming languages in embedded programming.
11. Camel Snake Case
Camel Case but with underscores.
Used quite rarely and in very few programming languages for special purposes.
Conclusion
Using the right convention is important as it contains a certain meaning and ensures that another developer understands the code and it's functionality correctly.
Thank you ๐