What is the preferred naming conventions du jour for c++?

梦想的初衷 提交于 2019-12-07 14:26:26

问题


I am quite confused by looking at the boost library, and stl, and then looking at people's examples. It seems that capitalized type names are interspersed with all lowercase, separated by underscores.

What exactly is the way things should be done these days? I know the .NET world has their own set of conventions, but it appears to be completely different than the C++ sphere.


回答1:


What a can of worms you've opened.

The C++ standard library uses underscore_notation for everything, because that's what the C standard library uses.

So if you want your code to look consistent across the board (and actually aren't using external libraries), that is the only way to go.

You'll see boost use the same notation because often their libraries get considered for future standards.

Beyond that, there are many conventions, usually using different notations to designate different types of symbols. It is common to use CamelCase for custom types, such as classes and typedefs and mixedCase for variables, specifically to differentiate those two, but that is certainly not a universal standard.

There's also Hungarian Notation, which further differentiates specific variable types, although just mentioning that phrase can incite hostility from some coders.

The best answer, as a good C++ programmer, is to adopt whatever convention is being used in the code you're immersed in.




回答2:


There is no good answer. If you're integrating with an existing codebase, it makes sense to match their style. If you're creating a new codebase, you might want to establish simple guidelines.

Google has some.




回答3:


It's going to be different depending on the library and organization.

For the developer utility library I'm building, for example, I'm including friendly wrapper modules for various conventions in the style of the convention. So, for example, the MFC wrapper module uses the 'm_typeMemberVariable' notation for members, whereas the STL wrapper module uses 'member_variable'. I'm trying to build it so that whatever front-end is used will have the style typical for that type of front-end.

The problem with having a universal style is that everyone would have to agree, and (for example) for every person who detests Hungarian notation, there's someone else who thinks not using Hungarian notation detracts from the basic value of comprehensibility of the code. So it's unlikely there will be a universal standard for C++ any time soon.




回答4:


Find something you feel comfortable with and stick with it. Some form of style is better than no style at all and don't get too hung up on how other libraries do it.

FWIW I use the Google C++ style guide (with some tweaks).

http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml



来源:https://stackoverflow.com/questions/361176/what-is-the-preferred-naming-conventions-du-jour-for-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!