C++:结构命名标准
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4445789/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++: Struct Naming Standards
提问by
How should I name structs and their variables? This include structs inside objects...
我应该如何命名结构及其变量?这包括对象内的结构......
Thanks.
谢谢。
回答by vanneto
Find a style that you like and stick to it. Its as simple as that. I do it like this:
找到你喜欢的风格并坚持下去。就这么简单。我这样做:
struct Foo
{
int bar;
char baz;
Foo foo;
};
Struct's and classes have their first character capitalized, variables inside don't.
结构和类的第一个字符大写,里面的变量没有。
But again, this is my style, find a style that suits you and stick to it. Nothing more to be said.
但再说一遍,这就是我的风格,找到适合自己的风格并坚持下去。没什么好说的了。
回答by Stuart Golodetz
Harmonise your style with the people around you -- within reason, it doesn't matter which naming scheme you use for structs and variables, provided everyone on your team is doing a consistent thing.
与周围的人协调你的风格——在合理范围内,你为结构和变量使用哪种命名方案并不重要,只要你团队中的每个人都在做一致的事情。
Personally, I name structs like this:
就个人而言,我这样命名结构:
FunkyContainer
FunkyContainer
And variables like this:
和这样的变量:
ratherUsefulVariable
ratherUsefulVariable
But you have to adapt what you do to fit the house style, or you'll ruin the consistency of your team's code.
但是您必须调整您所做的以适应房屋风格,否则您将破坏团队代码的一致性。
As a side note, you don't have "structs inside objects" -- you can have nested structs, which are structs inside structs, but that's not the same thing. An object is an instanceof a struct, not the struct itself. (Objects can also be instances of classes, obviously, but that's irrelevant for the purpose of making the distinction here.)
作为旁注,您没有“对象内的结构”——您可以有嵌套结构,它们是结构内的结构,但这不是一回事。对象是结构的实例,而不是结构本身。(显然,对象也可以是类的实例,但这与在这里进行区分的目的无关。)
回答by ArBR
This is complete list for Naming conventionsthat I consider to be very complete:
这是我认为非常完整的命名约定的完整列表:
- Use US-English for naming identifiers.
- Use Pascal and Camel casing for naming identifiers.
- Do not use Hungarian notation or add any other type identification to identifiers.
- Do not prefix member fields.
- Do not use casing to differentiate identifiers.
- Use abbreviations with care.
- Do not use an underscore in identifiers.
- Name an identifier according to its meaning and not its type.
- Name namespaces according to a well-defined pattern.
- Do not add a suffix to a class or struct name.
- Use a noun or a noun phrase to name a class or struct.
- Prefix interfaces with the letter I.
- Use similar names for the default implementation of an interface.
- Suffix names of attributes with Attribute.
- Do not add an Enum suffix to an enumeration type.
- Use singular names for enumeration types.
- Use a plural name for enumerations representing bitfields.
- Do not use letters that can be mistaken for digits, and vice versa.
- Add EventHandler to delegates related to events.
- Add Callback to delegates related to callback methods.
- Do not add a Callback or similar suffix to callback methods.
- Use a verb (gerund) for naming an event.
- Do not add an Event suffix (or any other type-related suffix) to the name of an event.
- Use an –ing and –ed form to express pre-events and post-events.
- Prefix an event handler with On.
- Suffix exception classes with Exception.
- Do not add code-archive related prefixes to identifiers.
- Name DLL assemblies after their containing namespace.
- Do not add MR building block prefixes to source files.
- Use Pascal casing for naming source files.
- Name the source file to the main class
- Only use the
this
. construction.
- 使用美国英语来命名标识符。
- 使用 Pascal 和 Camel 大小写命名标识符。
- 不要使用匈牙利符号或向标识符添加任何其他类型标识。
- 不要为成员字段添加前缀。
- 不要使用大小写来区分标识符。
- 谨慎使用缩写。
- 不要在标识符中使用下划线。
- 根据其含义而不是其类型来命名标识符。
- 根据定义良好的模式命名命名空间。
- 不要为类或结构名称添加后缀。
- 使用名词或名词短语来命名类或结构。
- 带字母 I 的前缀接口。
- 对接口的默认实现使用相似的名称。
- 属性名称后缀为 Attribute。
- 不要向枚举类型添加 Enum 后缀。
- 对枚举类型使用单数名称。
- 对表示位域的枚举使用复数名称。
- 不要使用可能被误认为是数字的字母,反之亦然。
- 将 EventHandler 添加到与事件相关的委托。
- 向与回调方法相关的委托添加回调。
- 不要向回调方法添加回调或类似的后缀。
- 使用动词(动名词)来命名事件。
- 不要在事件名称中添加事件后缀(或任何其他类型相关的后缀)。
- 使用 -ing 和 -ed 形式来表示事件前和事件后。
- 使用 On 为事件处理程序添加前缀。
- 使用 Exception 后缀异常类。
- 不要向标识符添加代码归档相关的前缀。
- 在它们包含的命名空间之后命名 DLL 程序集。
- 不要向源文件添加 MR 构建块前缀。
- 使用 Pascal 大小写命名源文件。
- 将源文件命名为主类
- 仅使用
this
. 建造。
This is for C# but also apply for Java/C++. If you want to standarize your code I recommend you to see the HIGH·INTEGRITY C++ CODING STANDARD MANUAL.
这适用于 C#,但也适用于 Java/C++。如果您想标准化您的代码,我建议您查看HIGH·INTEGRITY C++ CODING STANDARD MANUAL。
For casing identifiersyou should do it as follow:
对于外壳标识符,您应该按如下方式操作:
- Class, Struct (Pascal, eg: AppDomain)
- Enum type (Pascal, eg: ErrorLevel)
- Enum values (Pascal, eg: FatalError)
- Event (Pascal, eg: ValueChange)
- Exception class Pascal, eg: WebException)
- Field (camel, eg: listItem)
- Const Field Pascal, eg: MaximumItems)
- Read-only Static Field Pascal, eg: RedValue)
- Interface (Pascal, eg: IDisposable)
- Method (Pascal, eg: ToString)
- Namespace (Pascal, eg: System.Drawing)
- Parameter (camel, eg: typeName)
- Property (Pascal, eg: BackColor)
- 类、结构(Pascal,例如:AppDomain)
- 枚举类型(Pascal,例如:ErrorLevel)
- 枚举值(Pascal,例如:FatalError)
- 事件(Pascal,例如:ValueChange)
- 异常类 Pascal,例如:WebException)
- 字段(骆驼,例如:listItem)
- 常量字段帕斯卡,例如:MaximumItems)
- 只读静态字段 Pascal,例如:RedValue)
- 接口(Pascal,例如:IDisposable)
- 方法(Pascal,例如:ToString)
- 命名空间(Pascal,例如:System.Drawing)
- 参数(骆驼,例如:typeName)
- 属性(Pascal,例如:BackColor)
回答by Jonathan Wood
Any naming standard I'm familiar with are for the platform, library, etc. For example, Windows SDK has a standard, the CRT has a standard, and MFC has a standard.
我熟悉的任何命名标准都是针对平台、库等的。比如,Windows SDK 有标准,CRT 有标准,MFC 有标准。
I've seen no standard for C++. Perhaps you should look at what naming conventions others are using on your particular platform.
我没有看到 C++ 的标准。也许您应该查看其他人在您的特定平台上使用的命名约定。