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);
}
总结:
- 在程序中避免出现Magic Number,使用宏定义(#define)代替,宏定义全部大写。
- 变量在定义时即赋予初值,使用驼峰式(numTens)或_(num_tens)都可以,保持同一种风格。
- 适当使用空格使程序清晰。
- 花空号{}的使用格式,{ 在同一行但有空格,} 单独一行。
- 像是if while for等与后面的 ( 有一个空格。
- 在程序开始前介绍信息(作用、作者等)
- 先检查可能失败项。
C Programming Style 总结的更多相关文章
- [转] 編程風格要素-The Elements of Programming Style 中文英文中英對照
转自: http://www.loliman3000.com/tech/2fe33ce32906f0302412881.php 下面的程序風格規則提煉自Brian Kernighan和P. J. Pl ...
- 【转载】The Elements of Programming Style之代码风格金科玉律
原始日期: 2017-02-06 16:20 <The Elements of Programming Style >是一本很古老的书.尽管 Fortran 我们不太使用,尽管新奇的语言层 ...
- [转] Matlab编程规范(MATLAB Programming Style Guidelines)
转自: Jerry Zitao Liu的博客 主要是参考了下面这篇文章,简洁总结在这里. MATLAB Programming Style Guidelines 简洁总结如下: 表示object的数量 ...
- Java Programming Language Enhancements
引用:Java Programming Language Enhancements Java Programming Language Enhancements Enhancements in Jav ...
- Google Java Style Guide
https://google.github.io/styleguide/javaguide.html Table of Contents 1 Introduction 1.1 Terminolog ...
- Julia is a high-level, high-performance dynamic programming language for technical computing, with syntax that is familiar to users of other technical
http://julialang.org/ julia | source | downloads | docs | blog | community | teaching | publications ...
- The C++ Programming Language - Bjarne Stroustrup
Preface Part 1: Introduction 1.1 The Structure of This Book 1.1.1 Introduction 1.1.2 Basic Facilitie ...
- 类型检查和鸭子类型 Duck typing in computer programming is an application of the duck test 鸭子测试 鸭子类型 指示编译器将类的类型检查安排在运行时而不是编译时 type checking can be specified to occur at run time rather than compile time.
Go所提供的面向对象功能十分简洁,但却兼具了类型检查和鸭子类型两者的有点,这是何等优秀的设计啊! Duck typing in computer programming is an applicati ...
- Flux 普及读本
话说当时做 APP 时,三月不知肉味,再次将眼光投放前端,有种天上一天,地下一年的感觉. Flux 是一种思想 了解的最好方式当然是看Flux官方文档了.React 中文站点也能找到对应的翻译版本,但 ...
随机推荐
- SpringBoot系列——MyBatis整合
前言 MyBatis官网:http://www.mybatis.org/mybatis-3/zh/index.html 本文记录springboot与mybatis的整合实例:1.以注解方式:2.手写 ...
- 【.NET Core项目实战-统一认证平台】第十二章 授权篇-深入理解JWT生成及验证流程
[.NET Core项目实战-统一认证平台]开篇及目录索引 上篇文章介绍了基于Ids4密码授权模式,从使用场景.原理分析.自定义帐户体系集成完整的介绍了密码授权模式的内容,并最后给出了三个思考问题,本 ...
- javascript小实例,阻止浏览器默认行为,真的能阻止吗?支持IE和标准浏览器的阻止默认行为的方法
看到这标题,是不是有点逆天的感觉,总感觉好狂拽炫酷,耳边隐隐约约传来一个声音:你这么叼,你咋不上天呢! ~~ 额,好吧! 话入正题,我为什么会提出这么一个问题呢? 阻止浏览器默认行为,真的能阻止吗?那 ...
- 【代码笔记】Web-CSS-CSS 分组和嵌套
一, 效果图. 二,代码. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...
- [HTML/CSS]colum-gap属性
属性定义及使用说明 column-gap的属性指定的列之间的差距. 注意: 如果指定了列之间的距离规则,它会取平均值. 语法 column-gap: length|normal; 值 描述 l ...
- Microsoft Dynamics CRM 9.0 OP 版本 安装 的那些 雷
天天讲安装过程好无聊了,还是搞点有营养的东西来,那么后面来说说刚出来的MSCRM OP 9.0 版本安装的那些雷: 雷1:操作系统要求Windows 2016 Server 这点还好,因为之前安装MS ...
- vue实现表计监测界面
已经好几个月没有更新博客了,因为最近太忙,忙得连写博客的时间都没有.上班赶项目开启996模式,下班要去练车考驾照,一边还在赶书稿,一边还接了私活.不由得感叹:年纪大了,再也经不起那么折腾..... 每 ...
- 一个MySQL视图的优化过程
1.需要优化的sql 最近做一个基于.net mvc和MySQL的仓储系统的优化工作,遇到了一个执行特别慢的SQL语句,经过一番折腾,终于搞定啦,分享一下过程.问题就是下面这个家伙: create o ...
- iOS----------常用三方库
1.笔者常用三方库 名称 作用 说明 <small>AFNetworking <small>基于HTTP/HTTPS 联网请求 <small> <small& ...
- Redis持久化的方式
Redis小知识: redis是键值对的数据库,有5中主要数据类型: 字符串类型(string),散列类型(hash),列表类型(list),集合类型(set),有序集合类型(zset) Redis持 ...