0 added
0 removed
Original
2026-01-01
Modified
2026-02-21
1
<p>Below are the most common mistakes programmers make when naming variables and functions in their code. These examples are taken from the projects of Hexlet students. I use JavaScript for demonstration purposes only because it is the most universal language, so the examples have nothing to do with the language being used. These mistakes are found everywhere in equal proportions.</p>
1
<p>Below are the most common mistakes programmers make when naming variables and functions in their code. These examples are taken from the projects of Hexlet students. I use JavaScript for demonstration purposes only because it is the most universal language, so the examples have nothing to do with the language being used. These mistakes are found everywhere in equal proportions.</p>
2
<p>Materials underlying naming conventions:</p>
2
<p>Materials underlying naming conventions:</p>
3
<ul><li><a>Code complete: Naming in programming</a></li>
3
<ul><li><a>Code complete: Naming in programming</a></li>
4
</ul><p>Long story short.</p>
4
</ul><p>Long story short.</p>
5
<p>It is perfect if the name alone is enough to understand what the variable stands for or what the function does, rather than how it does. If you have to study the code around that name to get an understanding, it's probably a bad name. The message here is: Don't bother me when reading your code!</p>
5
<p>It is perfect if the name alone is enough to understand what the variable stands for or what the function does, rather than how it does. If you have to study the code around that name to get an understanding, it's probably a bad name. The message here is: Don't bother me when reading your code!</p>
6
<h3>File?</h3>
6
<h3>File?</h3>
7
<p>My "favorite" variable name is file. It's even more "wonderful" when you can't see the moment it was assigned (for example, when you pass this file into a function):</p>
7
<p>My "favorite" variable name is file. It's even more "wonderful" when you can't see the moment it was assigned (for example, when you pass this file into a function):</p>
8
<p>What is a file? Asking this question to different people will provide different answers. Regular ones are as follows:</p>
8
<p>What is a file? Asking this question to different people will provide different answers. Regular ones are as follows:</p>
9
<ul><li>File contents</li>
9
<ul><li>File contents</li>
10
<li>Filename</li>
10
<li>Filename</li>
11
<li>Path of a file</li>
11
<li>Path of a file</li>
12
<li>File descriptor</li>
12
<li>File descriptor</li>
13
</ul><p>All of these variants actually exist. However, the latter is more common in C languages, where a file is indeed a file descriptor.</p>
13
</ul><p>All of these variants actually exist. However, the latter is more common in C languages, where a file is indeed a file descriptor.</p>
14
<p><em>File</em>is a bad name; it forces conversion from its name to its real meaning: "call it a file, mean a path" or "call it a file, mean a content". The proper names are<em>filepath, filename, content</em>.</p>
14
<p><em>File</em>is a bad name; it forces conversion from its name to its real meaning: "call it a file, mean a path" or "call it a file, mean a content". The proper names are<em>filepath, filename, content</em>.</p>
15
<h3>Hungarian Notation</h3>
15
<h3>Hungarian Notation</h3>
16
<p>The Hungarian Notation is an identifier (variable and function) naming convention, which comes down to encoding the data types directly in its name: e.g.<em>userArray</em>. This naming is about fighting the consequence rather than tackling its cause (bad names). Let's review some typical examples.</p>
16
<p>The Hungarian Notation is an identifier (variable and function) naming convention, which comes down to encoding the data types directly in its name: e.g.<em>userArray</em>. This naming is about fighting the consequence rather than tackling its cause (bad names). Let's review some typical examples.</p>
17
<ul><li>Arrays. Dealing with an array means we deal with a collection of some elements with names. The perfect name for such an array is the name of that element in plural:<em>users, cars, numbers</em>, or, at most,<em>items</em>. It clearly shows that we are working with a collection.</li>
17
<ul><li>Arrays. Dealing with an array means we deal with a collection of some elements with names. The perfect name for such an array is the name of that element in plural:<em>users, cars, numbers</em>, or, at most,<em>items</em>. It clearly shows that we are working with a collection.</li>
18
<li>Strings. Quite simple: if we are working with an arbitrary string, we can name it<em>text</em>or<em>sentence</em>. For more specific cases it can be<em>char</em>or<em>word</em>.</li>
18
<li>Strings. Quite simple: if we are working with an arbitrary string, we can name it<em>text</em>or<em>sentence</em>. For more specific cases it can be<em>char</em>or<em>word</em>.</li>
19
<li>Numbers. It's even easier:<em>number</em>is a<em>number</em>anyway. However, a name that fits the purpose of the number is certainly better: e.g.<em>denom</em>(denominator).</li>
19
<li>Numbers. It's even easier:<em>number</em>is a<em>number</em>anyway. However, a name that fits the purpose of the number is certainly better: e.g.<em>denom</em>(denominator).</li>
20
</ul><h3>Common names</h3>
20
</ul><h3>Common names</h3>
21
<p>Projects often have such names as<em>before</em>and<em>after</em>(or<em>new</em>and<em>old</em>). It immediately raises the question:<em>what before</em>?<em>What after</em>? No idea what we are talking about without knowing the code. Those situations I was referring to meant the value before the change and the value after the change. In fact, it's all about naming variables to reflect this essence:<em>valueBefore</em>and<em>valueAfter</em>.</p>
21
<p>Projects often have such names as<em>before</em>and<em>after</em>(or<em>new</em>and<em>old</em>). It immediately raises the question:<em>what before</em>?<em>What after</em>? No idea what we are talking about without knowing the code. Those situations I was referring to meant the value before the change and the value after the change. In fact, it's all about naming variables to reflect this essence:<em>valueBefore</em>and<em>valueAfter</em>.</p>
22
<p>The same goes for functions. What do you think happens in the code below?</p>
22
<p>The same goes for functions. What do you think happens in the code below?</p>
23
<p>There is no chance to guess that. There may be situations with such code being appropriate, but they are few and far between. A function must tell you what it will result in, not what it does.</p>
23
<p>There is no chance to guess that. There may be situations with such code being appropriate, but they are few and far between. A function must tell you what it will result in, not what it does.</p>
24
<h3>Function as a noun</h3>
24
<h3>Function as a noun</h3>
25
<p>Naming functions with nouns is an antipattern, even among experienced developers. Below are examples, where the left is how it is written and the right is how it should be according to the meaning:</p>
25
<p>Naming functions with nouns is an antipattern, even among experienced developers. Below are examples, where the left is how it is written and the right is how it should be according to the meaning:</p>
26
<ul><li>diff(value) => genDiff(value)</li>
26
<ul><li>diff(value) => genDiff(value)</li>
27
<li>parser(data) => parse(data)</li>
27
<li>parser(data) => parse(data)</li>
28
<li>value(user) => getValue(user)</li>
28
<li>value(user) => getValue(user)</li>
29
</ul><p>There are relatively few commonly used words to describe different actions in programming. These include modal verbs and words like<em>render, build, generate, show, enable, set, get, edit</em>, and others.</p>
29
</ul><p>There are relatively few commonly used words to describe different actions in programming. These include modal verbs and words like<em>render, build, generate, show, enable, set, get, edit</em>, and others.</p>
30
<p>This also includes mixed-up word order. Sometimes we see these versions:<em>diffBuild</em>, while the opposite is correct:<em>buildDiff</em>. First, what we do (what action), then on what (the subject the action is aimed at).</p>
30
<p>This also includes mixed-up word order. Sometimes we see these versions:<em>diffBuild</em>, while the opposite is correct:<em>buildDiff</em>. First, what we do (what action), then on what (the subject the action is aimed at).</p>
31
<h3>Abbreviations</h3>
31
<h3>Abbreviations</h3>
32
<p>It is normal to use abbreviations in programming. Words can be long and awkward, so some abbreviations are nearly commonplace. For example, naming<em>number</em>as<em>num</em>is quite usual. But sometimes you can come across rather interesting variants. I've seen these:</p>
32
<p>It is normal to use abbreviations in programming. Words can be long and awkward, so some abbreviations are nearly commonplace. For example, naming<em>number</em>as<em>num</em>is quite usual. But sometimes you can come across rather interesting variants. I've seen these:</p>
33
<ul><li><em>empt</em>(short for empty)</li>
33
<ul><li><em>empt</em>(short for empty)</li>
34
<li><em>hid</em>(short for hide)</li>
34
<li><em>hid</em>(short for hide)</li>
35
</ul><p>This abbreviation not only doesn't make it better, it makes it worse, since the full word is no longer obvious, and it can be difficult to read the name. I don't know why you would cut one letter short, but it's definitely a bad idea.</p>
35
</ul><p>This abbreviation not only doesn't make it better, it makes it worse, since the full word is no longer obvious, and it can be difficult to read the name. I don't know why you would cut one letter short, but it's definitely a bad idea.</p>
36
<p>Long names, on the other hand, are usually a good choice. Don't be afraid of them. For example, Rails has functions with the following names:<em>distance_of_time_in_words</em>or<em>option_groups_from_collection_for_select</em>.</p>
36
<p>Long names, on the other hand, are usually a good choice. Don't be afraid of them. For example, Rails has functions with the following names:<em>distance_of_time_in_words</em>or<em>option_groups_from_collection_for_select</em>.</p>
37
<h3>Paired vs unpaired</h3>
37
<h3>Paired vs unpaired</h3>
38
<p>There are often cases in programming that are perfectly suited to antonyms (words that have opposite meanings). For example, start-end, new-old, before-after. You should know how to choose these words properly. What is an antonym for<em>begin</em>, for example? The right answer is<em>end</em>, not<em>finish</em>(an antonym for start), as many people think.</p>
38
<p>There are often cases in programming that are perfectly suited to antonyms (words that have opposite meanings). For example, start-end, new-old, before-after. You should know how to choose these words properly. What is an antonym for<em>begin</em>, for example? The right answer is<em>end</em>, not<em>finish</em>(an antonym for start), as many people think.</p>
39
<p>I constantly see pieces of code where one word is made correct, while the other remains neutral. Typical example: we have an old value and a new value. The programmer creates a pair of<em>value</em>and<em>newValue</em>. However, this is not correct. The issue is that this pairing doesn't make the connection obvious. Moreover, we should be lucky enough to guess that<em>value</em>means<em>oldValue</em>. And as you remember, a name should immediately tell you its meaning and you should not have to guess.</p>
39
<p>I constantly see pieces of code where one word is made correct, while the other remains neutral. Typical example: we have an old value and a new value. The programmer creates a pair of<em>value</em>and<em>newValue</em>. However, this is not correct. The issue is that this pairing doesn't make the connection obvious. Moreover, we should be lucky enough to guess that<em>value</em>means<em>oldValue</em>. And as you remember, a name should immediately tell you its meaning and you should not have to guess.</p>
40
<blockquote><h3>Additional materials</h3>
40
<blockquote><h3>Additional materials</h3>
41
<ul><li><a>Code complete: Naming in programming</a></li>
41
<ul><li><a>Code complete: Naming in programming</a></li>
42
</ul></blockquote>
42
</ul></blockquote>