分享
 
 
 

Elements of Programming Style -- The C++ Style Guide

王朝c/c++·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

Elements of Programming Style -- The C++ Style Guide

January 23, 1996

Version 1.0 Neill Kipp

Files

Header files have a ".h" suffix. Header file contains class, struct, and union declarations, enum declarations, #defines, typedefs.

Implementation files have a ".cc" (UNIX) or ".cpp" (Windows, DOS) suffix. Implementation files contain function and method implementations.

Make a header block in header files and source code files. The header should contain title, author, date, and project information as well as a simple description of how the file fits into the project.

Names of Things

Names of Common C++ Characters

{

open brace, open curly

}

close brace, close curly

(

open parenthesis, open paren

)

close parenthesis, close paren

[

open bracket

]

close bracket

.

period, dot

!

exclamation point, bang, not

|

bar, vertical-bar, or, or-bar (actually a "vertical virgule")

&

ampersand, and, reference, ref

*

asterisk, multiply, star, pointer

/

slash, divide

//

slash-slash, comment

#

pound

backslash, (sometimes "escape")

~

tilde

The primitive type name "char" is usually pronounced like the first syllable of "charcoal." Sometimes it is pronounced like "care" and sometimes "car."

Names and Indentation

Names of Naming Conventions

interspersed_underscores lowercaseMixedCapital CapitalMixedCapital ALL_UPPERCASE Applications of Naming Conventions

enumeration_item_name variableName TypeName, ClassName, MethodName() UnixFileName.cc dosfn.cpp POUND_DEFINES Self-documenting Code

Use long names for every name in your program. No Magic Numbers

Numeric constants besides 0 (and sometimes 1) are not allowed. Use constants or #defines. Whitespace

Space (what you get when you press spacebar) Newline (what you get when you press enter) Tab (substitutes for 8 spaces) Space and Indentation

After an open brace, indent every subsequent line four spaces until the matching close brace. If an if, while, or for has no brace following, indent the next line two spaces. Indent lines which end in a colon backward two spaces (public, case). A space precedes and follows reserved words (if, else, class, struct) unless preceded or followed by indentation or newlines or special punctuation. A space precedes and follows operators and comparators (except unary operator bang is not followed by a space). Pointer variants (ampersand, star) are preceded and followed by space in declarations. Pointer variants (ampersand, star) are preceded (but not followed) by a space in expressions. A space follows an open parenthesis. Newline

Newline precedes an open brace in the following: class, struct, union, enum, method, function (but not: if, else, do, for, while, switch --- these braces are preceded by a single space.) Newline follows a close brace for method, function, if, else, do, for, while, switch. Semi-colon, then newline, then blank line after close brace for class, struct, union. Newline follows an open brace. Comments

Comments always begin at current indentation level with "//" and a space. No other construct may appear on the same line as a comment. Comments always preceed the construct they address. Use complete sentences in a comment. When describing a statement, comments may be in the imperative. Above all, be guided by what pleases the eye. Be guided by what makes your code MORE READABLE.

Header File Example

// MODULE NAME: ClassName.h

// PROJECT: CS1344-1,2 Course Notes

// AUTHOR: Neill Kipp

// DATE: January 1, 1996

// DESCRIPTION: This file presents examples of naming and

// indentation style in a C++ class declaration. This title

// information is minimal.

// The following prevents files from being included

// twice. It is a naming exception designed to emulate a file name

// (period is not a name character; underscore is).

#ifndef ClassName_h

#define ClassName_h

// This directive includes the superclass declaration.

#include "super.h"

// This directive includes another class declaration.

#include "other.h"

// The comment for an enumeration declaration precedes the declaration.

enum OverflowState

{

// Each item's comment precedes it at the same indentation as the item.

no_overflow,

// Follow the last item with a comma;

// it helps avoid syntax errors when adding or rearranging items.

overflow_occurred,

};

// This class shows how naming conventions and comments are used in a

// simple class declaration. Whitespace precedes and follows reserved

// words (like "public").

class ClassName

{

// After a brace, indent four spaces.

// The description of the variable "memberData" goes here.

int memberData;

// If a line ends in single colon, reverse-indent two spaces.

public:

// The constructor gives initial values to member data.

ClassName();

// The destructor guarantees clean deallocation.

// The tilde (~) is part of the method name. It is not an operator.

~ClassName();

// This method increments the member variable by the value in "howMuch"

// and returns TRUE if overflow is detected (FALSE otherwise). Method

// comments tell what the method does, what the arguments are,

// and what the method returns.

OverflowState IncrementMemberVariable( int howMuch);

// Prints message about overflow.

void ShowOverflow( OverflowState overflow);

};

#endif

Source code file example

// MODULE NAME: ClassName.cc

// PROJECT: CS1344-1,2 Course Notes

// AUTHOR: Neill Kipp

// DATE: January 1, 1996

// DESCRIPTION: This file presents examples of naming and

// indentation style in a C++ class implementation. This title

// information is minimal.

// This directive includes header information for the "ClassName" class.

#include "ClassName.h"

ClassName::

ClassName()

{

// Initialize member data (statement comments are in the imperative,

// and preceed the statement). Suggestion: write the comments first, then

// write the code.

memberData = 0;

}

// The return type appears on the first line,

// followed by the class name colon-colon on the second,

// and finally the method name on the last. Then a newline, an open brace

// and then indent. Notice the space after the open parenthesis. It helps

// the eye catch the type name.

OverflowState

ClassName::

IncrementMemberVariable( int howMuch)

{

// Check the overflow condition.

if ( TOO_BIG - memberVariable > howMuch) {

// If overflow, return that overflow occurred.

return overflow_occurred;

} else {

// Otherwise, return overflow is ok.

return overflow_none;

}

}

// This code implements the ShowOverflow method.

void

ClassName::

ShowOverflow( OverflowState overflow)

{

// Switch is a reserved word. It is followed by a space.

switch ( overflow) {

// Lines ending in a colon reverse indent two spaces.

case no_overflow:

// Display message about no overflow.

cout << "No overflow occurred.\n";

break;

case overflow_occurred:

// Display message that overflow occurred.

cout << "Warning: overflow occurred.\n";

break;

}

}

Other examples

// Note the spacing and indentation in the for statement.

for ( whichItem = 0; whichItem < BIG_NUMBER; whichItem++) {

DoSomething( whichItem);

}

// Bang is not followed by a space.

while ( !SemaphoreOK()) {

DoWaitForSemaphore( LONG_TIME);

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
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- 王朝網路 版權所有