Jump to content

C++ Coding Standards

Recommended Posts

It's high-time we discussed coding standards. So let's discuss style and other things.

C++11 is always acceptable, and preferred. However, some elements of STL are best avoided, such as their string library. Containers (vectors, lists, maps, etc), random number generators, etc are mostly fine though.


Regarding variable and class property naming, I use Hungarian notation for naming variables: [variable type]<name in uppercase>. So for instance, here are some variables:

  • strings (sz): szTreasureClass
  • whole numbers (n): nNumberOfEntities
  • bitfield (bf): bfShowFlags
  • boolean (b): bContinue
  • floating point (f): fPrecision
  • 3D vector (vc): vcDirection
  • container vector (v): vNumbers
  • unordered map (um): umTreasureClasses
  • byte (_1): _1ByteSized
  • short/word (_2): _2ShortStuff
  • dword (_4): _4DoubleWorded
  • qword (_8): _8QuadAwesome
  • reference ®: rSomeReference

Arrays and pointers start with a and p respectively, and can optionally be suffixed with another value. So for instance afSomeFloats indicates an array of floats. These can also be nested indefinitely along with the type: ppszSomeString indicates a pointer to a pointer to a string. If it's not on this list, I might have either forgotten, or it's a custom structure type, in which case it is blank.


You'll find that this is similar to Microsoft's schema of naming things. The reason I have picked Hungarian notation is that it allows for variables to be named based on their type, which clears up a lot of confusion. For instance, consider the first variable that I have named: szTreasureClass. There can also be a pointer to the actual treasure class named pTreasureClass. In other naming schemes, you'd probably have a variable named treasureClass to indicate the pointer, and treasureClassName to indicate the name. Camelcase is not only ugly when used like this, it's not immediately apparent what treasureClass is supposed to be. Is it a pointer? Is it a structure? Such confusion is cleared up with this system.



Regarding spacing, 4-space tabs only. Text should fit comfortably on a 1080p monitor.


Microsoft's naming guidelines are an excellent resource. https://msdn.microsoft.com/en-us/library/ms229002%28v=vs.110%29.aspx


Feel free to discuss or share an opinion about these. I am still trying to come up with the best convention that I can.

Link to comment

I'll just out my opinion out there that Hungarian notation is more effort than it's worth, having used it at my last job. In some situations it's good (when dealing with bit manipulation for packed variables) but in almost all other situations you can usually infer the type from the variable name. The only place I would consider using any sort of prefix is for pointers (e.g. pEntity vs entity).

Link to comment

I'm not a real coder per se, but I'm familiar with C++ syntax somewhat and have scripted in Pawn for AMXX and SourceMod.


The MS link you gave tells not to use Hungarian notation. At first I didn't use it, then later I did, then I didn't use it anymore, then I started using it again, but in the end I dropped it due to it being quite useless in my eyes.

The page also contradicts itself when saying that when an acronym is made of only 2 letters, it's ok to leave both capitalized: they use IO in IOStream, but Ok instead of OK, unless OK is somehow not an acronym for "o kay" and it should be "ok" or "Ok" everywhere else outside coding. Also the idea that it's either ioStream or IOStream is silly IMO, it should be io or Io, never IO.

Link to comment

OK is a weird case. I don't know if it's an abbreviation or what.

Those are the C# standards which apply to mostly classes, not variables. I think they're still a good standard for C++ structs since structs and classes are identical (structs use public visibility by default, whereas classes use private visibility by default).


There's another Microsoft document somewhere (on cell atm) that has all their Hungarian abbreviations. I know that Win32 definitely uses these for parameter names at the very least.


Unrelated: Pawn is a nice little scripting language and I'd love to use it for a project sometime. Kudos for actually having used it. :)

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...