C++ Coding Style

A place for Ogre users to discuss non-Ogre subjects with friends from the community.
User avatar
Kojack
OGRE Moderator
OGRE Moderator
Posts: 7157
Joined: Sun Jan 25, 2004 7:35 am
Location: Brisbane, Australia
x 535

Re: C++ Coding Style

Post by Kojack »

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.
User avatar
mmixLinus
Silver Sponsor
Silver Sponsor
Posts: 199
Joined: Thu Apr 21, 2011 3:08 pm
Location: Lund, Sweden
x 12

Re: C++ Coding Style

Post by mmixLinus »

areay wrote:From the Ogre Standards
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)
That makes for nice real readable code but they're the most likely for me to ignore due to habit and laziness.
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. :lol:

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
Ie, you want a value power-of-2-multiplied, or bitmask shifted up, and some other bits set. The mistake lies in not realizing that <<1 doesn't have the same precedence as *2.
areay 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
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 variable :oops:

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
User avatar
mmixLinus
Silver Sponsor
Silver Sponsor
Posts: 199
Joined: Thu Apr 21, 2011 3:08 pm
Location: Lund, Sweden
x 12

Re: C++ Coding Style

Post by mmixLinus »

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.
That's interesting - m_ for members but NOT if they are simple types? And no other type info?

(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
User avatar
mmixLinus
Silver Sponsor
Silver Sponsor
Posts: 199
Joined: Thu Apr 21, 2011 3:08 pm
Location: Lund, Sweden
x 12

Re: C++ Coding Style

Post by mmixLinus »

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
iblues1976
Gnome
Posts: 379
Joined: Fri Sep 16, 2011 4:54 pm
x 10

Re: C++ Coding Style

Post by iblues1976 »

mmixLinus wrote:Here's some fun statistics regarding coding conventions on GitHub. Unfortunately no C++
No C++... it would had been nice to see...

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
User avatar
Kojack
OGRE Moderator
OGRE Moderator
Posts: 7157
Joined: Sun Jan 25, 2004 7:35 am
Location: Brisbane, Australia
x 535

Re: C++ Coding Style

Post by Kojack »

mmixLinus wrote:
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.
That's interesting - m_ for members but NOT if they are simple types? And no other type info?

(BTW, Android uses m for members, g for globals, and k for constants :))
A Vector3 class with m_x, m_y and m_z would be very annoying. :)
User avatar
Zonder
Ogre Magi
Posts: 1173
Joined: Mon Aug 04, 2008 7:51 pm
Location: Manchester - England
x 76

Re: C++ Coding Style

Post by Zonder »

mmixLinus wrote:Here's some fun statistics regarding coding conventions on GitHub. Unfortunately no C++
I code like them all but java and php.
There are 10 types of people in the world: Those who understand binary, and those who don't...
iblues1976
Gnome
Posts: 379
Joined: Fri Sep 16, 2011 4:54 pm
x 10

Re: C++ Coding Style

Post by iblues1976 »

what do you mean by member?

member variable, as instance variable?
User avatar
c6burns
Beholder
Posts: 1512
Joined: Fri Feb 22, 2013 4:44 am
Location: Deep behind enemy lines
x 139

Re: C++ Coding Style

Post by c6burns »

iblues1976 wrote:About the "java" style, yes, I guess is the only thing I like about Java
If you hate Java with a passion, don't worry ... you aren't alone :)
User avatar
Kojack
OGRE Moderator
OGRE Moderator
Posts: 7157
Joined: Sun Jan 25, 2004 7:35 am
Location: Brisbane, Australia
x 535

Re: C++ Coding Style

Post by Kojack »

iblues1976 wrote:what do you mean by member?

member variable, as instance variable?
member - variable in a class
method - function in a class
User avatar
mkultra333
Gold Sponsor
Gold Sponsor
Posts: 1894
Joined: Sun Mar 08, 2009 5:25 am
x 116

Re: C++ Coding Style

Post by mkultra333 »

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

Code: Select all

#define METRE	1024

	char m_chMapGenSeed[SEEDSTRINGSIZE] ;

	float flAngle=0.0f ;
	unsigned int uTex=0 ;
	int nCaulkCount=0 ;
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.

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 ;
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.

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) ; }
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.

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
		}
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.
"In theory there is no difference between practice and theory. In practice, there is." - Psychology Textbook.
User avatar
Zonder
Ogre Magi
Posts: 1173
Joined: Mon Aug 04, 2008 7:51 pm
Location: Manchester - England
x 76

Re: C++ Coding Style

Post by Zonder »

mkultra333 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. So

Code: Select all

#define METRE	1024

	char m_chMapGenSeed[SEEDSTRINGSIZE] ;

	float flAngle=0.0f ;
	unsigned int uTex=0 ;
	int nCaulkCount=0 ;
To be accurate that's Systems Hungarian. That's the one people hate ;) but I use systems when I don't have anything defined in the app notation.
There are 10 types of people in the world: Those who understand binary, and those who don't...
User avatar
mkultra333
Gold Sponsor
Gold Sponsor
Posts: 1894
Joined: Sun Mar 08, 2009 5:25 am
x 116

Re: C++ Coding Style

