C++ Coding Style
-
- OGRE Moderator
- Posts: 7157
- Joined: Sun Jan 25, 2004 7:35 am
- Location: Brisbane, Australia
- x 535
Re: C++ Coding Style
I use m_ for members (but usually not for pod-ish ones like a vector3), g_ for globals and c_ for constants. But that's it. No other type info.
I use pascal case for class names (ExampleName) and camel case for method and variable names (exampleName), same as ogre.
I use pascal case for class names (ExampleName) and camel case for method and variable names (exampleName), same as ogre.
-
- Silver Sponsor
- Posts: 199
- Joined: Thu Apr 21, 2011 3:08 pm
- Location: Lund, Sweden
- x 12
Re: C++ Coding Style
Ouch! Hehe, no, #4 doesn't make for nice readable code. Actually it only does what it says: make the operator precedence unambiguous. If used to full extent it makes operator precedence irrelevant.areay wrote:From the Ogre StandardsThat makes for nice real readable code but they're the most likely for me to ignore due to habit and laziness.3.Always insert spaces in between operators and operands (x + y, not x+y)
4.Use parenthesis to make the operator precedence unambiguous, even when it is not required ((x * y) + 1, not x * y + 1)

I try to use whitespace (both horizontal and vertical) to beautify my code (to group according to precedence, make it more readable, to group blocks logically etc). Not everywhere though

BTW, here's a classic precedence error that needs parentheses:
Code: Select all
unsigned int uShifted = uValue<<16 + 1; // wrong
Well, I regularly use both the small-cap-initial-camel-case "doSomething()" and the (perverted) Hungarian notation "bCheckVar" you mention. "doSomething()" is a Java thing, so I use it when Java coding. I say perverted because it is not how it was initially intended to be. I've been thinking of giving the original Hungarian a try.. but I'll probably never get around to it. Too much of a barrier to start thinking differently regarding the "semantical type" of a variableareay wrote:As far as variable and functions naming, I try not use underscore... unless I have to... it looks better to use doSomething than do_something()
also, while I have done it, because maybe the code already had it, I don't like the variable notation defining the type like
mSomeVar
or
bCheckVar (b boolean)
in other words, Hungarian notation

