Prefix Class Names with 'C' Good Practice?
I prefer the .NET naming guidelines. They make sense and they're not wonky with crazy conventions.
Ok first question: should I start off class names with C? I'm going to give you a short answer here but by far the best answer that anyone will give you is found at this link:
http://www.joelonsoftware.com/articles/Wrong.html
I assume that you know something about type safety. You can't assign a Texture to a Mesh, because the compiler won't let you. You can name a function f and you can name a class c, but the compiler won't let you do this will it: f=c; No of course not, because such statements are nonsense and the compiler recognizes them as such, thanks to compile-time type checking.
Now sometimes you don't have as much typesafety as you need. Let's say that you are using a simpler language, maybe C or even something simpler, or maybe other factors prevent you from using all of the features that C++ offers. Your program deals a lot with array indices and color values. You decide to represent both in your code using unsigned integers.
However is a Color an array index? If you have a variable named color_of_shirt and another named index_into_shirt_properties would it make any sense to do something like this: index_into_shirt_properties = color_of_shirt; Well no, because an index and a color aren't the same thing. So not only is that code wrong, but it is nonsense. However both are unsigned ints, so the compiler can't catch this kind of nonsense.
Thus in this case a naming convention will catch your error by making the code look wrong. Would putting ui (for unsigned int) onto the from of either variable have helped. The answer is a resounding NO. If your language provides a feature let the compiler take care of it, it if doesn't then and only then should you step in.
Oh and for your second question:
Instead of doing this:
Matrix Matrix;
do this:
Matrix matrix;
or if the variable is of a very limited scope (never leaves the screen) do this:
Matrix m;
http://www.joelonsoftware.com/articles/Wrong.html
I assume that you know something about type safety. You can't assign a Texture to a Mesh, because the compiler won't let you. You can name a function f and you can name a class c, but the compiler won't let you do this will it: f=c; No of course not, because such statements are nonsense and the compiler recognizes them as such, thanks to compile-time type checking.
Now sometimes you don't have as much typesafety as you need. Let's say that you are using a simpler language, maybe C or even something simpler, or maybe other factors prevent you from using all of the features that C++ offers. Your program deals a lot with array indices and color values. You decide to represent both in your code using unsigned integers.
However is a Color an array index? If you have a variable named color_of_shirt and another named index_into_shirt_properties would it make any sense to do something like this: index_into_shirt_properties = color_of_shirt; Well no, because an index and a color aren't the same thing. So not only is that code wrong, but it is nonsense. However both are unsigned ints, so the compiler can't catch this kind of nonsense.
Thus in this case a naming convention will catch your error by making the code look wrong. Would putting ui (for unsigned int) onto the from of either variable have helped. The answer is a resounding NO. If your language provides a feature let the compiler take care of it, it if doesn't then and only then should you step in.
Oh and for your second question:
Instead of doing this:
Matrix Matrix;
do this:
Matrix matrix;
or if the variable is of a very limited scope (never leaves the screen) do this:
Matrix m;
Quote:Original post by Raeldor
Will using using MyNamespace::Matrix; override a declared standard C structure such as Matrix in GDI+, or will it still be unresolvable when you reference it in a function?
It won't "override" it; it'll simply generate a name collision at compile-time. This is when you fully qualify your types. Contrived example:
#include <vector>struct vector{ // blah;};vector v; // my own vector typestd::vector< vector > myvectors; // no confusion
But what if they're from two external libraries?
#include <Lib1_Matrix.h>#include <Lib2_Matrix.h>// this function uses Lib1::Matrix mostly, only using Lib2::Matrix in a few placesvoid func1(){ using Lib1::Matrix; Matrix a; ... Matrix b; ... Matrix c; ... Lib2::Matrix d; ...}
Congratulations, you finally understand the flexibility and intent of the C++ using directive. And, yes, it would be nice if you could alias things like L1Matrix = using Lib1::Matrix or some less-ugly equivalent. *shrug*
Quote:Original post by Oluseyi
And, yes, it would be nice if you could alias things like L1Matrix = using Lib1::Matrix or some less-ugly equivalent. *shrug*
typedefs would get you pretty far in this regard, although they might not offer the scoping behavior that you may want.
CM
Quote:A typedef declaration introduces a name that, within its scope, becomes a synonym for the type given by the type-declaration portion of the declaration. You can use typedef declarations to construct shorter or more meaningful names for types already defined by the language or for types that you have declared.
Which suggests that typedef does what Oluseyi asks.
I recently found this: C++ Programming style Guidelines. It's a collection of all most common styles. It's very close to the style I personally use, and it gives good motivations for every rule.
The only thing I don't fully agree with is to add an underscore to a private member (rule 11). I just use "this->" to unambiguously denote members. This is also how it's done in Java and it reads very fluently.
The only thing I don't fully agree with is to add an underscore to a private member (rule 11). I just use "this->" to unambiguously denote members. This is also how it's done in Java and it reads very fluently.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement