1.1 Names
1.Use descriptive names for global, short names for local
Clarity is often achieved through brievity
2 Be Constant
3 Use active names for functions
checkoctal Vs isoctal
1.2 Expressions and Statement
1.Break up complex expressions
*x += (0xp = (2*k < (n-m) ? c[k+1]: d[k--]));
Vs
if (2 * k < n-m)
*xp = c[k+1];
else
*xp = d[k--];
*x += *xp;
2.Be carefull with side effects
scanf("%d %d" &yr, &profit[yr]);
1.3 Consistency and idioms
1.4 Functions Macros
1 Avoid function macros
parameter appears more than once in the definition might be evaluated more than once
2 Parenthesize the macro body and arguments
1.5 Magic Number
1 Define numbers as constants, not macros
We can use enum type for C;
We don't need to apply this rule if the magic number is clearly itself and is used for only once, arraySize for example.
2 Use the language to calculate the size of an object
C/C++:
#define NELEMS(array) (sizeof(array))/sizeof(array[0])
double buf[1024];
for(int i = 0; i < NELEMS(buf); i++)
Java:
char buf[] = new char[1024];
for(int i = 0; i < buf; i++)
1.6 Comments
1 Comment functions and global datas
2 Don't comment bad code, just rewrite it
3 Don't contradict the code
Change the comment accordingly when the code is changed
Supplementary Reading
1 The Elements of Programming Style: Brian Kernighan......
2.Code Complete