|
Apache DevelopersC Language Style Guide -- 最优代码风格(我认为)(2) 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.
|