Jump to content
  • Advertisement
Sign in to follow this  
GenuineXP

Thoughts on a (C++) Naming Convention

This topic is 3695 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've established a set of conventions to be used with one of my projects, but they're still evolving (sometimes requiring that I troll the existing source code making changes). I was just wondering what some thoughts about a change I want to make are. But first, a question. For some reason, I can't think of an example of any global constants in the C++ standard library or Boost, upon which my conventions are based. Are there any, and how are they named? Anyway, currently any constant values are named just like any other variables but using all uppercase lettering. For example, MAX_HEALTH or POINTS_IN_A_TRIANGLE. I'm considering ditching the huge uppercase convention, so the above would become max_health and points_in_a_triangle. I think the lowercase forms are sexier (but arguably harder to identify). What do you think? I've run into libraries (some of which I'm using in this project) that also mirror standard library and Boost conventions that indeed don't name constants with uppercase lettering. However, I've seen plenty of code that does use uppercase lettering, but it seems to me this is more associated with Java- and C#-like naming conventions (which I'm not using). Silly question, but... any thoughts? Thanks.

Share this post


Link to post
Share on other sites
Advertisement
To the best of my knowledge, ALL_CAPS constant names originates from C, where constants were preprocessor definitions and preprocessor definitions were usually written in ALL_CAPS. I personally use SC++L naming conventions for everything in C++ (which defines constants such as std::ios::hex using lowercase).

Share this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk
To the best of my knowledge, ALL_CAPS constant names originates from C, where constants were preprocessor definitions and preprocessor definitions were usually written in ALL_CAPS. I personally use SC++L naming conventions for everything in C++ (which defines constants such as std::ios::hex using lowercase).

Great, that's just the kind of example I was looking for.

One reason I wanted to make this change (other than the fact that I just think the uppercase naming is a bit ugly) is to help distinguish between constants and macros (since I use a few macros in the project).

EDIT:

Oh, and I wanted to apply the same changes to enumerators. How are these typically handled?

Share this post


Link to post
Share on other sites
Quote:
Original post by GenuineXP
Silly question, but... any thoughts? Thanks.


I believe it's a toolkit/API convention, Microsoft APIs use uppercase, whereas MacOS (9 and earlier) APIs use a mix of lowercase and uppercase (If I remember correctly).

Share this post


Link to post
Share on other sites
Quote:
Original post by GenuineXP
Oh, and I wanted to apply the same changes to enumerators. How are these typically handled?


The same. An enumeration value is, by definition, a constant.

Share this post


Link to post
Share on other sites
Quote:
Original post by cdoty
Quote:
Original post by GenuineXP
Silly question, but... any thoughts? Thanks.


I believe it's a toolkit/API convention, Microsoft APIs use uppercase, whereas MacOS (9 and earlier) APIs use a mix of lowercase and uppercase (If I remember correctly).


All of which have strong origins in C.

Share this post


Link to post
Share on other sites
Classic Mac OS was more Pascal-oriented, from what I recall -- it used Pascal calling and string conventions, at least, and all the original Inside Macintosh books were written to Pascal (sometimes with C and assembly side-by-side examples).

Share this post


Link to post
Share on other sites
I have been drifting away from writing constant in all-capitals.

I don't truly see the need to distinguish between constants and variables by their name, much like stating the data type of a variable in its name. Sometimes a constant needs to be turned into a variable, or vice-versa, and then you have to grep your whole code.

With const-correctness, things get a fraction foggier as for when exactly to use an all-caps name or otherwise. Having something as const doesn't mean you should give it an all-caps name - the rules need to be more precise, such as just when you assign a literal value to a constant semantic, but then so what? Const-ness can even be casted away.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!