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

原文如下:

  1 C Programming Style for Engineering Computation
  2 Created by Aidan Nagorcka-Smith (aidann@student.unimelb.edu.au) 13/03/2011
  3 Definitions and imports
  4 Definitions are in UPPER_CASE
  5 Imports go before definitions
  6 Space between imports, definitions and the main function.
  7 Use definitions for any constants in your program, do not just write them in
  8 Good:
  9 #import <stdio.h>
 10 #import <stdlib.h>
 11 #define MAX_STRING_SIZE 1000
 12 #define DEBUG 0
 13 int main(int argc, char **argv) {
 14 ...
 15 Bad:
 16 /* Definitons and imports are mixed up */
 17 #import <stdlib.h>
 18 #define MAX_STING_SIZE 1000
 19 /* Definitions are given names like variables */
 20 #define debug 0
 21 #import <stdio.h>
 22 /* No spacing between imports, definitions and main function*/
 23 int main(int argc, char **argv) {
 24 ...
 25 Variables
 26 Give them useful lower_case names
 27 Initialise them to something that makes sense whereever practical.
 28 Good:
 29 int main(int argc, char **argv) {
 30 int i = 0;
 31 int num_fifties = 0;
 32 int num_twenties = 0;
 33 int num_tens = 0;
 34 ...
 35 Bad:
 36 int main(int argc, char **argv) {
 37 /* Variable not initialised - causes a bug becase we didn't remember to
 38 * set it before the loop */
 39 int i;
 40 /* Variable in all caps - we'll get confused between this and constants */
 41 int NUM_FIFTIES = 0;
 42 /* Overly abbreviated variable names make things hard. */
 43 int nt = 0
 44 while (i < 10) {
 45 ...
 46 i++;
 47 }
 48 ...
 49 Spacing:
 50 Space intelligently, vertically to group blocks of code that are doing a
 51 specific operation, or to separate variable declarations from other code.
 52 One tab of indentation within either a function or a loop.
 53 Spaces after commas.
 54 Space between ) and {
 55 No space between the ** and the argv in the definition of the main function.
 56 Lines at most 78 characters in width
 57 Closing brace goes on its own line
 58 Good:
 59 int main(int argc, char **argv) {
 60 int i = 0;
 61 for(i = 100; i >= 0; i--) {
 62 if (i > 0) {
 63 printf("%d bottles of beer, take one down and pass it around,"
 64 " %d bottles of beer.\n", i, i - 1);
 65 } else {
 66 printf("%d bottles of beer, take one down and pass it around."
 67 " We're empty.\n", i);
 68 }
 69 }
 70 return 0;
 71 }
 72 Bad:
 73 /* No space after commas
 74 * Space between the ** and argv in the main function definition
 75 * No space between the ) and { at the start of a function */
 76 int main(int argc,char ** argv){
 77 int i = 0;
 78 /* No space between variable declarations and the rest of the function.
 79 * No spaces around the boolean operators */
 80 for(i=100;i>=0;i--) {
 81 /* No indentation */
 82 if (i > 0) {
 83 /* Line too long */
 84 printf("%d bottles of beer, take one down and pass it around, %d bottles of beer.\n", i, i - 1);
 85 } else {
 86 /* Spacing for no good reason. */
 87 printf("%d bottles of beer, take one down and pass it around."
 88 " We're empty.\n", i);
 89 }
 90 }
 91 /* Closing brace not on its own line */
 92 return 0;}
 93 Braces:
 94 Opening braces go on the same line as the loop or function name
 95 Closing braces go on their own line
 96 Closing braces go at the same indentation level as the thing they are
 97 closing
 98 Good:
 99 int main(int argc, char **argv) {
100 ...
101 for(...) {
102 ...
103 }
104 return 0;
105 }
106 Bad:
107 int main(int argc, char **argv) {
108 ...
109 /* Opening brace on a different line to the for loop open */
110 for(...)
111 {
112 ...
113 /* Closing brace at a different indentation to the thing it's closing
114 */
115 }
116 /* Closing brace not on its own line. */
117 return 0;}
118 Commenting:
119 Each program should have a comment explaining what it does and who created it.
120 Any interesting code should have a comment to explain itself.
121 We should not comment obvious things - write code that documents itself
122 Good:
123 /* change.c
124 *
125 * Created by Aidan Nagorcka-Smith (aidann@student.unimelb.edu.au) 13/03/2011
126 *
127 * Print the number of each coin that would be needed to make up some change
128 * that is input by the user
129 */
130 int main(int argc, char **argv) {
131 int input_change = 0;
132 printf("Please input the value of the change (0-99 cents inclusive):\n");
133 scanf("%d", &input_change);
134 printf("\n");
135 // Valid change values are 0-99 inclusive.
136 if(input_change < 0 || input_change > 99) {
137 printf("Input not in the range 0-99.\n")
138 }
139 ...
140 Bad:
141 /* No explanation of what the program is doing */
142 int main(int argc, char **argv) {
143 /* Commenting obvious things */
144 /* Create a int variable called input_change to store the input from the
145 * user. */
146 int input_change;
147 ...
148 Code structure:
149 Fail fast - input checks should happen first, then do the computation.
150 Structure the code so that all error handling happens in an easy to read
151 location
152 Good:
153 if (input_is_bad) {
154 printf("Error: Input was not valid. Exiting.\n");
155 exit(EXIT_FAILURE);
156 }
157 /* Do computations here */
158 ...
159 Bad:
160 if (input_is_good) {
161 /* lots of computation here, pushing the else part off the screen. */
162 ...
163 } else {
164 fprintf(stderr, "Error: Input was not valid. Exiting.\n");
165 exit(EXIT_FAILURE);
166 }

总结:

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

转载于:https://www.cnblogs.com/Will-zyq/p/9981546.html

C Programming Style 总结相关推荐

  1. 翻译】geosoft C++ Programming Style Guidelines (已翻译完毕,大家看看自己总结出了哪些吧!)...

    REF : http://geosoft.no/development/cppstyle.html 个人意译-大家可以随时拍砖讨论心得!^) 24.29 .53. 57 求助 `~` 1 介绍 这份文 ...

  2. The elements of programming style,好程序的要素

    软件工程经验 本是Fortran语言时代的软件工程经验总结,感觉多数条目也适合使用任何语言开发项目.后面的中文翻译是我理解的. 条目 Write clearly - don't be too clev ...

  3. Pair Programming 1 (Casual Game) 曹竹 杨牧 贪吃蛇游戏扩展 Blog Report

    作者: 曹竹 杨牧 我们的分工: 我们的"贪吃蛇"游戏主要有两个部分拼接组合:后台逻辑和前台绘图,在定义了数据结构后就可以分工合作了. 具体开发过程: 我们在前两天对要开发的游戏和 ...

  4. python怎么读音发音英语-python style是什么意思

    1. Also, for those who feel strongly either way and only want to run Python with a specific type of ...

  5. Functional Programming in C++

    Probably everyone reading this has heard "functional programming" put forth as something t ...

  6. 计算机专业免费电子书下载列表List of freely available programming books

                                    计算机专业免费电子书下载列表                       List of freely available progra ...

  7. Programming Paradigms

    2.2 Programming Paradigms Objectoriented programming is a technique for programming – a paradigm for ...

  8. Functional Programming for the Rest of Us 函数式编程另类指南

    Aug 1st, 2012 这是一篇需要全文转载的文章 函数式编程另类指南 原文:Functional Programming For The Rest of Us 原文作者:Vyacheslav A ...

  9. python程序设计作业_CS602留学生作业代做、代写Programming课程作业、代做Python语言作业、Python编程设计作业调试...

    CS602留学生作业代做.代写Programming课程作业.代做Python语言作业.Python编程设计作业调试 日期:2019-12-06 10:50 CS602 - Data-Driven D ...

  10. forkjoin rxjs_如何通过吃披萨来理解RxJS运算符:zip,forkJoin和Combine

    forkjoin rxjs 什么是RxJS? (What is RxJS?) Reactive programming is an asynchronous programming paradigm ...

最新文章

  1. “光纤之父”高锟离世,感谢他的贡献
  2. 手工雕刻图纸_龙门整体铝板雕刻弧形楼梯护栏实力商家
  3. 《税的真相》—— 读后总结
  4. python不能创建字典的是_用Python创建带有重复键的字典
  5. redis的zset使用(java)——存取List< Object>
  6. 【Mac】mac下使用 找不到或无法加载主类
  7. XSS绕过与防御总结
  8. 三维空间中判断射线与平面是否相交
  9. 商城网站前台html模板,网上购物商城前台模板HTML源码
  10. w ndows7旗舰版怎么重装系统,windows7旗舰版iso怎么安装
  11. python tkinter怎么读_Tkinter是什么意思
  12. Python基础------几种循环结构详解
  13. hibernate学习(4)——实体配置详解
  14. Latex笔记-脚注
  15. halcon 导出C++程序与visualstdio2019配置环境,在控制台显示一张图片
  16. 一分钟速看:《永不止步的云上创新》——蒋江伟
  17. elementUI container布局不生效解决办法
  18. sublime 中英文等宽字体
  19. AutoHotkey2的helloworld
  20. 使用wireshark抓取Tcp三次握手

热门文章

  1. Set集合之HashSet添加的数据是如何保证不重复的
  2. FusionCharts破解方法
  3. JavaScript基础专题之执行上下文和执行栈(二)
  4. 三列自适应布局(圣杯布局)
  5. Ubuntu双网卡绑定
  6. error CS1010 CS8025 CS1012 CS1525 常见文档错误解决
  7. linux系统资源信息监控
  8. 为什么我特别讨厌语音输入
  9. js操作checkbox(复选框)的方法总结
  10. nginx 过滤请求URL参数及重定向