Develop a good habit of writing code

Good code style makes people look comfortable, easy to understand and have the desire to read.

Poor code style can be daunting

Lei Jun said that the code he once wrote was as beautiful as poetry

This article will introduce some points that are easy to be ignored when writing code

I Blank line

Blank lines serve to separate program paragraphs. Proper blank lines (but not too much) will make the layout of the program clearer. Blank lines do not waste memory. Although printing programs with blank lines will consume more paper, it is worth it. So don't be reluctant to use empty lines.

1. Empty lines should be added after each class declaration and after each function definition.

// Blank line 
void Function1(...)
 { 
     ...
 } 
// Blank line 
void Function2(...)
 {
     ... 
 } 
// Blank line 
void Function3(...)
 { 
     ... 
 } 
//Blank line

2. In a function body, there is no blank line between closely related statements on logic, and blank lines should be added in other places.

//Blank line
while (condition)
{
    statement1;
    // Blank line 
    if (condition)
    {
        statement2;
    }
    else
    {
        statement3;
    }
    // Blank line 
    statement4;
}

II Code line

1. A line of code does only one thing, such as defining only one variable or writing only one statement. Such code is easy to read and easy to write comments.

int width; // width 
int height; // height 
int depth; // depth 
x = a + b;
y = c + d;
z = e + f;

//Compare the above code with the following code
//Obviously, the above code will be much more pleasing to the eye

int width, height, depth; // Width height depth
x = a + b; y = c + d; z = e + f;

2.if, for, while, do and other statements occupy one line, and the execution statement shall not be followed immediately. Add {} no matter how many statements are executed. This prevents writing errors.

//the former
if (width < height) 
{ 
	dosomething();
}

for (initialization; condition; update) 
{
	dosomething();
} 
// Blank line 
	other();

//the latter

if (width < height) dosomething(); 

for (initialization; condition; update) 
	dosomething();
other(); 

III Spaces within lines of code

1. Leave a space after the keyword. At least one space should be left after keywords such as const, virtual, inline and case, otherwise keywords cannot be discriminated. Keywords such as if, for and while should be followed by a space followed by an open parenthesis' ('to highlight the keyword.

2. Do not leave a space after the function name, followed by the left parenthesis' ('to distinguish it from the keyword.

3. Leave a space after ',', such as Function(x, y, z). If ';' It is not the ending symbol of a line, followed by a space, such as for (initialization; condition; update).  

4. Assignment operators, comparison operators, arithmetic operators, logical operators and bit field operators, such as "=", "+ =" > = "," < = "," + "," * ","% "," & & "," | "," < "," ^ "and other binary operators, should be preceded and followed by spaces.

5. Unary operators such as "!" "~", "+ +", "--" and "&" (address operator) are not preceded by spaces.

6. Like "[]", "." "- >" operators are not preceded by spaces.

7. For the for statement and if statement with long expression, some spaces can be appropriately removed for compactness, such as for (I = 0; I < 10; I + +) and if ((a < = b) & & (C < = D)).

void Func1(int x, int y, int z); // Good style 
void Func1 (int x,int y,int z); // Bad style 
if (year >= 2000) // Good style 
if(year>=2000) // Bad style 
if ((a>=b) && (c<=d)) // Good style
if(a>=b&&c<=d) // Bad style 
for (i=0; i<10; i++) // Good style
for(i=0;i<10;i++) // Bad style
for (i = 0; I < 10; i ++) // Too many spaces 
x = a < b ? a : b; // Good style 
x=a<b?a:b; // Bad style 
int *x = &y; // Good style 
int * x = & y; // Bad style 
array[5] = 0; // Do not write array [5] = 0;
a.Function(); // Don't write a Function();
b->Function(); // Do not write B - > function();

IV alignment

1. The delimiters' {'and'} 'of the program should be exclusive on one row and in the same column, and aligned to the left of the statement referencing them.  

void Function(int x)
{
	... // program code 
}

//The first one looks much better than the second

void Function(int x) {
	... // program code 

}

V Long line split

The maximum length of the code line should be controlled within 70 to 80 characters. The code line should not be too long, otherwise the eyes can't see it and it's not easy to print. The long expression is split into new lines at the lower priority operator, and the operator is placed at the top of the new line (so as to highlight the operator). The split new line shall be properly indented to make the layout neat and the statement readable.

if ((very_longer_variable1 >= very_longer_variable12) 
	&& (very_longer_variable3 <= very_longer_variable14) 
	&& (very_longer_variable5 <= very_longer_variable16))
{
	dosomething();
}

virtual CMatrix CMultiplyMatrix(CMatrix leftMatrix, 
								CMatrix rightMatrix);

for (very_longer_initialization; 
	 very_longer_condition; 
	 very_longer_update)
{
	dosomething(); 
}

Vi Modifier position

Whether modifiers * and & should be close to data types or variable names is a controversial issue. If the modifier * is close to the data type, for example: int* x; Semantically speaking, this writing method is more intuitive, that is, X is a pointer of int type. The disadvantage of the above writing method is that it is easy to cause misunderstanding, such as int* x, y; Here y is easily misunderstood as a pointer variable. Although the definition of X and y can avoid misunderstanding, not everyone is willing to do so.  

Modifiers * and & should be placed next to the variable name

char *name;
int *x,y;//Here y will not be misunderstood as a pointer

Although many compilers can help us do this work, we still can't be lazy. We should form a habit and be good to ourselves and others!

Keywords: C Back-end

Added by spookztar on Sun, 16 Jan 2022 21:26:46 +0200