C Programming Style 总结

对材料C Programming Style for Engineering Computation的总结。

原文如下:

 C Programming Style for Engineering Computation
Created by Aidan Nagorcka-Smith (aidann@student.unimelb.edu.au) //
Definitions and imports
Definitions are in UPPER_CASE
Imports go before definitions
Space between imports, definitions and the main function.
Use definitions for any constants in your program, do not just write them in
Good:
#import <stdio.h>
#import <stdlib.h>
#define MAX_STRING_SIZE 1000
#define DEBUG 0
int main(int argc, char **argv) {
...
Bad:
/* Definitons and imports are mixed up */
#import <stdlib.h>
#define MAX_STING_SIZE 1000
/* Definitions are given names like variables */
#define debug 0
#import <stdio.h>
/* No spacing between imports, definitions and main function*/
int main(int argc, char **argv) {
...
Variables
Give them useful lower_case names
Initialise them to something that makes sense whereever practical.
Good:
int main(int argc, char **argv) {
int i = ;
int num_fifties = ;
int num_twenties = ;
int num_tens = ;
...
Bad:
int main(int argc, char **argv) {
/* Variable not initialised - causes a bug becase we didn't remember to
* set it before the loop */
int i;
/* Variable in all caps - we'll get confused between this and constants */
int NUM_FIFTIES = ;
/* Overly abbreviated variable names make things hard. */
int nt =
while (i < ) {
...
i++;
}
...
Spacing:
Space intelligently, vertically to group blocks of code that are doing a
specific operation, or to separate variable declarations from other code.
One tab of indentation within either a function or a loop.
Spaces after commas.
Space between ) and {
No space between the ** and the argv in the definition of the main function.
Lines at most characters in width
Closing brace goes on its own line
Good:
int main(int argc, char **argv) {
int i = ;
for(i = ; i >= ; i--) {
if (i > ) {
printf("%d bottles of beer, take one down and pass it around,"
" %d bottles of beer.\n", i, i - );
} else {
printf("%d bottles of beer, take one down and pass it around."
" We're empty.\n", i);
}
}
return ;
}
Bad:
/* No space after commas
* Space between the ** and argv in the main function definition
* No space between the ) and { at the start of a function */
int main(int argc,char ** argv){
int i = ;
/* No space between variable declarations and the rest of the function.
* No spaces around the boolean operators */
for(i=;i>=;i--) {
/* No indentation */
if (i > ) {
/* Line too long */
printf("%d bottles of beer, take one down and pass it around, %d bottles of beer.\n", i, i - );
} else {
/* Spacing for no good reason. */
printf("%d bottles of beer, take one down and pass it around."
" We're empty.\n", i);
}
}
/* Closing brace not on its own line */
return ;}
Braces:
Opening braces go on the same line as the loop or function name
Closing braces go on their own line
Closing braces go at the same indentation level as the thing they are
closing
Good:
int main(int argc, char **argv) {
...
for(...) {
...
}
return ;
}
Bad:
int main(int argc, char **argv) {
...
/* Opening brace on a different line to the for loop open */
for(...)
{
...
/* Closing brace at a different indentation to the thing it's closing
*/
}
/* Closing brace not on its own line. */
return ;}
Commenting:
Each program should have a comment explaining what it does and who created it.
Any interesting code should have a comment to explain itself.
We should not comment obvious things - write code that documents itself
Good:
/* change.c
*
* Created by Aidan Nagorcka-Smith (aidann@student.unimelb.edu.au) 13/03/2011
*
* Print the number of each coin that would be needed to make up some change
* that is input by the user
*/
int main(int argc, char **argv) {
int input_change = ;
printf("Please input the value of the change (0-99 cents inclusive):\n");
scanf("%d", &input_change);
printf("\n");
// Valid change values are 0-99 inclusive.
if(input_change < || input_change > ) {
printf("Input not in the range 0-99.\n")
}
...
Bad:
/* No explanation of what the program is doing */
int main(int argc, char **argv) {
/* Commenting obvious things */
/* Create a int variable called input_change to store the input from the
* user. */
int input_change;
...
Code structure:
Fail fast - input checks should happen first, then do the computation.
Structure the code so that all error handling happens in an easy to read
location
Good:
if (input_is_bad) {
printf("Error: Input was not valid. Exiting.\n");
exit(EXIT_FAILURE);
}
/* Do computations here */
...
Bad:
if (input_is_good) {
/* lots of computation here, pushing the else part off the screen. */
...
} else {
fprintf(stderr, "Error: Input was not valid. Exiting.\n");
exit(EXIT_FAILURE);
}

总结:

  1. 在程序中避免出现Magic Number,使用宏定义(#define)代替,宏定义全部大写。
  2. 变量在定义时即赋予初值,使用驼峰式(numTens)或_(num_tens)都可以,保持同一种风格。
  3. 适当使用空格使程序清晰。
  4. 花空号{}的使用格式,{ 在同一行但有空格,} 单独一行。
  5. 像是if while for等与后面的 ( 有一个空格。
  6. 在程序开始前介绍信息(作用、作者等)
  7. 先检查可能失败项。
上一篇:MatrixTree速成


下一篇:Android文件的流操作工具类