Preface
There are only two hard things in Computer Science: cache invalidation and naming things.
-- Phil Karlton
I, Giorno Chopana, have a dream to become a programmer superstar. But if you have read the code I wrote, you will know that I am far from reaching the word "superstar". There are many inconsistent naming in my code:
- When the constant is named, it is in full uppercase (such as WAIT_CONFIRM), and when it is in full lowercase;
- Most of the naming of a project uses camel case (such as TaskController), but because the column names in the database use snake case (such as context_id), the code related to the database columns in the project uses camel case and snake case (such as restricted_hours[new Date(timestamp * 1000).getHours()] = 1;);
- Also a function that constructs complex objects, their prefixes may be any of build, create, make, or even compute;
- It is obviously an array, but the singular order is used as the variable name.
It's so confusing because I didn't follow a consistent set of naming rules. Whenever I pop up some new ideas in a project, I’m eager to try it out--no, I really use it. I have never sorted out my naming rules (good I've written JavaScript for three years) so I can't tell if the "new idea" is really new - maybe it's a rule that I've abandoned.
In order not to get deeply trapped in the quagmire of inconsistent naming, I set the naming rules for this article, hoping that they will give me some advice in the future.
Variable name
General Rules
- Variable names use the naming style of camel case. For example, use namingConvention instead of naming_convention;
- Adapt to the rules as much as possible.
Singular and plural rules for variable names
- If a variable stores a value of an array of types (i.e., the result is true when the variable is used as a parameter to a method), then the name of the variable is in plural form. For example, use fruits = [] instead of fruit = [].
- If the type of value stored by a variable is a set (i.e., Set), the name of the variable should be prefixed with the word unique. For example, use uniqueUserIds instead of userIdSet.
Naming rules for boolean variables
If the value of a variable is of boolean, the name of the variable should be prefixed with the following words:
- is. When a variable expresses a binary state, for example isFull, isEmpty. The following is should be an adjective;
- has. When a variable expresses whether an event has occurred in history, such as hasPaid and hasArrived. The one that follows has should be a verb and a past participle is used;
- can. When a variable expresses a certain permission, such as canWrite and canExecute. The one behind can should be a verb, and the present participle should be used.
Naming rules for numeric variables
- If the variable is stored in the maximum value in a series of numbers, the name of the variable should be prefixed with max, such as maxScore. If it is the minimum value, it is prefixed with min, such as minScore;
- If the variable is stored in the sum of a series of numbers, the name of the variable should be prefixed with total, such as totalIncome;
- If the variable is storing the length of the array, the name of the variable can be prefixed with numberOf, such as numberOfUsers.
Naming rules for string variables
- If it expresses the name of the person, brand name, company name, and table name in the database, then the name of the variable can be ended with the word name, such as customerName, brandName, and companyName;
- If it means a short description on the left side of the input box on a key or button, a paper form or an electronic form, the name of the variable can end with the word label, such as buttonLabel;
- Word content is not allowed
Function name
- The name of the function should be composed of verbs and nouns, such as readFile and writeFile;
- If the function's function is to convert the parameters into another form of output (such as binary conversion, currency conversion), then the name of the function should be prefixed with the word to, such as toDollar, toHexadecimal;
- If the function's function is to check the parameter and return a Boolean value, the name of the function should be prefixed with the word check, such as checkIsDirectory, checkIsExecutable;
- If the function's function is "count", the name of the function should be prefixed with the word count, and the object being counted in it should be in plural form, such as countPaidOrders.
Naming rules for constructor functions
- If the expression is to create an object from scratch, the function name can be prefixed with create, such as createObject;
- If the expression is to put some inputs together intact (maybe something else is added between the inputs) to create an object, then the function name can be prefixed with make, such as makeFloor. Further, if the function does not change the relative order of the input, the function name can be concat, such as concatString;
- If a function will create an object with more than one hierarchy based on the input, the function name can be prefixed with build, such as buildBinarySearchTree.
Naming rules for modified functions
- If the function is responsible for updating records in the database, the function name should be prefixed with the word update;
- If the function modifies some enumerable state, the function name should be prefixed with the word change.
Class Name
- The name of the class should adopt the naming style of capital case, such as DatabaseConnection;
- The name of the class should end with a noun;
- If the class exists to use a certain design pattern, the class name should be reflected in the location in the design pattern. For example, in the State pattern, the names of the class representing a specific state can be InitialState or UnpaidState.
References
The art of naming variables
Google JavaScript Style Guide
Letter case
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.