Archived

This topic is now archived and is closed to further replies.

Cybertron

C++ notation and naming conventions

Recommended Posts

I am working on a new graphics interface right now using OOP, and was wondering what are the standard naming conventions for classes, variables, functions and enums I already use hungarian notation for variable names (except for ''m_'' or ''g_'') but classes are all over the place and functions are usually just describe what they do In need your input on a logical way to orginize all these data types and structures

Share this post


Link to post
Share on other sites
quote:
Original post by Cybertron
I am working on a new graphics interface right now using OOP,
<snip>
I already use hungarian notation for variable names


Some people are going to tell you that HN is redundant in an OO world.




"I would defend the liberty of concenting adult creationists to practice whatever intellectual perversions they like in the privacy of their own homes; but it is also necessary to protect the young and innocent."
Arthur C Clarke

Share this post


Link to post
Share on other sites
quote:
Original post by Magmai Kai Holmlor
Standards are great! There''s so many to choose from!


Haha! Too true, too true.

/*=========================================*/
/* Chem0sh */
/* Lead Software Engineer & Tech Support */
/* http://www.eFaces.biz */
/*=========================================*/

Share this post


Link to post
Share on other sites
The only "notation" I use are to indicate scopes and
to distinguish pointers from other types.

g_ : global variable (ewww!)
m_ : member variable (very nice)
s_ : static variable (...)
p : pointer variable (MUST!)

That''s all! I need to distinguish pointers from the others
because pointers use -> rather than . to access members.
References are fine in that regard.



Kami no Itte ga ore ni zettai naru!

Share this post


Link to post
Share on other sites
Here''s my notation:
(local) variables:-- item, itemIndex, itemStr (not strItem, yuk!)
member variables:-- item_, itemIndex_, itemStr_
global variable:-- theItem, theItemIndex, theItemStr
pointer:-- pItem, pItemIndex, pItemStr
pointer global:-- pTheItem, pTheItemIndex, pTheItemStr

(member) functions:-- EatItem(), Eat()
#define/const int:-- MAX_ITEM, MAX_SIZE, LOG()
classes/structs:-- Star, Rectangle, Object, Alien, SuperAlien
template <class X>:-- where X = T, U ,V ,W... or A (for allocator )
quote:
Quote from tangentz
p : pointer variable (MUST!)


YES, IS A MUST!!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I put caps on things that there are one of, and go lower case with underscores for members.

Class
GlobalVariableOrFunction
StaticVariableOrFunction

member_variable_or_function
local_variable_in_a_function

so if you see C::D you know it is static whereas C::d is just a plain member. Likewise if you see V you know it is a global variable or function and if you see v it is a local variable of function

Share this post


Link to post
Share on other sites
Thanks for the input

It looks like naming scope of the variable is a good idea. local variables can be left alone but classes can have all their variables prefixed with m_.

What should I call classes? I either start it with C or ends in "Class". Interfaces usually start with I_ (like I_GraphicsInterface). What could I call the implementation of that interface?

Share this post


Link to post
Share on other sites
quote:
Original post by Kylotan
What use is a prefix to denote scope? Everything should have as small a scope as possible anyway.



Right, the majority of my code has no "warts" attached to
the variable names. But m_ is so useful to denote
member variables that you shouldn't ignore it.

All my member variables are private and I write public or
protected accessors to them if there is a need to. They
all look like:


    
class Vec
{
float m_X, m_Y, m_Z;
public:
float GetX(void) const { return m_X; }
void SetX(float X) { m_X = X; }
float GetY(void) const { return m_Y; }
void SetY(float Y) { m_Y = Y; }
float GetZ(void) const { return m_Z; }
void SetZ(float Z) { m_Z = Z; }
};
// end



Especially in the Set-methods, I simply reuse the "X" or "Y"
or whatever to name the parameters. There is never a
confusion as to which is member and which is parameter.

In fact, you can write some macros:


