HTML Diff
0 added 0 removed
Original 2026-01-01
Modified 2026-02-21
1 <p>What is the most difficult part of being a programmer? Naming variables.</p>
1 <p>What is the most difficult part of being a programmer? Naming variables.</p>
2 <p>This thought seems to be popular among programmers for a reason; people often struggle with naming. Indeed, how we name our entities (functions/variables/constants/classes/modules) is of great significance because generally, we read code, we don't write it.</p>
2 <p>This thought seems to be popular among programmers for a reason; people often struggle with naming. Indeed, how we name our entities (functions/variables/constants/classes/modules) is of great significance because generally, we read code, we don't write it.</p>
3 <p>In this article, I will analyze generally accepted rules among developers. The examples will be in Javascript, but my tips apply to every language.</p>
3 <p>In this article, I will analyze generally accepted rules among developers. The examples will be in Javascript, but my tips apply to every language.</p>
4 <h3>Naming conventions</h3>
4 <h3>Naming conventions</h3>
5 <p>Before we turn to semantics, let's take a look at the syntax. There are several popular naming conventions:</p>
5 <p>Before we turn to semantics, let's take a look at the syntax. There are several popular naming conventions:</p>
6 <ul><li>Camel case (a word with capital letters except the first one): myClass</li>
6 <ul><li>Camel case (a word with capital letters except the first one): myClass</li>
7 <li>Snake case (underscore is the separator): my_const</li>
7 <li>Snake case (underscore is the separator): my_const</li>
8 <li>Kebab case (hyphen is the separator): my-data</li>
8 <li>Kebab case (hyphen is the separator): my-data</li>
9 <li><a>Hungarian Notation</a>stands completely apart</li>
9 <li><a>Hungarian Notation</a>stands completely apart</li>
10 </ul><p>In fact, there are much more, although many have outdated and are no longer used or very rarely (at least, hardly many remember COBOL-CASE).</p>
10 </ul><p>In fact, there are much more, although many have outdated and are no longer used or very rarely (at least, hardly many remember COBOL-CASE).</p>
11 <p>It begs the question, what naming style to choose? The answer is simple. Each language has a generally recognized, and often an official, coding standard. It should become your guideline. Take some time to find a standard for your language and go through it, usually it can be found on GitHub and contains many illustrative examples.</p>
11 <p>It begs the question, what naming style to choose? The answer is simple. Each language has a generally recognized, and often an official, coding standard. It should become your guideline. Take some time to find a standard for your language and go through it, usually it can be found on GitHub and contains many illustrative examples.</p>
12 <h3>Size does matter</h3>
12 <h3>Size does matter</h3>
13 <p>Those who took lab programming tests must clearly remember that most variables were single-letter. Interesting fact, in the first programming languages identifiers were single-character as in mathematical notation. The first language, presumably, that named entities using words was Lisp. Much has happened since then (the 60s) and the usage of one-letter identifiers today is considered bad manners.</p>
13 <p>Those who took lab programming tests must clearly remember that most variables were single-letter. Interesting fact, in the first programming languages identifiers were single-character as in mathematical notation. The first language, presumably, that named entities using words was Lisp. Much has happened since then (the 60s) and the usage of one-letter identifiers today is considered bad manners.</p>
14 <p>And yet they can and should be used for some purposes,generally to name counters and indexes.</p>
14 <p>And yet they can and should be used for some purposes,generally to name counters and indexes.</p>
15 <h3>Actions and entities</h3>
15 <h3>Actions and entities</h3>
16 <p>Compare this:</p>
16 <p>Compare this:</p>
17 <p>When we implement a function, we describe some action, that natural languages express with verbs. So it is obvious that a function name should be also a verb. Even though this rule is simple and reasonable, beginners often refer to functions with nouns.</p>
17 <p>When we implement a function, we describe some action, that natural languages express with verbs. So it is obvious that a function name should be also a verb. Even though this rule is simple and reasonable, beginners often refer to functions with nouns.</p>
18 <p>Usually there is no such problem with variables, no one uses verbs to name them, but just in case: values should be expressed in nouns.</p>
18 <p>Usually there is no such problem with variables, no one uses verbs to name them, but just in case: values should be expressed in nouns.</p>
19 <h3>Predicates</h3>
19 <h3>Predicates</h3>
20 <p>Let me remind you that predicate is a function checking the truthiness of expressions, it returns only two possible values: true and false.</p>
20 <p>Let me remind you that predicate is a function checking the truthiness of expressions, it returns only two possible values: true and false.</p>
21 <p>In most languages, booleans are prefixed with is.</p>
21 <p>In most languages, booleans are prefixed with is.</p>
22 <p>But not all languages follow this rule. In most Lisp languages, as well as in ruby (which borrowed this from Lisp), a question mark ? is used at the end of the word:</p>
22 <p>But not all languages follow this rule. In most Lisp languages, as well as in ruby (which borrowed this from Lisp), a question mark ? is used at the end of the word:</p>
23 <p>Considering that in these languages a function call does not require parentheses at the end, this form feels especially natural and more readable.</p>
23 <p>Considering that in these languages a function call does not require parentheses at the end, this form feels especially natural and more readable.</p>
24 <h3>Entry</h3>
24 <h3>Entry</h3>
25 <p>Not all predicates can be expressed using is. For example, how to ask a question to check if there is an odd number in the list? In such situations, it’s agreed to use has:</p>
25 <p>Not all predicates can be expressed using is. For example, how to ask a question to check if there is an odd number in the list? In such situations, it’s agreed to use has:</p>
26 <h3>Quantity</h3>
26 <h3>Quantity</h3>
27 <p>If you need a variable that contains a quantity of something, use this combination: plural entity + count.</p>
27 <p>If you need a variable that contains a quantity of something, use this combination: plural entity + count.</p>
28 <p>The following is an example of how you shouldn’t name a variable denoting quantity:</p>
28 <p>The following is an example of how you shouldn’t name a variable denoting quantity:</p>
29 <p>Such naming is certainly misleading. An entity in the plural should always denote only a collection.</p>
29 <p>Such naming is certainly misleading. An entity in the plural should always denote only a collection.</p>
30 <h3>Examples</h3>
30 <h3>Examples</h3>
31 <blockquote><h3>Additional materials:</h3>
31 <blockquote><h3>Additional materials:</h3>
32 <ul><li><a>Code Complete: Naming Mistakes to Avoid in Programming I</a></li>
32 <ul><li><a>Code Complete: Naming Mistakes to Avoid in Programming I</a></li>
33 </ul></blockquote>
33 </ul></blockquote>