• We should now be fully online following an overnight outage. Apologies for any inconvenience, we do not expect there to be any further issues.

C++ Indentation Poll

Armitage

Banned
Feb 23, 2001
8,086
0
0
How do you indent your C++? I'm working with somebody elses code right now that's all done in a style like option 1 and I find it very hard to read - particularly when its nested.

I always use option 2 almost compulsively. I would change this code, except it makes for a huge mess when you go to look at changes in the revision control. And would likely annoy the original author if I have to send back bug fixes and such.

 

jman19

Lifer
Nov 3, 2000
11,225
664
126
I code the same way Cooler does (though I don't see how this is limited to C++ programming :confused;)
 

fishbits

Senior member
Apr 18, 2005
286
0
0
The way Smilin does. The OP's options stink for me because the ending brace doesn't align with the conditional. But, sooner or later you've got to use someone else's style *shrug*.

My bigger peeve would be use of tabs, which can display and be saved differently in different text editors. Better to just use spaces and keep it consistant, though many would disagree with this too.
 

oog

Golden Member
Feb 14, 2002
1,721
0
0
Same way that Smilin does it, though I think the GNU coding conventions recommends Cooler's way. I've never seen any of the options in the poll.
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
Originally posted by: fishbits
The way Smilin does. The OP's options stink for me because the ending brace doesn't align with the conditional. But, sooner or later you've got to use someone else's style *shrug*.

My bigger peeve would be use of tabs, which can display and be saved differently in different text editors. Better to just use spaces and keep it consistant, though many would disagree with this too.

That's an even bigger peeve - tabs. I always set my editor to insert spaces for tabs.

And yes, my poll options suck. I just put my style and the PITA style of the code I'm working with at the moment. No point in changing it now since so many have voted other though.
 

fs5

Lifer
Jun 10, 2000
11,774
1
0
Option 3, I despise option 4 enough though it's the default for visual studio :frown:
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Option2 is just messed :confused:

I'm option 3, of course :p Except when using Pascal, which has 'begin' and 'end' instead of curly braces. Then 'begin' goes on a seperate line, à la Option2. I once wrote a code style evaluator that subtracted points for methods/functions that were too long, so developers started moving the 'begin' on to the same line as the 'for' to save space. Then I had to implement a metric that dinged 'em double for trying that :p
 

EpsiIon

Platinum Member
Nov 26, 2000
2,351
1
0
Option 3 FTW. It's clean, compact, and makes it easy to see nested structures.

Up until this year, I preferred option 4. I started using 3 a bit (group projects + Eclipse) and then got really frustrated with how much space 4 wasted, especially in simple loops/conditionals. BTW, I only used 3 for loops and conditionals. I still use 4 for classes, methods, and everything else.

How can you not like the compact elegance of:
 

statik213

Golden Member
Oct 31, 2004
1,654
0
0
wow 1 and 2 are really weird.... i'm a 3 -- was surprised to see that 3 was way more popular than 4 (i was expecting a close tie between 3 & 4).
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Originally posted by: Cooler
Here Option that i see in one of my friends code. He was trying to be 1337
Well sure, it makes sense. If you don't have extra whitespace in your code, then the compiler can scan it faster. It then uses that extra time to perform extra optimization and that makes your final code run faster. Plus the compiler can sense how l337 you are and knows you need the extra special optimization. It's like gentoo, but for java.

I'm serious man, I've seen 5-10% improvements by writing all my code on a single line.

Edit: whoops, we're talking about c++ apparently. I've got quite a one-track mind :confused:
 

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
Originally posted by: kamper
I'm serious man, I've seen 5-10% improvements by writing all my code on a single line.

In execution time or compile time??? Or "404 funnay not found"? :p
 

Cooler

Diamond Member
Mar 31, 2005
3,835
0
0
Originally posted by: xtknight
Originally posted by: kamper
I'm serious man, I've seen 5-10% improvements by writing all my code on a single line.

In execution time or compile time??? Or "404 funnay not found"? :p

white space and \n are both one char so it does not matter time wise.
He just does it so others cant read it.
 

itachi

Senior member
Aug 17, 2004
390
0
0
i prefer k&r (3).. mainly because i can see more code on a page than with 4.. but 4 and 2 are by far the easiest to read.
1 is annoying.. the left brace is on a different line than the code block, but the right brace is on the same line. it's unnecessarily difficult to read.
 

Smilin

Diamond Member
Mar 4, 2002
7,357
0
0
Originally posted by: EpsiIon
Option 3 FTW. It's clean, compact, and makes it easy to see nested structures.

Up until this year, I preferred option 4. I started using 3 a bit (group projects + Eclipse) and then got really frustrated with how much space 4 wasted, especially in simple loops/conditionals. BTW, I only used 3 for loops and conditionals. I still use 4 for classes, methods, and everything else.

How can you not like the compact elegance of:

Yeah that's awful.

Truly guys I like the way cooler's way looks. It's very clean and compact and easy on the eye when rolling down the page. The problem is the braces don't align. If your statements get any sort of complexity to them you'll get error prone.

I don't particularly like the "look" of my way but the braces are aligned with each other and the statement itself. "My" method (I obviously didn't invent it) becomes more and more the wise choice as the code gets more and more complicated. That's my other $0.02 :)