★★★ ★✰
An overview of the
Formatting is important, formatting is about communication, and communication is a top priority for professional developers. Our format should be like the layout of a newspaper.
1. Relevance
Variable declarations: As close as possible to where they are used, variables in a function are declared at the top of the function and global variables in a class are declared at the top of the class.
Related functions or concepts related: Try to put them together and follow a top-down reading pattern.
Second, the layout
- The value contains a maximum of 120 characters. (The monitor in team is very wide and can be slightly enlarged, again for easy communication and reading).
- Indent 2 or 4 Spaces to maintain unity across the team.
Third, the use of space
Proper use of white space can greatly increase the readability of the program.
bad
function(line){
let baseNum=0;
let lineSize=line.length();
return -baseNum+lineSize;
}
Copy the code
good
function(line) {
let baseNum = 0;
let lineSize = line.length();
return -baseNum + lineSize;
}
Copy the code
Iv. Unified team specification
The team unified a standard, one is to facilitate communication.
In another case, when two people modify the same file at the same time, only one line of code is modified, but because the format is different, the whole file is modified, which increases the complexity of code comparison.
5. Use tools
Modern ides almost all have formatting tools.
Our team uses vscode and Vue stack. So team members use one code formatting set – “vscode minimal code formatting configuration”
This article is based on Code Cleanliness by Robert C. Martin, translated by Han Lei.
Zhejiang Dahua Technology Co., Ltd.- Soft research – Smart City Product R&D Department Recruiting senior front end, welcome to contact, interested can send resume to [email protected], long-term effective
Last: four, notes
Objects and data structures