A good code layout helps you read and maintain the code. It can be imagined that there are no good typographical articles. It is estimated that the readers will not be able to read them any more without patience. The Code is even more so. Without a good programming style, the written code is bitter and hard to read and understand. So learning a good programming style is beneficial!
Make good use of spaces and line breaks (the two can make the meaning of our code clearer)
Empty rows do not waste memory, so do not give up, but do not abuse it! Generally, we add blank lines (or line breaks) in the following cases ):
After each class declaration and after each function definition
In a function body, close-logical related statements are not added. Otherwise, empty rows can be added for separation.
One line of code only does one thing. That is, one row defines only one variable or only one statement. This facilitates reading and commenting
If \ for \ while \ do and other statements occupy only one row, the execution statement does not keep up with it, no matter how many statements are executed
Try to initialize the variable while defining the variable. In this way, you can avoid referencing errors caused by uninitialized variables.
Use spaces properly to keep variable names or keywords away from other characters. In this way, the "Aesthetic Fatigue" caused by over-compact code is avoided ".
Likewise, we have the following suggestions on how to properly use spaces:
Leave at least one space after all keywords. For example, if \ while \ for and other keywords, a space should be left with braces '('
Function names cannot contain spaces.
'('Backward followed,') ','; 'backward followed
',' Followed by a space. If ';' is not the end symbol of a line, then leave a space.
Spaces should be added before and after binary Operators
Without spaces before and after the unary operator
'[]', '.', '->' Without spaces
[Cpp]
Int width; // width
Int height; // height
X = a + B;
Y = c + d;
If (width <= height)
{
Function ();
}
// Empty rows
For (int I = 0; I <n; I ++)
{
Function ();
}
Void function (int I, int j );
If (a> = B) & (c <= d ))
{
//...
}
X = a <B? A: B;
A [5] = 0;
A. function ();
A-> function ();
Long row Splitting:
The maximum length of a code line is 70 to 80 characters. Even so, we can imagine that if the entire line is full, it will undoubtedly cause inconvenience for us to read the code and maintain it. Therefore, you need to split long rows.
[Cpp]
If (very_longer_variable1> = very_longer_variable12)
& (Very_longer_variable3 <= very_longer_variable14)
& (Very_longer_variable5 <= very_longer_variable16 ))
{
Function ();
}
For (very_longer_initialization;
Very_longer_condition;
Very_longer_update)
{
Function ();
}
Annotations (usually used ):
Version and copyright notice
Function interface declaration
Important code line or paragraph prompts
Comments and rules:
Annotations should be accurate, easy to understand, and prevent ambiguity
Write and annotate Code while modifying code to ensure consistency
When the code is relatively long, especially when multiple nesting, comments should be added at the end of some paragraphs to facilitate reading.
The location of the comment should be adjacent to the described Code. It is best to place it above or right of the Code.
Annotations cannot be abused. Because simplified comments can help us understand the meaning of the Code, such as excessive abuse and some "redundant" comments. This is the only option. For example: I ++; // variable I for auto-Increment
[Cpp] view plaincopyprint?
If ()
{
// Code line
While ()
{
// Code line
} // End of while
// Code line
} // End of if
Class layout (there are two main types ):
"Data-centric ". Private Data is written in front, and public functions are written in the back
"Behavior-centric ". Write Public-type functions in front and private-type data in the back
We recommend that you use "behavior-centric ". This not only makes your own ideas clear during design, but also makes it easier for others to read. Because the user is concerned with interfaces.