Briefly about naming in JS

Hello, Habr! I decided to touch on the topic of naming entities in Javascript. At work, I interact a lot with interns and have seen enough of all. So I thought that it would be nice to collect in one small note the rules of naming entities adopted today in the JavaScript community. Maybe I didn’t collect everything, so I would be grateful if you supplement me in the comments.

Entity naming


Naming is very important in software development. As we know, the code is written primarily for people who will read it (for programmers). Bad naming can significantly increase labor costs for developing or supporting a project due to the extra time spent reading code, as with poor naming, the interpretation of "what is what in code" is difficult.

There are different syntactic forms of names, there are a lot of them, some are no longer in use. Here are the most common ones in js:

  • Camel Notation (CamelCase): MyClass
  • Snake_case: my_const
  • Barbecue notation (kebab-case): my-data

When choosing a case, it is important to consider the currently accepted standard. In js today snake_case and kebab-case are not accepted, but they can be found for example in Python or Ruby .

Single letter identifiers


Institutions often use single-letter identifiers in code. I see this coding style in half of the guys who come after high schools. This is a very vicious practice. The name should clearly describe the essence. Nowadays, using single-letter identifiers is a bad sign. Exceptions may be counters and indices, i.e. situations where one letter is more than enough to convey the essence of the essence.

Translit in the name


It is also very popular among students to use transliteration. Of course, this is also a sign of bad taste and bad code. There should be no transliteration in naming, because The common language in programming is English. When developing the code, we must use the international language that professionals in any country know. Russian transliteration does not apply to those.

Naming Variables and Classes


Variables are named in lower camelCase:

const maxCount = 10;

Classes are named in CamelCase:

class EnumerableCollection {
//some code
}

Actions


It is very important to use verbs for naming actions (for example, functions). You need to choose such a verb. which corresponds to the type of action.
For instance:

const checkNumberIsEven = (number) => (number % 2 === 0);

checkNumberIsEvenIs a good name. it is immediately clear that the function checks the number for parity.

Also a good name isEven - if this function lies in any /helpers/number.js, then even such a short name is more than enough, because the directory itself tells us that it contains functions for working with numbers. (But even here you can use the first option, because in a file that uses this function, there can be quite a lot of code, and the call can be somewhere in middle.)

Functions are far from always actions , it is also important to understand this.
For instance,

const arifmeticalProgression = (start, depth, maxLength = 10) => {
  const progression = [start];
  const iter = (acc) => {
    if (acc.length >= maxLength) {
      return acc;
    }
    const newIndex = acc.length;
    const newItem = start + newIndex * depth;
    const newProgresion = [].concat(acc, newItem);
    return iter(newProgresion);
  };
  return iter(progression);
};

This function generates an arithmetic progression, but it is not an action, because in view of its declarative nature, the definition of arithmetic progression is considered. It is important to be able to distinguish this moment. This also includes functions defining constants.

const defaultCollection = () => ([]);

Predicates


Above we discussed the function

const checkNumberIsEven = (number) => (number % 2 === 0);

This type of function is called predicate. A predicate is a statement about something. So called functions are running checks "the essence is something." A programming predicate always returns a boolean value.

As a rule, predicates are referred to through the third person singular form of the English auxiliary verb to be , i.e. is .

const isEven = (number) => (number % 2 === 0);

Some predicates determine the occurrence (presence) of the desired element (property or method or item'a) in the entity. Such predicates. usually begin with the English verb has (3rd person singular verb to have ). For example, a safe form Object.prototype.hasOwnPropertymight look like this:

const hasProp = (obj, key) => (Object.prototype.hasOwnProperty.call(obj, key));

If an entity is a quantity of something, then you should use the word count in the name.

All Articles