Anyone here using Apps Hungarian variable naming?
Powered by Ogre3D:
MMiX.Me 3D - 3D Music Player
Galaxy Navigator 3D - 2 million stars (ESA's Gaia satellite)
YouTube|Facebook
MMiX.Me 3D - 3D Music Player
Galaxy Navigator 3D - 2 million stars (ESA's Gaia satellite)
YouTube|Facebook
-
- Silver Sponsor
- Posts: 199
- Joined: Thu Apr 21, 2011 3:08 pm
- Location: Lund, Sweden
- x 12
Re: C++ Coding Style
That's interesting - m_ for members but NOT if they are simple types? And no other type info?Kojack wrote:I use m_ for members (but usually not for pod-ish ones like a vector3), g_ for globals and c_ for constants. But that's it. No other type info.
I use pascal case for class names (ExampleName) and camel case for method and variable names (exampleName), same as ogre.
(BTW, Android uses m for members, g for globals, and k for constants

Powered by Ogre3D:
MMiX.Me 3D - 3D Music Player
Galaxy Navigator 3D - 2 million stars (ESA's Gaia satellite)
YouTube|Facebook
MMiX.Me 3D - 3D Music Player
Galaxy Navigator 3D - 2 million stars (ESA's Gaia satellite)
YouTube|Facebook
-
- Silver Sponsor
- Posts: 199
- Joined: Thu Apr 21, 2011 3:08 pm
- Location: Lund, Sweden
- x 12
Re: C++ Coding Style
Here's some fun statistics regarding coding conventions on GitHub. Unfortunately no C++
Powered by Ogre3D:
MMiX.Me 3D - 3D Music Player
Galaxy Navigator 3D - 2 million stars (ESA's Gaia satellite)
YouTube|Facebook
MMiX.Me 3D - 3D Music Player
Galaxy Navigator 3D - 2 million stars (ESA's Gaia satellite)
YouTube|Facebook
-
- Gnome
- Posts: 379
- Joined: Fri Sep 16, 2011 4:54 pm
- x 10
Re: C++ Coding Style
No C++... it would had been nice to see...mmixLinus wrote:Here's some fun statistics regarding coding conventions on GitHub. Unfortunately no C++
About the "java" style, yes, I guess is the only thing I like about Java
doSomething()
but sometimes I forget and I use
DoSomething()... I think someone called Pascal style?
Microsoft tends to use that a loot...
for constanst, I like
UPPERCASE
ZERO
but it is true, that we can have a pre-defined macro
#define ZERO 0
or
const int ZERO =0;
so, I can see why some people will use
c for constant
I have used g for gloval variables, if the code had it already...
I also tend to use g for global variables when doing Win32 programming...
I have to admit, while off-topic, I'm a WINAPI LOVER
-
- OGRE Moderator
- Posts: 7157
- Joined: Sun Jan 25, 2004 7:35 am
- Location: Brisbane, Australia
- x 535
Re: C++ Coding Style
A Vector3 class with m_x, m_y and m_z would be very annoying.mmixLinus wrote:That's interesting - m_ for members but NOT if they are simple types? And no other type info?Kojack wrote:I use m_ for members (but usually not for pod-ish ones like a vector3), g_ for globals and c_ for constants. But that's it. No other type info.
I use pascal case for class names (ExampleName) and camel case for method and variable names (exampleName), same as ogre.
(BTW, Android uses m for members, g for globals, and k for constants)

-
- Ogre Magi
- Posts: 1173
- Joined: Mon Aug 04, 2008 7:51 pm
- Location: Manchester - England
- x 76
Re: C++ Coding Style
I code like them all but java and php.mmixLinus wrote:Here's some fun statistics regarding coding conventions on GitHub. Unfortunately no C++
There are 10 types of people in the world: Those who understand binary, and those who don't...
-
- Gnome
- Posts: 379
- Joined: Fri Sep 16, 2011 4:54 pm
- x 10
Re: C++ Coding Style
what do you mean by member?
member variable, as instance variable?
member variable, as instance variable?
-
- Beholder
- Posts: 1512
- Joined: Fri Feb 22, 2013 4:44 am
- Location: Deep behind enemy lines
- x 139
Re: C++ Coding Style
If you hate Java with a passion, don't worry ... you aren't aloneiblues1976 wrote:About the "java" style, yes, I guess is the only thing I like about Java

-
- OGRE Moderator
- Posts: 7157
- Joined: Sun Jan 25, 2004 7:35 am
- Location: Brisbane, Australia
- x 535
Re: C++ Coding Style
member - variable in a classiblues1976 wrote:what do you mean by member?
member variable, as instance variable?
method - function in a class
-
- Gold Sponsor
- Posts: 1894
- Joined: Sun Mar 08, 2009 5:25 am
- x 116
Re: C++ Coding Style
Since we're going deeper into personal style...
I use Hungarian notation, just because it's what was the style when I did a few mickey mouse programming courses. If it's some non-standard class or struct, I skip the hungarian. I use m_ for class globals. I use capitalized first word style. And I use all caps for constants and defines. So
I like my code to be condensed, all squeezed together, so I can see more of it on the page. I use a tab of only 2 spaces. I only put spaces around things if it makes the layout neater or maybe clarifies an equation.
I skip backets on an if statement if it's just a single action. I will often put it on the same line. Sometimes I put bracketed stuff on the same line if it's something repeated, since it looks neater and simpler to me and keeps everything squeezed together. I do the same for some types of repetative code, where it's really not important and just a waste of space to put everything on a new line.
However most the time I'll do a standard, Allman style bracketing around if statements. And if the statement has lots of clauses, I'll often put them on new lines.
My style probably looks pretty messy and cluttered to most programmers. I'm pretty much a classic "cowboy" programmer, so I haven't needed to fit in other people's styles, just going with what suits me. I admit my code kinda starts at the level of "ball of mud" and gets worse from there. Architecture and style are things that happen to other people.
But I do use a lot of comments, just to help myself when I come back to code I wrote months or years ago, which happens daily since I've been working on the same project for almost 5 years now.
I use Hungarian notation, just because it's what was the style when I did a few mickey mouse programming courses. If it's some non-standard class or struct, I skip the hungarian. I use m_ for class globals. I use capitalized first word style. And I use all caps for constants and defines. So
Code: Select all
#define METRE 1024
char m_chMapGenSeed[SEEDSTRINGSIZE] ;
float flAngle=0.0f ;
unsigned int uTex=0 ;
int nCaulkCount=0 ;
Code: Select all
flAngle=(float)nVert/(float)nSeg*TWOPI ;
flPosX=m_EntityStore.Position[CENTRE].XPos ;
flPosY=m_EntityStore.Position[CENTRE].YPos + m_EntityStore.VarVal[ENTITY_INFO_LIGHT_BRIGHTNESS]*sin(flAngle)*flScale ;
flPosZ=m_EntityStore.Position[CENTRE].ZPos + m_EntityStore.VarVal[ENTITY_INFO_LIGHT_BRIGHTNESS]*cos(flAngle)*flScale ;
Code: Select all
nTex=ARCH_L ; strcpy(chTex, "textures/archatron/Arch_XNeg.bmp") ; pArch->SetTextureName(nTex, chTex) ; if( !CreateTexture(nTex, chTex, true) ) return E_FAIL;
nTex=ARCH_R ; strcpy(chTex, "textures/archatron/Arch_XPos.bmp") ; pArch->SetTextureName(nTex, chTex) ; if( !CreateTexture(nTex, chTex, true) ) return E_FAIL;
nTex=ARCH_F ; strcpy(chTex, "textures/archatron/Arch_ZNeg.bmp") ; pArch->SetTextureName(nTex, chTex) ; if( !CreateTexture(nTex, chTex, true) ) return E_FAIL;
nTex=ARCH_B ; strcpy(chTex, "textures/archatron/Arch_ZPos.bmp") ; pArch->SetTextureName(nTex, chTex) ; if( !CreateTexture(nTex, chTex, true) ) return E_FAIL;
nTex=ARCH_U ; strcpy(chTex, "textures/archatron/Arch_YPos.bmp") ; pArch->SetTextureName(nTex, chTex) ; if( !CreateTexture(nTex, chTex, true) ) return E_FAIL;
nTex=ARCH_D ; strcpy(chTex, "textures/archatron/Arch_YNeg.bmp") ; pArch->SetTextureName(nTex, chTex) ; if( !CreateTexture(nTex, chTex, true) ) return E_FAIL;
// code from elsewhere in the project
if(nKind==P_PATCH_CYLINDER) { sprintf(chT, " CreatePrefab_P_PATCH_CYLINDER(0, -1) ; ") ; strcat(chD, chT) ; strcat(chD, CRLF) ; }
if(nKind==P_MONSTER_BZN) { sprintf(chT, " CreatePrefab_P_MONSTER_BZN(0, -1) ; ") ; strcat(chD, chT) ; strcat(chD, CRLF) ; }
if(nKind==P_WEAPON_BZN) { sprintf(chT, " CreatePrefab_P_WEAPON_BZN(0, -1) ; ") ; strcat(chD, chT) ; strcat(chD, CRLF) ; }
if(nKind==P_AMMO_BZN) { sprintf(chT, " CreatePrefab_P_AMMO_BZN(0, -1) ; ") ; strcat(chD, chT) ; strcat(chD, CRLF) ; }
if(nKind==P_ITEM) { sprintf(chT, " CreatePrefab_P_ITEM(0, -1) ; ") ; strcat(chD, chT) ; strcat(chD, CRLF) ; }
if(nKind==P_FURNITURE) { sprintf(chT, " CreatePrefab_P_FURNITURE(0, -1) ; ") ; strcat(chD, chT) ; strcat(chD, CRLF) ; }
Code: Select all
// X overlap for NS portals
if(
((nPortalDir[0]==MAZEDIR_N) && (nPortalDir[1]==MAZEDIR_S))
||
((nPortalDir[0]==MAZEDIR_S) && (nPortalDir[1]==MAZEDIR_N))
)
{
// do stuff
}
But I do use a lot of comments, just to help myself when I come back to code I wrote months or years ago, which happens daily since I've been working on the same project for almost 5 years now.
"In theory there is no difference between practice and theory. In practice, there is." - Psychology Textbook.
-
- Ogre Magi
- Posts: 1173
- Joined: Mon Aug 04, 2008 7:51 pm
- Location: Manchester - England
- x 76
Re: C++ Coding Style
To be accurate that's Systems Hungarian. That's the one people hatemkultra333 wrote:Since we're going deeper into personal style...
I use Hungarian notation, just because it's what was the style when I did a few mickey mouse programming courses. If it's some non-standard class or struct, I skip the hungarian. I use m_ for class globals. I use capitalized first word style. And I use all caps for constants and defines. SoCode: Select all
#define METRE 1024 char m_chMapGenSeed[SEEDSTRINGSIZE] ; float flAngle=0.0f ; unsigned int uTex=0 ; int nCaulkCount=0 ;

There are 10 types of people in the world: Those who understand binary, and those who don't...
-
- Gold Sponsor
- Posts: 1894
- Joined: Sun Mar 08, 2009 5:25 am
- x 116
Re: C++ Coding Style
Yeah, so I hear. I think it does help me a little, to constantly keep variable types directly in mind as I use them, but probably not much. I wouldn't miss it if I stopped using it, but for now it's a habit.To be accurate that's Systems Hungarian. That's the one people hate
"In theory there is no difference between practice and theory. In practice, there is." - Psychology Textbook.
-
- Silver Sponsor
- Posts: 199
- Joined: Thu Apr 21, 2011 3:08 pm
- Location: Lund, Sweden
- x 12
Re: C++ Coding Style
Ah, I see, I misunderstood. I tend to agree on that oneKojack wrote:A Vector3 class with m_x, m_y and m_z would be very annoying.

Powered by Ogre3D:
MMiX.Me 3D - 3D Music Player
Galaxy Navigator 3D - 2 million stars (ESA's Gaia satellite)
YouTube|Facebook
MMiX.Me 3D - 3D Music Player
Galaxy Navigator 3D - 2 million stars (ESA's Gaia satellite)
YouTube|Facebook
-
- OGRE Moderator
- Posts: 7157
- Joined: Sun Jan 25, 2004 7:35 am
- Location: Brisbane, Australia
- x 535
Re: C++ Coding Style
i don't like (system) hungarian notation, but i love reverse polish notation. it's not c++ related though, i just like an excuse to mention it.Zonder wrote:To be accurate that's Systems Hungarian. That's the one people hatemkultra333 wrote:Since we're going deeper into personal style...
I use Hungarian notation, just because it's what was the style when I did a few mickey mouse programming courses. If it's some non-standard class or struct, I skip the hungarian. I use m_ for class globals. I use capitalized first word style. And I use all caps for constants and defines. SoCode: Select all
#define METRE 1024 char m_chMapGenSeed[SEEDSTRINGSIZE] ; float flAngle=0.0f ; unsigned int uTex=0 ; int nCaulkCount=0 ;
but I use systems when I don't have anything defined in the app notation.

-
- Bugbear
- Posts: 819
- Joined: Wed May 05, 2010 4:59 am
- Location: Auckland, NZ
- x 69
Re: C++ Coding Style
I lol'd. Especially at that last sentence.mkultra333 wrote: My style probably looks pretty messy and cluttered to most programmers. I'm pretty much a classic "cowboy" programmer, so I haven't needed to fit in other people's styles, just going with what suits me. I admit my code kinda starts at the level of "ball of mud" and gets worse from there. Architecture and style are things that happen to other people.
We're eagerly awaiting your next alpha release btw.
Last edited by areay on Fri Jan 24, 2014 10:31 am, edited 1 time in total.
-
- Silver Sponsor
- Posts: 199
- Joined: Thu Apr 21, 2011 3:08 pm
- Location: Lund, Sweden
- x 12
Re: C++ Coding Style
Hehe this is hilariousmkultra333 wrote:My style probably looks pretty messy and cluttered to most programmers. I'm pretty much a classic "cowboy" programmer, so I haven't needed to fit in other people's styles, just going with what suits me. I admit my code kinda starts at the level of "ball of mud" and gets worse from there. Architecture and style are things that happen to other people.

Yeah, writing to my future self is something I try to do too. It's humbling in a comical way, to realize that "I am clever enough to write this dazzling piece of code now, but not clever enough to understand it quickly or with ease in the future, which probably means that I am not clever enough to write this piece of code now, in a way that ensures I understand it in the future" (or thereabouts)mkultra333 wrote:But I do use a lot of comments, just to help myself when I come back to code I wrote months or years ago, which happens daily since I've been working on the same project for almost 5 years now.

More fun stuff. Over at SO
Powered by Ogre3D:
MMiX.Me 3D - 3D Music Player
Galaxy Navigator 3D - 2 million stars (ESA's Gaia satellite)
YouTube|Facebook
MMiX.Me 3D - 3D Music Player
Galaxy Navigator 3D - 2 million stars (ESA's Gaia satellite)
YouTube|Facebook
-
- Silver Sponsor
- Posts: 199
- Joined: Thu Apr 21, 2011 3:08 pm
- Location: Lund, Sweden
- x 12
Re: C++ Coding Style
he said, whipping out his old HP calculatorKojack wrote: but i love reverse polish notation. it's not c++ related though, i just like an excuse to mention it.


Powered by Ogre3D:
MMiX.Me 3D - 3D Music Player
Galaxy Navigator 3D - 2 million stars (ESA's Gaia satellite)
YouTube|Facebook
MMiX.Me 3D - 3D Music Player
Galaxy Navigator 3D - 2 million stars (ESA's Gaia satellite)
YouTube|Facebook
-
- Gold Sponsor
- Posts: 1894
- Joined: Sun Mar 08, 2009 5:25 am
- x 116
Re: C++ Coding Style
areay,
mmixLinus, nice comments. I particularly liked the one that started "/// Class used to work around Richard being a f..."
Had some turmoil last year, set me back, but aiming for a second release in March, exactly one year after the first.We're eagerly awaiting your next alpha release btw.
mmixLinus, nice comments. I particularly liked the one that started "/// Class used to work around Richard being a f..."
"In theory there is no difference between practice and theory. In practice, there is." - Psychology Textbook.
-
- OGRE Moderator
- Posts: 7157
- Joined: Sun Jan 25, 2004 7:35 am
- Location: Brisbane, Australia
- x 535
Re: C++ Coding Style
mmixLinus wrote:he said, whipping out his old HP calculatorKojack wrote: but i love reverse polish notation. it's not c++ related though, i just like an excuse to mention it.![]()

I was just marking maths exams with it a few weeks ago. I also have an emulator on my phone, but touch screen is no match for the physical feel and muscle memory use of a serious calculator.

If C++ had a whitespace operator (oh how I wish it had a whitespace operator!) I could implement some nice rpn within the language. I could do it with commas, but that's not as nice.
I'm surprised nobody has mentioned the good old "int *p;" vs "int* p;" issue yet.

Thanks to C++11 we can now do this:
Code: Select all
int main(){(([](){})());}

-
- Old One
- Posts: 2565
- Joined: Sun Sep 11, 2005 1:04 am
- Location: Paris, France
- x 56
Re: C++ Coding Style
Wait, soon we should be able to do this:
That's a template function with types checked.
That will allow us even more fun with coding styles!
BTW I also prefer my old programmable calculator, indeed feels better when smashing the keys. If not available, but have internet, I just type the calculus in Google these days.
Code: Select all
auto join(Range{R1} r1, Range{R2} r2) {...}
That will allow us even more fun with coding styles!

BTW I also prefer my old programmable calculator, indeed feels better when smashing the keys. If not available, but have internet, I just type the calculus in Google these days.
-
- OGRE Moderator
- Posts: 2819
- Joined: Mon Mar 05, 2007 11:17 pm
- Location: Canada
- x 218
Re: C++ Coding Style
bronzebeard wrote:They say you are what you program... and I'm 100% Allman!

+1 Allman.
I use a modified Hungarian for vars. Let's call it "Drunken Canadian" notation.

-
- Gnome
- Posts: 379
- Joined: Fri Sep 16, 2011 4:54 pm
- x 10
Re: C++ Coding Style
I never understood reverse polish notation, but since this is going in a tangent, I must mention my profound love for my TI-89 calculator... and then my upgraded TI-89 Titanium... which of course, my niece has it now for her university... which probably she is not using and she doesn't love it like I do.
going back to C++, I particularly don't like to add a "m" for member ,which is a class variable (instance variable), but I can see why is use... to make difference between local variables... but how about just using "this" keyword ... this-> ... if one wants to make a difference...
Like I said, there is nothing wrong with different styles... Before I study Computer Science, I remember taking some programming and computer classes in a community college (vb, c, c++, as400, novell -- really??? ). I remember the vb class ( Visual Basic 4) the instructor like to name the GUI objects with
txt (for text box) , chk (for check box) , frm (for form) and so forth... which wasn't his idea, I think it was just a Microsoft standard or came from some Microsoft book.
Let me just said that the C++ class I took was horrible... but I knew it wasn't C++, but the instructor! I was lucky, not to blame C++...
Anyways... now seeing this, I think one day I should commit to one style... I'm all over the place...
and I should write it down, so I don't forget.
going back to C++, I particularly don't like to add a "m" for member ,which is a class variable (instance variable), but I can see why is use... to make difference between local variables... but how about just using "this" keyword ... this-> ... if one wants to make a difference...
Like I said, there is nothing wrong with different styles... Before I study Computer Science, I remember taking some programming and computer classes in a community college (vb, c, c++, as400, novell -- really??? ). I remember the vb class ( Visual Basic 4) the instructor like to name the GUI objects with
txt (for text box) , chk (for check box) , frm (for form) and so forth... which wasn't his idea, I think it was just a Microsoft standard or came from some Microsoft book.
Let me just said that the C++ class I took was horrible... but I knew it wasn't C++, but the instructor! I was lucky, not to blame C++...
Anyways... now seeing this, I think one day I should commit to one style... I'm all over the place...
and I should write it down, so I don't forget.
-
- Old One
- Posts: 2565
- Joined: Sun Sep 11, 2005 1:04 am
- Location: Paris, France
- x 56
Re: C++ Coding Style
I use a modified Hungarian for vars. Let's call it "Drunken Canadian" notation.
I wonder if a "ostrich head in a hole" style exists.

I tried to go without m_ for some time but always failed most of the time because of ambiguities that can happen in implementation of functions.going back to C++, I particularly don't like to add a "m" for member ,which is a class variable (instance variable), but I can see why is use... to make difference between local variables... but how about just using "this" keyword ... this-> ... if one wants to make a difference...
However, I think that it's because I don't use any special notation for globals.
-
- Beholder
- Posts: 1512
- Joined: Fri Feb 22, 2013 4:44 am
- Location: Deep behind enemy lines
- x 139
Re: C++ Coding Style
LOL +1 for drunken canadian notationJabberwocky wrote:I use a modified Hungarian for vars. Let's call it "Drunken Canadian" notation.