About named

From the perspective of the machine's point of view, variables, functions, classes ...... name is used to distinguish other things.
From the human perspective, the difference between other things, just to name the most basic requirements, but also to help the reader understand the name of the code.

In order to give readers a better understanding of the code, named when the following two points to note:

  1. The avoidance of doubt - let the reader understand correctly
  2. Explain what, not how - to improve the reader's understanding of efficiency

1. Avoid ambiguity

const limitOfLoginTries = 5;

This variable is used to define: how many times the wrong password is entered, the user will be locked.

In the end it will be locked or 4 times wrong wrong five locks?
limitThere is ambiguity on the question of whether the inclusive values. Here with maxbetter.


const getPath = (start, end) => {
  // expensive calculation
  ......
}

The method of the path between the starting and ending points for calculation.

getCommon to operation from maptaking an element.
Subconscious will think it is a lightweight action.
However, an example method will perform complex calculations. The method is easy to be taken lightly caller, called repeatedly, eventually leading to performance problems.
Here with caculatebetter.

These two examples from the "write-readable code Art" this book.

2. Description what rather than how

const moneyMultiplyRatio = money * 0.027;

Money is multiplied by a percentage of the same name and the code says.
After reading still do not know why this variable would like.
If the interest is, then, with interestmuch better.


The intention expressed by the variable variable names.
A name to a piece of code, the intent of the code expression is function.

The refrigerator loaded elephant, reading comprehension 3 行名字is clearly better than 100 行实现细节more efficient.

refrigerator.open();
refrigerator.put(elephant);
refrigerator.close();

This is consistent and functional programming declarative ideas.

Note To do -what, hiding technical details -how.
Written by people thinking more clearly, people understand more easily read.

Guess you like

Origin www.cnblogs.com/apolis/p/11792185.html