Apache Developers'C Language Style Guide -- 最優程式碼風格(我認為) (轉)
Developers' C Language Style Gu
Compiled by Paul Sutton to:paul@awe.com">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 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 keys 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 allowedn"); exit(1); } exit(0); }
(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 tein 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
- Ctalisation of Enums
No rule.
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10748419/viewspace-1007540/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- C 語言程式碼風格之 Linux 核心程式碼風格Linux
- 程式碼的風格 (轉)
- Object Pascal Style Guide (轉)ObjectGUIIDE
- Google C++ style guide——格式GoC++GUIIDE
- 我為什麼推薦Prettier來統一程式碼風格
- Google C++ Style Guide的哲學GoC++GUIIDE
- 前端進階之養成一個良好的程式碼風格--JavaScript standard style前端JavaScript
- 影像風格遷移(Neural Style)簡史
- JavaScript 程式碼風格指南JavaScript
- [npm資源] naming-style,快速轉換各種命名風格NPM
- Google C++程式設計風格指南(六):程式碼註釋GoC++程式設計
- 5個最優秀的Java和C#程式碼轉換工具JavaC#
- Vue 前端程式碼風格指南Vue前端
- Google JavaScript 程式碼風格指南GoJavaScript
- FPGA可綜合風格程式碼FPGA
- Verilog程式碼風格
- 專案程式碼風格要求
- C風格字串字串
- 批量匯出 CSDN 部落格並轉為 hexo 部落格風格Hexo
- Google C++程式設計風格指南GoC++程式設計
- .editorconfig 程式碼風格配置
- Pycharm 程式碼註釋風格模板PyCharm
- 好的程式碼風格積累
- Typora 程式碼塊Mac風格化Mac
- Notes about Vue Style GuideVueGUIIDE
- 程式碼榮辱觀-以運用風格為榮,以隨意編碼為恥
- [C++][程式設計風格]C++命名規則C++程式設計
- 編碼規範(四)之程式碼風格
- Google C++ 程式設計風格指南:類GoC++程式設計
- Google C++ 程式設計風格指南:格式GoC++程式設計
- 從另一個考慮來看程式碼的風格 (轉)
- Swift之旅_Language Guide1SwiftGUIIDE
- [C++]C風格、C++風格和C++11特性的執行緒池C++執行緒
- C++雜思錄——風格的選擇 (轉)C++
- Laravel 保持統一程式碼風格Laravel
- 前端程式碼規範 — JavaScript 風格指南前端JavaScript
- 不要再爭論程式碼風格了!
- 金庸筆下的良好程式碼風格