Common notation

Started by
39 comments, last by SiCrane 18 years, 3 months ago
Quote:Original post by Roboguy
Quote:Original post by ZadrraS
I'll start writing classes in all caps from now on.


I wouldn't, if I were you. One of the most common conventions among C and C++ programmers is to use all caps for constants and macros.


Ahh.. But I do wish to make my classes stand out as much as possible, I write the instances of the classes all in lower caps, and I need to see a clear diffrence between a class and its instance. Till now "_player player" or "PLAYER player" looked good enough, but if I can't use those, what then?

Quote:Original post by Mordt
I'm just a beginner programmer but I tend to prefix all my classes, functions and variables for easy identifying with either C, F or V

e.g.

CThisIsMyClass

FThisIsMyFunction

I tend to write variables like this

Vthisismyvariable

I don't know if this is good or bad but it is helping me learn, not sure what others would think though.

Heh. I certainly don't want to use those. IMHO things like these makes the code VERY ugly.. (just an opinion :) )
Advertisement
Player player
I'm not sure if this is a common thing, but naming an instance the same name as the class is probably not the greatest idea in the world. Just might lead to confusion/typos. I'm guilty of doing this myself, but I've oftentimes regreted it, especially when its just one or two leters that are different(like no longer capitalized). An exception to this might be if you have a singleton.

Anyone else agree with this since I'm *far* from being even good at writing code.

I agree with the others that you should not name your classes in all caps. That is really the one convention that is extremely consistent, so its even worse when its been broken.
And that's exactly why I want the same words to be as diffrent as possible.
I remmember using "Player player" before, and it led to a lot of confusion. "_player player" fixed this for me...
Normally struct names are all caps and class names have the first letter of each word capitalised.

As for your Player player thing, why not try this:
Player thePlayer;
"I just wanted to hang a picture on my wall, but somehow now I'm in the Amazon Jungle looking for raw materials." - Jekler
Quote:Heh. I certainly don't want to use those. IMHO things like these makes the code VERY ugly.. (just an opinion :) )



Your right ZadrraS and I must get out of the habit :> It is helping me though and some standard for of notation would be helpful if there was such a thing :)
Mordt: I'm just a beginner C++ programmer so take my advice with a grain of salt.Krayven Entertainment
Quote:Original post by Mordt
I'm just a beginner programmer but I tend to prefix all my classes, functions and variables for easy identifying with either C, F or V

e.g.

CThisIsMyClass

FThisIsMyFunction

I tend to write variables like this

Vthisismyvariable

I don't know if this is good or bad but it is helping me learn, not sure what others would think though.


i dont think this is required, which things are functions and classes are fairly obvious from how they are used and so are variables.

some people like to declare member variables different, but not me, i know my member variables from the lack of declaring them inside a member function...
Naming structs all capital comes from C where things like
typedef struct MyStruct { ... } MYSTRUCT;
were done, as far as I know, to avoid the need of writing
struct MyStruct
and instead writing
MYSTRUCT
However, since C++ that isn't needed any longer, and I personally see no requirement of distinguishing between classes and structs in this sense.

My favorite writing is like this:
/**Class that does anythingYackety-yak about this class.*/template<size_t A_GENERIC_CONSTANT_g,typename AnyType_g>class AnyStructOrClass {public: // traits   /**   explanation of the type.   */   typedef AnyType_g AnyScopeInternalType_t;   /**   explanation of the constant.   */   static const int32_t A_MULTI_WORD_CONSTANT;   /**   explanation of the enums.   */   enum Something_t {      /// an explanation.      AN_ENUM_CONST   };public: // stuff of doing something   /**   explanation of the method.   */   void anyFunctionOrMethod(AnyType_g anyAnArg) {      AnyType_g anyLocalVar;   }   /**   explanation of the field.   */   int32_t anyPublicMemberField;protected: // stuff of doing something   /**   explanation of the field.   */   int32_t _anyProtectedMemberField;private: // stuff of doing something   /**   explanation of the field.   */   AnyType_g _anyPrivateMemberField;};

where the comments are formatted as are due to doxygen.

A thing I know that could be complained is the leading underscore of protected/private member fields.
Quote:Original post by SiCrane
Quote:Original post by Xai
As mentioned already, DO NOT start any non-members with an underscore, it just makes life easier (some people do start member variables with an underscore ... which is ok).

Underscore followed by a capital letter is not ok, even for member variables.
Oh, come on. Stop nitpicking on this issue already.
I realize that it may be good to know that they're technically reserved, in fact I like trivia and obscure language details as much as the next guy, but do we really have to comment on it every single time someone shows a code snippet containing a leading underscore in one of the identifiers? I mean if someone uses "foo" then no one complains but apparently "_foo" is some kind of sin, now which one do you think is more likely to cause problems?

I personally use them all over the place to mark internal things (i.e. private or local), mostly because I find trailing underscores quite ugly. In fact the consistent use of underscores to separate all of those short local names combined with the use of module prefixes and namespace has significantly reduced the number of collisions I've had to deal with during the last few years.

And guess what, they've *never* collided with any reserved identifiers for me. Ever (well, I can't be absolutely certain but it hasn't happened in recent history).
On the other hand I've frequently crashed into the WinAPI (I swear they must have defined half the English language in that rat's nest of headers) and various other libraries. And in any event a global search and replace to fix the name is rarely more than a two minute affair, especially with private names since they're by their very nature easy to fix.

</off topic rant>

[Edited by - doynax on January 7, 2006 8:48:55 AM]
Quote:Original post by doynax
Quote:Original post by SiCrane
Quote:Original post by Xai
As mentioned already, DO NOT start any non-members with an underscore, it just makes life easier (some people do start member variables with an underscore ... which is ok).

Underscore followed by a capital letter is not ok, even for member variables.
Oh, come on. Stop nitpicking on this issue already.
I realize that it may be good to know that they're technically reserved, in fact I like trivia and obscure language details as much as the next guy, but do we really have to comment on it every single time someone shows a code snippet containing a trailing underscore in one of the identifiers? I mean if someone uses "foo" then no one complains but apparently "_foo" is some kind of sin, now which one do you think is more likely to cause problems?

Leading underscore, not trailing underscore. And the nitpick was about leading underscore followed by a capital ex: _Foo, not _foo, and it wasn't someone showing an identifier, it was someone trying to explain what identifier patterns to avoid. And guess what, I have run into problems with that one. One of the Metrowerks preprocessor versions took the identifier _Line and stripped it out of source code. So yes, it is worth commenting on. Just because you've gotten away with doing a bad practice doesn't mean that it isn't a bad practice.

This topic is closed to new replies.

Advertisement