Post by mkultra333 »

To be accurate that's Systems Hungarian. That's the one people hate
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.
"In theory there is no difference between practice and theory. In practice, there is." - Psychology Textbook.
User avatar
mmixLinus
Silver Sponsor
Silver Sponsor
Posts: 199
Joined: Thu Apr 21, 2011 3:08 pm
Location: Lund, Sweden
x 12

Re: C++ Coding Style

Post by mmixLinus »

Kojack wrote:A Vector3 class with m_x, m_y and m_z would be very annoying. :)
Ah, I see, I misunderstood. I tend to agree on that one 8)
Powered by Ogre3D:
MMiX.Me 3D - 3D Music Player
Galaxy Navigator 3D - 2 million stars (ESA's Gaia satellite)
YouTube|Facebook
User avatar
Kojack
OGRE Moderator
OGRE Moderator
Posts: 7157
Joined: Sun Jan 25, 2004 7:35 am
Location: Brisbane, Australia
x 535

Re: C++ Coding Style

Post by Kojack »

Zonder wrote:
mkultra333 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. So

Code: Select all

#define METRE	1024

	char m_chMapGenSeed[SEEDSTRINGSIZE] ;

	float flAngle=0.0f ;
	unsigned int uTex=0 ;
	int nCaulkCount=0 ;
To be accurate that's Systems Hungarian. That's the one people hate ;) but I use systems when I don't have anything defined in the app notation.
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. :)
User avatar
areay
Bugbear
Posts: 819
Joined: Wed May 05, 2010 4:59 am
Location: Auckland, NZ
x 69

Re: C++ Coding Style

Post by areay »

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.
I lol'd. Especially at that last sentence.

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.
User avatar
mmixLinus
Silver Sponsor
Silver Sponsor
Posts: 199
Joined: Thu Apr 21, 2011 3:08 pm
Location: Lund, Sweden
x 12

Re: C++ Coding Style

Post by mmixLinus »

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.
Hehe this is hilarious :lol:
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.
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) :lol:

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
User avatar
mmixLinus
Silver Sponsor
Silver Sponsor
Posts: 199
Joined: Thu Apr 21, 2011 3:08 pm
Location: Lund, Sweden
x 12

Re: C++ Coding Style

Post by mmixLinus »

Kojack wrote: but i love reverse polish notation. it's not c++ related though, i just like an excuse to mention it. :)
he said, whipping out his old HP calculator :D :wink:
Powered by Ogre3D:
MMiX.Me 3D - 3D Music Player
Galaxy Navigator 3D - 2 million stars (ESA's Gaia satellite)
YouTube|Facebook
User avatar
mkultra333
Gold Sponsor
Gold Sponsor
Posts: 1894
Joined: Sun Mar 08, 2009 5:25 am
x 116

Re: C++ Coding Style

Post by mkultra333 »

areay,
We're eagerly awaiting your next alpha release btw.
Had some turmoil last year, set me back, but aiming for a second release in March, exactly one year after the first.

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.
User avatar
Kojack
OGRE Moderator
OGRE Moderator
Posts: 7157
Joined: Sun Jan 25, 2004 7:35 am
Location: Brisbane, Australia
x 535

Re: C++ Coding Style

Post by Kojack »

mmixLinus wrote:
Kojack wrote: but i love reverse polish notation. it's not c++ related though, i just like an excuse to mention it. :)
he said, whipping out his old HP calculator :D :wink:
Image

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(){(([](){})());}
I wonder how that should be formatted? :)
User avatar
Klaim
Old One
Posts: 2565
Joined: Sun Sep 11, 2005 1:04 am
Location: Paris, France
x 56

Re: C++ Coding Style

Post by Klaim »

Wait, soon we should be able to do this:

Code: Select all

auto join(Range{R1} r1, Range{R2} r2) {...}
That's a template function with types checked.

That will allow us even more fun with coding styles! :twisted:


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.
User avatar
Jabberwocky
OGRE Moderator
OGRE Moderator
Posts: 2819
Joined: Mon Mar 05, 2007 11:17 pm
Location: Canada
x 218

Re: C++ Coding Style

Post by Jabberwocky »

bronzebeard wrote:They say you are what you program... and I'm 100% Allman!
:lol:

+1 Allman.
I use a modified Hungarian for vars. Let's call it "Drunken Canadian" notation.
Image
iblues1976
Gnome
Posts: 379
Joined: Fri Sep 16, 2011 4:54 pm
x 10

Re: C++ Coding Style

Post by iblues1976 »

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.
User avatar
Klaim
Old One
Posts: 2565
Joined: Sun Sep 11, 2005 1:04 am
Location: Paris, France
x 56

Re: C++ Coding Style

Post by Klaim »

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. :lol:

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...
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.
However, I think that it's because I don't use any special notation for globals.
User avatar
c6burns
Beholder
Posts: 1512
Joined: Fri Feb 22, 2013 4:44 am
Location: Deep behind enemy lines
x 139

Re: C++ Coding Style

Post by c6burns »

Jabberwocky wrote:I use a modified Hungarian for vars. Let's call it "Drunken Canadian" notation.
LOL +1 for drunken canadian notation