#define GET(T,X) T Get##X(void) const { return m_##X; }
#define SET(T,X) void Set##X(T X) { m_##X = X; }




Kami no Itte ga ore ni zettai naru!

[edited by - tangentz on July 30, 2002 6:56:10 PM]

Share this post


Link to post
Share on other sites
I have only three notation rules:

1.Class Names start with C
2.Interface names start with I
3.More recently I have put p in front of pointer variables (helps remind me to use ''->'' instead of ''.'')

I''d just use whatever notation style suits you

Share this post


Link to post
Share on other sites
quote:
Original post by Grambo
...
1.Class Names start with C
...


I''m quite irritated with this notation ... I don''t use it normally, but ... when I''m dealing with MFC.

Share this post


Link to post
Share on other sites
I prefix each variable name with the type, the scope, the function name it''s initialized in, the time and date it was created, and the entire name of the variable UUencoded.

Sheesh. Besides the fact that most IDE editors worth their salt will quite gladly cough up type information, if you can''t tell whether a variable should be a char* or a double from the name, your code has serious problems. Spend your time writing long, descriptive variable names to exposelogic errors; the compiler will happily expose syntax errors for you.



Don''t listen to me. I''ve had too much coffee.

Share this post


Link to post
Share on other sites
quote:
Original post by Arild Fines
Some people are going to tell you that HN is redundant in an OO world.



It is redundant and in some cases self defeating when you have to change a varable type or such.

m_
g_
s_

are retardard (excuse the overtone) in OO and modern IDE''s because:
(1) Code fragments should be kept small so keeping track shouldn''t be a problem.
(2) The IDE should be able to provide all the type and scope information needed.
(3) The number of globals needed in OO should be so small that they can be listed on one hand.

As for the p notation for pointers, I''m a little more relaxed about that (personaly I don''t use it and rely on Visual Assist) becasuse so many problems are caused by pointers that the extra inforation can be useful.

As for capatilization I adhere to one of two standards:
(1) varName
(2) var_name

As for classes, variables, functions and enums again this information should be provided by the IDE. With the addition of well documented code namming convention should be able to be kept to as little as posible.

Notation standards are easy to do when working on your own project, unfortunatley I don''t have the same luxuray at work and have to adhere to there standards.

Share this post


Link to post
Share on other sites
quote:
Original post by RegentZ
(2) The IDE should be able to provide all the type and scope


The over-reliance of such "mythical", all-powerful ,
so-called IDE is shaky at best. As are features such as
color syntax-highlighting and "class browsers".

When you''re stuck with using simple editor such as
Notepad, you''ll appreciate the utility of any naming
conventions.

In the good old days, I did all my Unix programming at
school using nothing but "man" and "pico".



Kami no Itte ga ore ni zettai naru!

Share this post


Link to post
Share on other sites
quote:
Original post by tangentz
The over-reliance of such "mythical", all-powerful ,
so-called IDE is shaky at best. As are features such as
color syntax-highlighting and "class browsers".



Which is why I added:

quote:
Original post by RegentZ
With the addition of well documented code



Which I don''t mean as just comments but also the names of classes/methods/variables.

And most IDE''s can and do support these features. Maybe not notepad but VS.NET + Visual Assist is a joy to work with. For my more enviroment limited work I rely on emacs which can support almost all the useful features of VS.NET

quote:
Original post by tangentz
In the good old days, I did all my Unix programming at
school using nothing but "man" and "pico".



I the good old days I wrote in assembly and machine code, since then I have moved on to use enviroments that are more productive in many ways and hence cheaper to develop with.

Share this post


Link to post
Share on other sites
quote:
Original post by tangentz
When you''re stuck with using simple editor such as
Notepad, you''ll appreciate the utility of any naming
conventions.



And if you''re stuck with no C-compiler, you''ll appreciate the utility of programming directly in hexadecimal. What''s the point of this? If you don''t have the right tools for the job, you''ll have a problem doing the job. We''re way past the days of using compilers with no support applications.



