譯者序:這是一篇寫於1996年1月23日的文章,到現在已經有9個年頭了,很陳舊,有可能跟不上形勢,但是有些東西仍然值得現在的開發者學習,我翻譯這篇文字僅供讀者參考。
原文鏈接:http://www.gamedev.net/reference/articles/article708.asp
文件
頭文件有".h"後綴。頭文件包含類(class),結構(struct),和聯合(union)的聲明,枚舉(enum)的聲明,#define,typedef。
實現文件有一個".cc" (UNIX) 或者".cpp" (Windows, DOS)後綴。實現文件包括函數和方法的實現。
在頭文件和源代碼文件中安排一個頁眉。頁眉可以包含標題,作者,日期,和一些工程的信息,比如這個文件是配合整個工程的。
一些名字
通用C++字符的名字:
(注:這些都是符號的英文原名,目前並沒有完全標准化的漢語詞匯對應,所以後面的翻譯只是個人建議)
{ 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 按位取反
基本類型 "char" 通常發音是"charcoal."的首音節。有時念作 "care" 或者 "car."
名字和排版
命名約定的名字
interspersed_underscores 中間下劃線
lowercaseMixedCapital 小寫混合(首字母)大寫;
CapitalMixedCapital (首字母)大寫混合(首字母)大寫;
ALL_UPPERCASE 全部大寫
命名約定的應用
enumeration_item_name 枚舉,小寫加下劃線;
variableName 變量,小寫前綴加首字母大寫後綴;
TypeName, ClassName, MethodName() 類型名,類名,方法名,首字母大寫前後綴;
UnixFileName.cc Unix/Linux文件名:每個單詞首字母大寫;
dosfn.cpp windows/dos文件名:全部小寫;
POUND_DEFINES 宏定義,全部大寫;
自成檔代碼(也就是沒有文檔,僅靠注釋和代碼說明的源代碼文件)
程序中為每個名字使用完整拼寫.
避免直接使用數字(Magic number)
不允許出現除了0(有時也包括1)之外的數字常量. 使用常變量或者宏定義(#defines).
空白
空格(按空格鍵得到) ;
新行(按回車鍵得到) ;
制表符(tab) (用8個空格代替) ;
空白和排版
左花括號之後, 每行縮進4個空格直到對應的右花括號出現.;
如果if, while, 或 for 後面沒有跟花括號, 下一行縮進兩個空格;
冒號結尾的語句,反向縮進兩個空格(public, case);
保留字(if, else, class, struct) 前後要加1個空格除非已經因為新行或者特殊標點做了縮進;
運算符和比較符前後要有一個空格 (除了!之外);
指針變量 (&,*) 聲明的時候要前後加一個空格;
指針變量 (&,*) 在表達式中,前面(不是後面)要加一個空格llowed) ;
左圓括號後要加一個空格;
換行
在下面這些關鍵字後的左花括號後要換行: class, struct, union, enum, method, function (而不是: if, else, do, for, while, switch --- 這些的花括號後只要1個空格.)
方法(method),函數( function), if, else, do, for, while, switch的右花括號後要換行;
class, struct, union的右花括號後要換行並插入新空行。.(原文有寫Semi-colon,不理解含義);
左花括號後要換行.
注釋
注釋總是從當前縮進開始 "//" 然後緊接一個空格;
注釋中不允許其他注釋;
注釋要加在注釋的對象之後. (譯者注:原文 Comments always preceed the construct they address );
注釋中使用完整語句;
用於聲明的時候,注釋可以使用祈使句; 上面這些,是你的代碼看起來舒服的指南,也是你的代碼更具可讀性的指南.
頭文件示例:
// 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
源代碼文件示例:
// 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;
}
}
其他例子:
// 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);
}