Good programming style

Source: Internet
Author: User

Good programming style

 
The Hungarian name is C.ProgramThe naming of identifiers defines a very standardized method, which is based on two rules:
A. The name of a variable starts with one or more lowercase letters. The prefix can reflect the data type, scope, and other information of the variable.
B. In an identifier, a prefix is followed by one or more uppercase words with the first letter. These words clearly indicate the role of the identifier.
  1  ) variable naming and prefix 
C char
S short
n int ndoornum
L long
the value of B Boolean is only true and false integer variables such as bvalid
F float floating point
d double
array a [ 5 ]
  2  ) in a variable name, uppercase letters start with a word, and lowercase letters 
but commonly used variables with obvious meanings, such as I, J, K, coordinate X, 1 ), 2 ) studentname, teachername
3 ) constants and macros are both large. Write, separated by '_'
# define max_width 5
#define PI 3.14
# define ABS (x)> = 0? (X):-(x)
The identifier should provide sufficient information, preferably pronounced.
Long name for a global variable with many descriptive information, short name for a local variable
The abbreviation of a word can be used when the name is too long. Note that the abbreviations must be consistent. All abbreviations are required.
For example, if the word number is abbreviated:
IntNdoornum; it is recommended that all variables containing the word number be abbreviated to num.
4Note that the plural form of a word is used. For example
IntNtotalstudents, nstudents; it is easy to understand that it represents the number of students, and the meaning of nstudent is not very obvious.

 5  ) For functions whose return values are true or false, add the "is" prefix, for example:
Int Iscanceled ();
Int Isalpha (); // C Standard library functions
Bool isbuttonpushed ();
6 ) Add the "get" prefix to the function that obtains a value.
Char * Getfilename ();
7 ) Add the "set" prefix to the function that sets a value.
Void Setmaxvolume ();
8 Generally, variables and structures use nouns, and function names use verb or verb-object phrases.
Correct indent
First, there must be indentation. OtherwiseCodeIs not obvious.
The indentation should be 4 spaces. Press the tab key when you want to indent it, or press the Space key. Do not use the tab key to indent it sometimes, or use the space key to indent it sometimes. Generally, you can set the number of spaces equivalent to a tab key in the development environment.
 
2.
A line must not be too long and cannot exceed the display area. To avoid reading inconvenience. If it is too long, it should be broken. It is best to place the line ahead of the operator. Do not place it behind the operator, as shown in figure
If(Condition1 ()&&Condition2 ()
&&Condition3 ()){
}

3) Note that the positions of '{', '}' are not random and must be unified.
If:
If(Condition1 ()){
Dosomething ();
}
Do not write elsewhere
If(Condition2 ())
{
Dosomething ();
}
 4  ) It is best to add a space between the variable and the operator.
Int Nage = 5 ;
Nage = 4 ;
If (Nage > = 4 )
Printf (" % D ", Nage );

For (I = 0 ; I < 100 ; I ++ );
 1  ) Try not to use immediate data instead.  # Define  (Const in C ++) is defined as a constant for later modification.  
# Define Max_students 20
Struct Sstudent astudents [max_students];
Struct Sstudent astudents [ 20 ];

# Define Total_elements 100
For (I = 0 ; I < Total_elements; I ++ ){
}
 2 ) The macro definition with parameters should be enclosed in parentheses throughout the macro, and the macro parameters should be enclosed in parentheses.
# Define Square (x) x * x // Square
Square (K + 1 ); To K + 1 * K + 1 ; Error
Even if # Define Square (x) * (X)
No insurance
1 / Square (x); 1 / (X) * (X); Error
Should:
# Define Square (x) * (x )) // Square
 3  ) Use parentheses in a slightly complex expression to avoid confusion in priority understanding.
N = K ++ J; // Not good
N = (K ++ ) + J; // Better
4 ) The expressions that are not easy to understand should be written in several lines:
N = (K ++ ) + J; it should be written:
N = K + J;
K ++ ;
 5  ) Is not recommended in expressions.  ?  : Format, which is replaced by the IF... else statement.
XP = 2 * K < (N - M) ? C [K + 1 ]: D [K -- ];
If ( 2 * K < (N - M ))
XP = C [K + 1 ];
Else
XP = D [K -- ];
 
6) Nested ifElseUse more statements {}
If(Condition1 ())
If(Condition2 ()
Dosomething ();
Else
Nocondition2 ();
Not good enough. It should be:
If(Condition1 ()){
If(Condition2 ()
Dosomething ();
Else
Nocondition2 ();
}
 7 ) Should be avoided  If     Else  In parallel.
If (Condition1 ()){
If (Condition2 ()){
If (Condition3 ()){
Condition123 ();
} Else {
Nocondition3 ();
}
} Else {
Nocondition2 ();
}
} Else {
Nocondiction1 ();
}

Replace:

If ( ! Condition1 ){
Nocondition1 ();
} Else If ( ! Condition2 ){
Nocondition2 ();
} Else If ( ! Condition3 ){
Nocondition3 ();
} Else {
Condition123 ();
}
 8  ) Follow some conventional writing methods, such:
Fixed Statement of loop:
For (I = 0 ; I < N; I ++ ) Array [I] = 0 ;
Instead of I = 0 ;
While (I <= N - 1 ) Array [I ++ ] = 0 ;
Statement of endless loops:
For (;;){... } Or While ( 1 ){... }
 9  ) The written code should be easy to read.
For example
If ( ! (N > M) && ! (S > T ))
Not as good
If (M <= N) && (T <= S ))

If ( ! (C = ' Y ' | C = ' Z ' ))
Worse
If (C ! = ' Y ' && C ! = ' Z ' );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.