views:

190

answers:

7

Assuming that a project has been using the C class prefix for a long time, and it would be a waste of time to change at a late stage, and that the person who originally wrote the style guide has been hit by a bus, and that there are no structs in the code already...

It's a pretty trivial question, but if a C++ code style guide says "use C for class name prefix" then should this be taken to mean also use C for struct prefix also, or should we use something different, like S for example.

class CFoo { };
struct CBar { };

... or ...

class CFoo { };
struct Bar { };
+16  A: 

Simple answer - don't use the C prefix for classes. This is hungarian notation of the most pointless sort. It's probably time to re-write the style guide. Frankly (and speaking as someone who's written several of the things), most such guides are rubbish and/or were written long, long ago and never updated.

anon
Given the fact that the question is about the consistency of the style guide, basically you say that you don't have an answer...
xtofl
I always thought this one was mongolian notation ...
Nikolai N Fetissov
The notation is pretty pointless, I agree, but that was not the question, as xtofl said. The question was if it has been used for classes, then should it be used for structs?
nbolton
@Nick OK, the guide, if it is any good at all, should explain WHY the C prefix is being used for classes. If the same reasoning applies to structs, then use an S.
anon
I agree with the sentiment, but this is more of a comment than an answer to the question.
Shmoopty
@Nick I suspect it was copied blindly from Microsoft's original (and unwritten) MFC standards.
anon
@Niel, oops, deleted my comment. But thanks for replying. This sounds logical to me.
nbolton
I think this answer is right, don't spend time on problems of your own making when you can remove them. The C prefix is just about made worthwhile in Symbian, which uses C for "proper classes", T for value types, R for handles to kernel objects, and M for mixins. But I don't miss it in other C++ projects, and adding C to every single user-defined type tells you nothing. C++'s syntax makes it hard to always know what's a type and what isn't, but not that hard.
Steve Jessop
+8  A: 

If the style guide doesn't specify, I would (probably) use the "structs are classes with all members public"-rule to use C for structs too, yes. Or I would think "hah, here's a loophope to get around that silly initial rule, yay" and not use it. In other words, this is highly subjective.

unwind
Haha, loophole, I agree! +1
nbolton
+4  A: 

If the code style guide doesn't specify, find code that's been following the style guide and see what's already been done.

If there is no code already following the style guide, come to an agreement with everyone involved in the project.

If nobody else is involved in the project, just decide and be consistent.

Shmoopty
+2  A: 

I think this guideline is stupid and confusing.. The fact that you had to ask this question proves it.

Coding styles are meant to increase readability; it's obvious if an identifier is a class or not, especially if you are using a decent IDE with mouseover tooltips.

Andreas Bonini
This kind of style guide indeed stems from the pre-ide era. That doesn't make it useless.
xtofl
Well, refusing to use an IDE for some reason and then resort to these workarounds is shooting yourself in the foot.
Andreas Bonini
Style guides, really, have nothing to do with IDEs. As others have stated, the guides should be for readability and consistency. They also help in code reviews (at a bare minimum to flame people who don't follow them).
Thomas Matthews
+2  A: 

We usually use C prefix for classes and T prefix for structs that have no methods (ie, "C" structs).

jrbjazz
Same here. (-padding to meet 15 characters limit-)
n0rd
And if a method is inserted into the structure or class, the name must be changed (to meet the guideline) and all the code reviewed to propagate the name change. Primary vote against Hungarian and similar styles.
Thomas Matthews
+2  A: 

For me it would come down to:

Do you want the readers of your code to immediately differentiate between the two declaration types?

While the use of the prefix is generally distasteful, carefully consider the view of the code maintainer. Is it helpful for them to think, "Ah! no C prefix, this is a struct". Using a struct instead of a class may imply something specific in your code. If it doesn't, it makes more sense to continue to use the prefix for the sake of the maintainer.

luke
+1  A: 

If a style guide does not serve its purpose to promote easy readability, consistency, and correctness, it should be modified until it does so or thrown into the circular file (trash can).

Also, if people don't follow it, then it should be updated so that it is easier to follow (or the tools amended to make coding to the guidline easier).

Thomas Matthews