Main | All posts | Code

Code Complete: Naming in Programming

Time Reading ~3 minutes
Code Complete: Naming in Programming main picture

What is the most difficult part of being a programmer? Naming variables.

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.

In this article, I will analyze generally accepted rules among developers. The examples will be in Javascript, but my tips apply to every language.

Naming conventions

Before we turn to semantics, let's take a look at the syntax. There are several popular naming conventions:

  • Camel case (a word with capital letters except the first one): myClass
  • Snake case (underscore is the separator): my_const
  • Kebab case (hyphen is the separator): my-data
  • Hungarian Notation stands completely apart

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).

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.

Size does matter

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.

And yet they can and should be used for some purposes,generally to name counters and indexes.

Actions and entities

Compare this:

 bed(); // bad
 sleep(); // good

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.

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.

Predicates

Let me remind you that predicate is a function checking the truthiness of expressions, it returns only two possible values: true and false.

In most languages, booleans are prefixed with is.

 isEmpty();
 isValid();
 isBusy();

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:

 empty?
 valid?
 busy?

Considering that in these languages a function call does not require parentheses at the end, this form feels especially natural and more readable.

Entry

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:

 node.hasChildren();

Quantity

If you need a variable that contains a quantity of something, use this combination: plural entity + count.

 symbolsCount
 peopleCount

The following is an example of how you shouldn’t name a variable denoting quantity:

 errors;

Such naming is certainly misleading. An entity in the plural should always denote only a collection.

Examples

// Data normalization 
 normalizeDomainName('hexlet.io');

 // Extracting part of the data 
 getName(user);
 getDomainFromEmail('support@hexlet.io');

 // Getting an array with errors 
 const errors= validate(user);
 if (errors.length> 0) {
   // ...
 }

 // Calculations 
 calculateDiff(first, second)

 // Access verification 
 canSwim(user)
 canViewProfile(user)

Additional materials:

User avatar Kirill Mokevnin
Kirill Mokevnin 14 April 2022
1
Related posts
Suggested learning programs
profession
Development of front-end components for web applications
10 months
from scratch
Start at any time
profession
Layout with the latest CSS standards
5 months
from scratch
under development
Start at any time
profession
new
Developing web applications with Django
10 months
from scratch
under development
Start at any time