根据范围(类、方法、全局...)的变量命名约定
我很快开始了一个小型 C++ 项目,并根据范围详细阐述了一些变量的命名约定规则。
我创建了类成员 _prefixed_with_underscore
和方法参数 suffixed_with_underscore_
。我很快就对为所有东西发明命名约定感到神经质,比如全局变量(好吧,这些可能很重要)、内联全局函数,以试图提高代码的可读性。
我读过这个问题及其答案,回答了我的一些疑问命名约定,特别是关于方法参数名称。也许使用 a_rule_like_this_
作为参数名称可能不是一个好主意。
所以,我的问题是,在编程时,您对不同的“实体”使用什么命名约定,特别是参数名称?谢谢。
I soon started a small C++ project and elaborated some naming convention rules for variables, according to scope.
I made class members _prefixed_with_underscore
and methods parameters suffixed_with_underscore_
. I soon got neurotic about inventing naming conventions for everything, like global variables (ok, those might be important), inline global functions, to try to improve code readability.
I've read this question and its answers that answered some of my doubts about naming convention, specially about methods parameters names. Maybe having a_rule_like_this_
for parameters names might not be a good idea.
So, my question is, what naming conventions do you use for different "entities" when programming, specially for parameter names? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
有很多人创建了约定 - 谷歌搜索 c++ 命名约定
还有一些风格指南也建议了编程方法。
您可能不会同意其中的所有观点,但在您的项目/团队/公司中保持一致是重要的。
此外,不同语言(以及跨操作系统)的命名约定也会有所不同,因此不要对 C++ 使用 PHP 建议。 (前面的 _ 给 C++ 带来了问题)对于 C++,最常见的约定是参数是普通文本,但实例变量要么以 _ 结尾,要么以 m_ 开头(我更喜欢前者)
There are many people who have created conventions - google for c++ naming convention
Also there are style guides which also suggest ways of programming.
You will probably not agree with all the points in one but being consistent in your project/team/company is the important one.
Also naming conventions will differ for different languages (and across OS) So don't use PHP suggestions for C++. (the preceding _ gives a problem fo C++) For C++ the most common conventions are that parameters are normal text but instance variables either end in _ or start with m_ (I prefer the former)
切勿使用前缀“_”来创建任何类型的标识符。带前缀的下划线是为编译器实现者保留的,用于他们自己的邪恶目的。这意味着 C/C++ 编译器实现者创建宏、隐式局部变量、命名空间是完全有效的,只要它们至少有一个下划线。
如果您的代码在所有其他方面都符合 C++ 标准,则标识符上前缀下划线的存在意味着它不会在其他编译器或您现在使用的编译器的不同版本上进行编译。
Never make any type of identifier that uses a prefixed '_'. The prefixed underscore is reserved for compiler implementers to use for their own neferious purposes. Meaning its perfectly valid for C/C++ compiler implementors to create macros, implicit local variables, namespaces, as long as they have at least one underscore.
If your code conforms to the c++ standard in every other way, the presence of prefixed underscores on identifiers mean that there is no expectation that it will compile on other compilers, or different versions of the compiler you are using right now.
不要在标识符前添加下划线,因为某些编译器将它们保留用于自己的非标准目的。
我喜欢 Google C++ 样式指南中的命名约定。
Do not prefix identifiers with an underscore, as some compilers reserve them for their own non-standard purposes.
I like the naming conventions in the Google C++ style guide.
这只不过是一个意见问题。所以,这是我的。
这是我的一些约定:
<块引用>
This is little more than a matter of opinion. So, here's mine.
Here's some of my convention: