首页 > 编程 > C++ > 正文

谷歌c++代码规范(1)---命名

2019-11-06 06:49:43
字体:
来源:转载
供稿:网友

通用命名规则

原文

Function names, variable names, and filenames should be descriptive; eschewabbreviation.Give as descriptive a name as possible, within reason. Do not worry about saving horizontal space as it is far more important to make your code immediately understandable by a new reader. Do not use abbreviations that are ambiguous or unfamiliar to readers outside your PRoject, and do not abbreviate by deleting letterswithin a Word.```一句话,就是不要担心浪费内存空间,命名少用缩写,语义明确才是最重要的。## 文件名 ##原文

Filenames should be all lowercase and can include underscores () or dashes (-).Follow the convention that your project uses. If there is no consistent local pattern to follow, prefer ““.

规则:字母全部小写,可以使用下划线‘_' 或者扩折号 '-'。另外,文件名最好多加一些定义。例如 http_server_logs.h 而不是logs.h.## 类型名 ##

Type names start with a capital letter and have a capital letter for each new word,with no underscores: MyExcitingClass, MyExcitingEnum. The names of all types — classes, structs, typedefs, and enums — have the same naming convention. Type names should start with a capital letter and have a capital letter for each new word. No underscores.

类型名包括类名,结构体,枚举,以及typedef。其命名方式符合大驼峰法,也就是名字由一个或者多个单词组成,每个单词首字母大写。## 变量名 ##

The names of variables and data members are all lowercase, with underscores between words. Data members of classes (but not structs) additionally have trailing underscores. For instance: a_local_variable, a_struct_data_member,a_class_data_member_

变量名字母全部小写,字母间使用下划线联接。类成员变量需要在名称后增加一个尾下划线,例如 a_local_variable,而类成员变量 a_class_data_member_.## 全局变量名##

There are no special requirements for global variables, which should be rare in any case, but if you use one, consider prefixing it with g_ or some other marker to easily distinguish it from local variables.

在谷歌规范中并没有对全局变量的具体限制,只是建议我们为全局变量起一个明显的区别相对于其他,比如在名字前加个统一的前缀例如g_。##常量名##

Use a k followed by mixed case, e.g., kDaysInAWeek, for constants defined globally or within a class.As a convenience to the reader, compile-time constants of global or class scope follow a different naming convention from other variables. Use a k followed by words with uppercase first letters “` 常量名字的规范是混合型即前缀k+驼峰法,例如 const int kDaysInAWeek=3.


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选