Do you usually prefix your classes with the letter 'C' or something else?

Started by
89 comments, last by Ravyne 7 years, 11 months ago

Actually, m_ for members and g_ for globals helps quite a bit while reading a lot of unknown code.


The problem is that this kind of convention is brittle. It encodes information about scope into the variable name, but if you refactor to change scope the encoding is wrong and the variable must be renamed. Forget or neglect to rename the variable and now you've got misleading information.

A far more robust convention is to require the use of this-> for members and to require full namespace naming for globals. The compiler can then help you by catching incorrect usage. In an ideal world C++ would have had these requirements from the outset.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

Advertisement

Actually, m_ for members and g_ for globals helps quite a bit while reading a lot of unknown code.


The problem is that this kind of convention is brittle. It encodes information about scope into the variable name, but if you refactor to change scope the encoding is wrong and the variable must be renamed. Forget or neglect to rename the variable and now you've got misleading information.


Any kind of extra encoded information in a variable name can be omitted or out of date by mistake; that holds regardless of convention. I don't feel that's a strong enough argument against the practice. I would argue that if the scope of a variable has changed, then its semantics have changed enough to warrant a rename.

A far more robust convention is to require the use of this-> for members and to require full namespace naming for globals. The compiler can then help you by catching incorrect usage.


I didn't know you could ask the compiler to flag members accessed without an explicit this-> and full namespace qualification of symbols within the current namespace. How does one do so? Without that compiler enforcement, using this-> and full namespace qualification are just more conventions...

The real question is, do you prefix your structs with the letter 'S'?

So far I also don't prefix struct with S cause I treat struct as an obvious collection of PODs ... something like EmployeeCard containing information about an Employee, instead of containing both information and behavior of what an Employee can do, which I prefer it as an Employee class.

So by that term alone, I have different naming for on struct or class, so I don't really need S or C prefixes on them.

The idea has mostly fallen out of favour these days, but that's where it came from.

I'd say that the idea is pretty much just common sense and gets used everywhere. Except instead of as abbreviated prefixes, people use all sorts of forms of it, e.g.
scrnCoordSomething
somethingScreenCoordinate
something_sc

etc


Sorry yes, I meant the abbreviated prefixes rather than the concept as a whole.

I think ideally, as SeanMiddleditch pointed out, we have strong typedefs so you couldn't do


typedef int Row;
typedef int Column;

Row x;
Column y;

x = y; // error, requires cast of some sort
if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight

I've started suffixing (as opposed to prefixing) my classes with the word Class. And I use Pascal Case because I started my formal training in Pascal and I like it that way and there's no one that can tell me not to. ;-)

But I'm writing tutorials with beginners in mind. So, I figure, the more explicit the better. What's an object. What's a class. What is this. I figure putting the word Class at the end takes 5 characters and a smidgen of time. VS pretty much autocompletes everything anyway. But there's no question about what it is when the word "class" is slapping you in the face. It also allows me to give my objects the same name, just remove the word "class" from the end. I'm also giving classes their own cpp files and headers and the file names are the class name with the word class at the end.

It also makes it clear that my Texture2DClass.h and .cpp files are dedicated strictly to the Texture2DClass class and you'll know where to go looking for the code of that class without question.

It's maybe a bit overkill, but I'm feeling pretty good about this convention so far.

I also follow this convention elsewhere, by suffixing pointers with the word Pointer and such. I don't bother with standard types like ints and strings. I generally define those pretty locally anyway, and you probably only have to go up one page of code to look up their definition because I try to define all variables at the start of the function/method.

I've started suffixing (as opposed to prefixing) my classes with the word Class.


I just threw up in my brain :(

I've started suffixing (as opposed to prefixing) my classes with the word Class.

I just threw up in my brain :(

At least the suffix is at the end of the name not the start so it doesn't mess up autocomplete :)
Sure sure... it's just worthless information encoded in to a name which a human is reading... and just wait until you need a 'ThingClassClass'... or when you decide it is no longer a class.. or something becomes a class...

Sure sure... it's just worthless information encoded in to a name which a human is reading... and just wait until you need a 'ThingClassClass'... or when you decide it is no longer a class.. or something becomes a class...


Or a type for a classification of a group of students; a ClassClassClass? :lol:
We need to class deeper..

This topic is closed to new replies.

Advertisement