分享
 
 
 

Apache Developers'C Language Style Guide -- 最优代码风格(我认为)

王朝system·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

Apache Developers' C Language Style Guide

Compiled by Paul Sutton paul@awe.com. Based on a vote taken in November, 1996.

Further refinements voted upon in July 1997.

Introduction

[This bit could state that code should be laid out to be clear to someone else familiar with Apache. Functions should be short and easily understood. Comments should be provided to explain the rationale for code which is not obvious, and to document behavior of functions. The guidelines can be broken if necessary to acheive a clearer layout]

This style can be generated with the following arguments to GNU indent:

-i4 -npsl -di0 -br -nce -d0 -cli0 -npcs -nfc1

The Guidelines

Opening braces are given on the same lines as statements, or on the following line at the start of a function definition.

Code inside a block (whether surrounded by braces or not) is indented by four space characters. Tab characters are not used. Comments are indented to the same level as the surrounding code.

Closing braces are always on a separate line from surrounding code, and are indented to line up with the start of the text on the line containing the corresponding opening brace.

Functions are declared with ANSI-style arguments.

There is no space between the function name and the opening bracket of the arguments to the function. There is a single space following commas in argument lists and the semi-colons in for statements.

Inside a switch() statement, the case keywords are indented to the same level as the switch line.

Operators in expressions should be surrounded by a single space before and after, except for unary increment (++), decrement (--), and negation (!) operators.

There is no whitespace between a cast and the item modified (e.g., "(int)j" and not "(int) j").

If a cast is to a pointer type, there is a space between the type and the * character (e.g., "(char *)i" instead of "(char*)i")

Details and Examples

Indentation, General Style

Each level of indentation of code is four spaces. Tab characters should never be used. Specific indentation rules for function declarations and control-flow keywords are given below.

Example: main(int argc, char **argc)

{

if (argc != 0) {

fprintf(stderr, "No arguments allowed\n");

exit(1);

}

exit(0);

}

If an expression (or a routine declaration or invocation) would extend past column 80, the terms or arguments are wrapped at a convenient spot and the wrapped portion is indented under the first term in the expression (or the first argument to the function). Conditional expressions should be wrapped to keep single or parenthesized terms as atomic as possible, and place Boolean operators at either the start (preferable) or end of the line.

Example:

static const char *really_long_name(int i, int j,

const char *args, void *foo,

int k)

if (cond1 && (item2 || item3) && (!item4)

&& (item5 || item6) && item7) {

do_a_thing();

}

Comments

Provide comments which explain the function of code where it is not clear from the code itself. Provide rationale where necessary for particular bits of code.

Comments should be indented to same level as the surrounding text.

Example: code;

/* comment */

code;

Function Declaration and Layout

Functions are laid out as follows: int main(int argc, char **argv)

{

code;

}

The return type is placed on the same line as the function. Arguments (if any) are given in ANSI style. If no arguments, declare function as void. No space between function name and opening bracket, single space after comma separating each argument. The opening brace is placed on the line after the definition, indented to line up with the start of the return type text. The code is indented with four spaces, and the closing brace is indented to line up with the opening brace. Also see the section on indenting long declarations and invocations.

Function Calls

Space after commas in function calls. No space between function name and opening bracket. f(a, b);

Also see the section on indenting long declarations and invocations.

Flow-Control Layout

Flow-control statements (if, while, for, etc.) are laid out as in this example: if (expr) {

code;

}

else {

code;

}

There is a space between the keyword and the opening bracket. Opening brace placed on same line as the flow keyword. The code itself is indented by four spaces. The closing brace is indented to line up with the opening brace. If an else clause is used, the else keyword is placed on the line following the closing brace and is indented to line up with the corresponding if. Also see the section on indenting long expressions.

for Layout

Space after the semi-colons. Example: for (a; b; c)

switch Layout

case lines within a switch() are indented to same level as the switch statement itself. The code for each case is indented by four spaces. Braces are laid out as for other control-flow keywords. Example: switch (x) {

case a:

code;

case b:

code;

}

Expressions

Space before and after assignment and other and operators. No space between unary operators (increment, decrement, and negation) and the lvalue. Examples: a = b

a + b

a < b

a = -b

a = !b

++a

Capitalisation of Enums

No rule.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有