Don''t listen to me. I''ve had too much coffee.

Share this post


Link to post
Share on other sites
i use
m_ for members, and _ for function/methodparameters..

like this:

  
struct Mystruct {
int m_data;
mystruct(int _data) : m_data(_data) {}
};


thats about it. when i code in windowsapi, i use javastyle language. why? solves all the makrodefined functions windows do have..

createWindow is the only way to have a nice named function that does now interfer with the macro CreateWindow ..

i don''t care about pointers as i never use them (except internally as data objects in classes, but normally just in the generic classes that solve some problem, for the rest i don''t use them anymore.. so i don''t have much of memory problems anyways.. *cough..*)

the last thing i use are Delegates, wich are named with on_


  
struct Engine {
Delegate on_idle;
Delegate on_timer;
Timer m_timer;
void quit(unsigned int) { m_running = false; }
void loop() {
m_timer.set_timestep(50); //physics all 50 milliseconds

while(m_running) {
if(m_timer()) on_timer(50);
on_idle(m_timer.fraction());
}
}
};

struct Message {
SingleDelegate on_quit;
void process(unsigned int _msg) {
switch(...) case SOME_ON_QUIT: on_quit(); break;
}
};

void draw(unsigned int _blendfactor) {
//drawing

}

void update(unsigned int _timestep) {
//physics

}

int main() {
Engine e;
Message m;
e.on_idle += makeCallback(&m,&Message::process);
e.on_idle += makeCallback(&draw);
e.on_timer += makeCallback(&update);
m.on_quit = makeCallback(&e,&Engine::quit);
e.loop();
// optional cleanup, done automatically in the end anyways..

e.on_idle -= makeCallback(&m,&Message::process);
e.on_idle -= makeCallback(&draw);
e.on_timer -= makeCallback(&update);
}


thats about what my code looks a like. funny, not? all sweet selfcoded c++..

"take a look around" - limp bizkit
www.google.com

Share this post


Link to post
Share on other sites
quote:
Original post by Sneftel
And if you''re stuck with no C-compiler, you''ll appreciate the utility of programming directly in hexadecimal. What''s the point of this? If you don''t have the right tools for the job, you''ll have a problem doing the job. We''re way past the days of using compilers with no support applications.



Comparing no-IDE programming to machine language programming
is simply absurd and inappropriate. The former is
very realistic. The latter is just insanity .

You''re assuming everyone can shell out $1000+ for the "latest
and greatest" so-called IDE (e.g. VS.NET)...? Get real!

The only tool you need is the compiler. GCC fits the bill
just fine. Anything else is luxury. You''re on shaky grounds
if you over-rely on the "luxuries".

P.S. Sorry for the off-topic post.



Kami no Itte ga ore ni zettai naru!

Share this post


Link to post
Share on other sites
everything else is luxury? there are much free ide''s out, for different os'' and all. and for students visual studio is cheap anyways, so no need to spit out much money. there are ide''s out with wich you can code in any language, you just need the proper compiler.

use what you can. you always have a webaccess when you can post in here. and with the power of web, coding in the notepad is not needed anymore at anytime..

"take a look around" - limp bizkit
www.google.com

Share this post


Link to post
Share on other sites
quote:
Original post by tangentz
You''re assuming everyone can shell out $1000+ for the "latest
and greatest" so-called IDE (e.g. VS.NET)...? Get real!



You don''t need to shell out $1000+, you can get Dev-C++ for the price of a 10 Meg download

Share this post


Link to post
Share on other sites
quote:
Original post by tangentz
You're assuming everyone can shell out $1000+ for the "latest
and greatest" so-called IDE (e.g. VS.NET)...? Get real!



According to many, the "latest and greatest so-called IDE". You'd be surprised how much it'll do for you.

EDIT: whoops, the link works now.



Don't listen to me. I've had too much coffee.

[edited by - sneftel on July 31, 2002 1:13:03 PM]

Share this post


Link to post
Share on other sites