C++命名:read_input() 与 readInput()
C++ 中哪种命名约定更可取? 下划线方法还是camelCase方法? 我已经用 Java 编写了一段时间,并且习惯了驼峰命名约定。 哪一种更流行?
另外,在定义类时,私有/公共/受保护变量/方法是否有任何首选顺序?
朋友通常放在最后吗?
typedef 怎么样,它们位于类定义的顶部吗?
Which naming convention is more preferable in C++? The underscore method or the camelCase method?
I have coded in Java for a while and I am used to the camelCase naming conventions.
Which one is more prevalent?
Also, while defining a class, is there any preferred ordering of private/public/protected variables/methods?
Are friends usually put in the end?
What about typedefs, do they come at the top of the class definition?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
我更喜欢走boost路线,并且匹配标准库。这意味着
lower_case_names
。我喜欢我的代码读起来与 STL 一致。I prefer to take the boost route, and match the standard library. That means
lower_case_names
. I like that my code reads consistent with respect to the STL.这都是非常主观的,但通常对于 C++,我会使用:
用于函数和变量的驼峰命名法。
用于类的
PascalCase
。在课堂上。
编辑:忘记了这些2:
是的,
friend
在最后,typedef
要么在开头,如果它们在类中使用,要么在它们使用类之后(对于明显的原因)。This is all very subjective, but generally for C++ I do:
camelCase
for functions and variables.PascalCase
for classes.In classes.
Edit: Forgot these 2:
Yes,
friend
at the end,typedef
either at the beginning if they are used in the class, or after if they use the class (for obvious reasons).这里最重要的是你保持一致。如果您要将其他人的代码合并到您的项目中,请坚持使用他们使用的任何方法。如果您计划将来将此代码贡献给开源软件项目,请尝试遵守他们的编码约定。如果您从头开始编写自己的所有代码,我会说坚持您习惯使用的约定。当您稍后返回代码并尝试理解您所写的内容时,这将特别有帮助。
关于结构/类访问规范,您通常会看到首先列出公共成员,然后是受保护的成员,然后是私有成员(按照增加访问控制的顺序)。这样做主要是出于可读性的原因。当其他人使用您的代码时,他们将与这些公共成员进行交互,因此将它们放在声明的顶部可以更容易找到它们。以这种方式对成员进行排序可以将最有可能使用的信息保持在最靠近顶部的位置。我没有看到
friend
使用得太频繁,所以我不记得任何关于它的用法的模式。typedef
通常出现在顶部,以便在查看类的其余部分时,读者已经了解了您的自定义类型(也是出于可读性原因,typedef
是通常分组在一起并且不散布于成员声明中)。有许多现有的通用编码约定,它们的共同点就是标准。无论您使用什么系统,即使您自己定义它,如果您有一个概述编码约定的文档(或一页示例代码)也会有所帮助。一致性提高了可读性,尤其是当您将来某个时候重新访问旧代码时。
以下是一些编码约定,也许可以给您一些想法:
(2017 年 12 月:包含编码约定的 pdf 版本不再可用。)
The most important thing here is that you stay consistent. If you are incorporating other people's code into your project, stick with whatever method they were using. If you are planning on contributing this code to, say, an open-source software project in the future, try to abide by their coding conventions. If you are writing all of your own code from scratch, I would say stick with the conventions that you are accustomed to using. This will especially help when you come back to your code later and try to understand what you wrote.
Regarding structure/class access specifications, you will typically see public members listed first, followed by protected then private (in order of increasing access control). This is done mainly for readability reasons. When other people are using your code it will be these public members that they will be interfacing with, so placing them at the top of the declaration makes them easier to find. Ordering members in this fashion keeps the most likely to be used information closest to the top. I don't see
friend
used all too often, so I can't recall any patterns as to its usage.typedef
usually appears at the top so that when looking through the rest of the class, the reader already has an understanding of your custom types (also for readability reasons,typedef
s are typically grouped together and not interspersed with member declarations).There are a number of existing coding conventions out there in common use, and the one thing they have in common is a standard. Whatever system you go with, even if you define it yourself, it helps if you have a document (or a page of example code) outlining the coding convention. Consistency improves readability, especially when you are revisiting older code at some time in the future.
Here are a couple coding conventions to perhaps give you some ideas:
(December 2017: the pdf with coding conventions is no longer available.)
我通常尊重我正在编程的平台/环境的传统,除了我保持中立的多平台 C/C++ 项目。当为 Win32 平台进行 C++ 编程时,我倾向于使用匈牙利表示法来表示变量(类型或语义前缀)。在编写 MFC m_ 成员变量等时,我眼中唯一不太容易的是 Unix/POSIX
open_device_driver
约定与openDeviceDriver
驼峰风格。I usually respect the traditions of the platform/environment I'm programming in, except on multiplatform C/C++ projects where I'm neutral. When programming C++ for Win32 platform, I tend to use the hungarian-notation for variables (type or semantic-prefixes). When programming MFC m_ member variables, etc. The only thing that I cannot get easy in my eyes is the Unix/POSIX
open_device_driver
convention versusopenDeviceDriver
camelcase style.下划线在 UNIX 或跨平台代码中通常更为普遍。
Windows 代码往往是驼峰式的,
一般是公共的、受保护的、私有的,这是我所期望的——但也许这更多是从我的 C# 时代开始的。
underscores are often more prevalent on unix or cross platform code.
windows code tends to be camel cased
generally public, protected, private is what i would expect - but maybe that is more from my C# time.
数十年的编码工作让我的手指患上了一些疾病。每当我用小指按大写字母的[SHIFT]键时,我都会感到轻微的疼痛。
所以我开始更喜欢snake_notation。使用 AutoHotKey 实用程序,我将 [alt]+[space] 组合分配给 Snake 的“下划线字符”。我的拇指按[alt]有点不舒服,但比用小指好。 (实际上[ctrl]+[space]要好得多,但VisualStudio使用这个组合作为智能感知。)而且,我觉得它比camelCase更快。
我希望 i-rocks 为喜欢 Snake_notation 的程序员推出带有“下划线键”的新键盘。
Decades of coding as a job gave my fingers some disease. Whenever I use my little finger to press the [SHIFT] key for capital letter, I feel mild pain.
So I came to prefer snake_notation. Using AutoHotKey utility, I assigned [alt]+[space] combination to 'underscore character' of snake. It is little uncomfortable for my thumb to press [alt], but better than using little finger. (Actually [ctrl]+[space] is much better, but VisualStudio uses this combination as intellisense.) Also, I feel it is faster than camelCase.
I desire i-rocks launches new keyboard with 'underscore key' for programmers who prefer snake_notation.
对于我自己的项目,我采用了 Apple 编码约定。在编写大量 Objective-C 代码期间,我逐渐喜欢上了编写良好的 Objective-C 代码和 Cocoa 库的自记录性质。
因此,在 C++ 中,我一直坚持这些约定的精神,因为在我看来,它们提高了可读性,并且在某些情况下,正确命名也有助于我思考代码需要做什么。
SCREAMING_SNAKE_CASE:
FRAMES_PER_SECOND
PascalCase:
ClassName
camelCase:
descriptiveMethodName()
positionX
下划线:
_isGameOver
私有/受保护/公共:
私有:
protected:
public:
Apple 约定示例:
虽然需要多打字,但清晰度的提高通常是值得的。
有太多的例子无法提及这一点,但请考虑几乎所有教程中的介绍性代码示例。有意义的名称可以提高清晰度并减少程序员的错误。
我知道像 i、x、j 和 hWPTR 这样的名称有历史原因,但在我看来没有理由这样做,而且它们对教学来说很糟糕。
Microsoft 代码示例(永远复制和重复)是最严重的违规者之一。
严重地?!
当然,当与团队合作时,我很乐意采用任何标准。我希望我的代码看起来像属于该项目。唯一让我有点抽搐的是奇怪的牙套。
K&R 牙套是唯一正确的牙套。 :)
For my own projects I have adopted the Apple Coding Conventions. During my time writing a decent amount of Objective-C code I came to love the self documenting nature of well written Objective-C code and the Cocoa library.
So in C++ I have stuck with the spirit of these conventions because in my opinion they improve readability and on several occasions getting the name right also helped me think through what the code needs to do.
SCREAMING_SNAKE_CASE:
FRAMES_PER_SECOND
PascalCase:
ClassName
camelCase:
descriptiveMethodName()
positionX
Underscore:
_isGameOver
Private/Protected/Public:
private:
protected:
public:
Apple Convention Examples:
A little more typing to do but the increase in clarity is usually worth it.
There are too many examples to mention of this, but consider the introductory code examples from nearly any tutorial. Meaningful names increase clarity and reduce programmer error.
I understand there are historical reasons for names like i, x, j, and hWPTR but in my opinion there is no reason to do them and they are awful for teaching.
Microsoft code examples (copied and repeated forever) are some of the worst offenders.
Seriously?!
Of course when collaborating with a team I am happy to adopt whatever the standard is. I want my code to look like it belongs to the project. The one thing that does make me twitch a little are weird braces.
K&R braces are the only correct braces. :)
我使用下划线表示局部变量;宏和常量为 ALL_UPPERCASE; CamelCase 不用于任何内容,CamelCase 用于其他所有内容。
我总是使用结构而不是类,因此从 public: (不指定它)开始,然后将 private: 放在最后。
这都是非常主观的,没有正确或错误的答案。
I use underscores for local variables; ALL_UPPERCASE for macros and constants; camelCase for nothing and CamelCase for everything else.
I always use structs and never classes, and thus start with public: (without specifying it) and then put private: at the end.
This is all very subjective and there is no right or wrong answer.
我会说“如果你开始编写自己的库,就坚持你所知道的”,它们都经常使用。
这因程序员/团队而异。我按类别订购。我使用“内部/私有方法”类别,该类别通常是倒数第二个(最后一个被禁止实现)。
我很少使用它们;我将它们插入到依赖项(方法)之上,否则插入到构造函数/析构函数类别之后。
那就是我把它们放的地方。
如果您想了解详细信息,可以查看公开的编码约定。由于您已经拥有 Java 背景,因此您将能够轻松地领会其中的“味道”。
I'd say 'just stick with what you know on this if you are starting to write your own libraries', they are both used regularly.
This varies by programmer/team. I order by category. I use a category for 'internal/private methods', and that category is generally second to last (the last being prohibited implementation).
I use them rarely; I insert them above the dependency (method), otherwise, after the constructor/destructor category.
That is where I put them.
If you want to get into details, there are publicly available coding conventions. Since you already have a Java background, you'll easily be able to cut through the 'taste' in them.
如果某些语言具有命名约定(例如, https://peps.python.org/pep-0008 / 对于 python),在 C++ 中情况并非如此。
这可能与 B. Stroustrup(C++ 的创建者)“不喜欢单一文化”有关 (https://youtu .be/ZQds2aGHwDA?t=169)。
甚至 C++ 标准也遵循不同的命名约定(在拥挤和变化的世界中蓬勃发展:C++ 2006–2020,第 6.5 部分概念命名)。我认为 C++ 社区中没有流行的风格;我没有找到统计数据,但不同的公司使用的风格非常不同。
正如 Freek de Bruijn 所建议的,关键是一致性。如果您正在从事的项目遵循惯例,请遵守它。
如果没有,请遵循 Freek 帖子中建议的现有约定之一/这比发明自己的约定更容易。
If some languages come with naming conventions (e.g., https://peps.python.org/pep-0008/ for python), this is not the case in C++.
This might be linked with B. Stroustrup (the creator of C++) "disliking monoculture" (https://youtu.be/ZQds2aGHwDA?t=169).
Even the standards of c++ follows different naming convention (Thriving in a Crowded and Changing World: C++ 2006–2020, part 6.5 Naming of concepts). I don't think there is a prevalent style in the C++ community; I have not found statistics but different companies use very different styles.
As suggested by Freek de Bruijn, the key is consistency. If the project you are working on follows a convention, stick to it.
If not, follow one of the existing convention suggested in Freek's post/ it would be easier than inventing your own.