• Create Account

Banner advertising on our site currently available from just \$5!

# Poll: if () with single inner statement. What do you do? What do you do?

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

47 replies to this topic

### Poll: Single-state if's (89 member(s) have cast votes)

#### if () has a single statement. Do you still use braces?

1. Extra work? No way! (12 votes [13.48%])

Percentage of vote: 13.48%

Percentage of vote: 29.21%

Percentage of vote: 57.30%

Vote Guests cannot vote

### #21Vortez  Crossbones+   -  Reputation: 2704

Like
0Likes
Like

Posted 14 November 2011 - 12:51 PM

I answered "Sometime...". That might seem weird, but i got used to write my if and for loop that way:

if(){
...
} else {
....
}

for( ; ; ){
....
}

This save a line or two of code, lol, and it's not hard to understand once you got used to it. I still write functions and switch the "right" way though.

### #22NightCreature83  Crossbones+   -  Reputation: 3287

Like
0Likes
Like

Posted 14 November 2011 - 01:11 PM

I never use braces for single-line statement blocks (unless the required coding style prevents it). My primary rationales for this is that I write the majority of my code on an 11" MacBook Air, so vertical screen real estate is really kind of precious.

<humorous rant>

I would argue that the whole 'lack of braces causes bugs' line of reasoning is baloney. Curly braces (no matter where you put them) are much harder to spot than proper indentation, so I would say that you have all spent too long in curly-brace land, and have come to use them as a crutch, to support poor code formatting and/or code reading skills.

</humorous rant>

You are a python hacker aren't you where indentation level is block structuring
Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, Mad Max

### #23Ravyne  GDNet+   -  Reputation: 9575

Like
2Likes
Like

Posted 14 November 2011 - 01:19 PM

I prefer to leave them out if they're unnecessary. My two reasons are:

1. Vertical white space is premium real-estate, especially on laptops and/or widescreen monitors. My brand new 27" monitor at work is a 1080p screen (with no rotation support) that's not a lot of space, honestly.

2. I use vertical white space to create "code paragraphs" within functions. When quickly scanning source, a brace that's taking up a whole line to itself registers to me
as a blank line.

Also, if the 'if' is executing an assignment, then I use the conditional assignment operator (? -- its more conise, and expresses intent more clearly. I may or may not put unbraced statements on the same line as the conditional depending on a number of factors such as length, and whether similar conditionals follow on subsequent lines (in which case, I often don't separate the 'if' lines at all: one 'if' per line, no white space between).

### #24Bregma  Crossbones+   -  Reputation: 5765

Like
5Likes
Like

Posted 14 November 2011 - 01:50 PM

Folks, I've seen a lot of code. A lot. Over 30 years, many languages, many styles.

And you know, it really just does not matter. It's nice to have consistency within the same source file, but even that doesn't matter too much. Really.

I hear a lot of arguments about cramming as much code as possible onto "tiny" screens, but I'm not swayed. I spent years and years using a 80x24 green screen, cry me a river. I would rather have code that follows centuries-old rules of good typography and composition (good use of whitespace, proper leading, judicious justification, and so on) to make reading code easier, but even that doesn't matter in the long run.

It's just not worth getting fussed over. If it's really important to you that other people follow your True Brace Style, it just indicates you're inexperienced. Go find something productive to do.
Stephen M. Webb
Professional Free Software Developer

### #25Sirisian  Crossbones+   -  Reputation: 1835

Like
0Likes
Like

Posted 14 November 2011 - 02:19 PM

Folks, I've seen a lot of code. A lot. Over 30 years, many languages, many styles.

And you know, it really just does not matter. It's nice to have consistency within the same source file, but even that doesn't matter too much. Really.

Heathen.

Braces always. Single line statements I use the ternary operator if applicable or try to remove the branch.

Hodgman's example could have just used abs though. There's rarely a legitimate use of a single line if and in any case this is always preferable:
if (foo)
{
Foo();
}
Notice the "if (" and not "if( ", important. Also remember tabs for indentation and spaces for alignment.

### #26Serapth  Crossbones+   -  Reputation: 5907

Like
0Likes
Like

Posted 14 November 2011 - 02:23 PM

I almost always do, unless I am slipping in to lazy mode, or on occasion where it makes sense ( like example code that needs to fit within a certain size contraint ).

Then again, I almost always long form everything, so for example, I rarely if ever use "using namespace". Key presses are a cheap trade off for easily groked and maintained code.

My more Popular Tutorial Series:

### #27mhagain  Crossbones+   -  Reputation: 8692

Like
0Likes
Like

Posted 14 November 2011 - 02:28 PM

Braces around single statements is something that I know I should do, but generally don't (unless the statement needs to be broken over more than one line - e.g. a Windows API call that takes a lot of args). I just find the arguments for to be more compelling than the arguments against: protection against spending hours or days chasing down stupid bugs is more than worth any amount of vertical real-estate in my book. But like I said, I somehow always end up not doing it - a bad habit that I need to get out of.

As regards 1TBS - I personally find code written using it to be completely unreadable. Having the opening brace and the closing brace in the same column (Allman/BSD) is an important visual cue for me, moreso than indent levels, and the extra whitespace it gives neatly separates blocks from surrounding code. It's like paragraphs!

Fortunately it's 2011 and we have code formatters these days, so that whole argument is long past it's time to die.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.

### #28mind in a box  Members   -  Reputation: 664

Like
0Likes
Like

Posted 14 November 2011 - 03:24 PM

Every timeI skipped them I had to add them later anyways because I needed to put more into my if-statement.

I've stopped being so lazy now.

### #29swiftcoder  Senior Moderators   -  Reputation: 11739

Like
0Likes
Like

Posted 14 November 2011 - 03:35 PM

Curly braces (no matter where you put them) are much harder to spot than proper indentation

As long as you have a good auto-formatter, yeah. Otherwise you get the
if(condition)
do stuff;
and stuff we added later;
bug.

Even most online code editors can manage that much auto-formatting, and I haven't run into a programmer's text editor that couldn't since maybe Win98? If you do insist on coding in Wordpad, well, you get what you deserve, and have to format it manually

<humorous rant>

you have all spent too long in curly-brace land, and have come to use them as a crutch, to support poor code formatting and/or code reading skills.

</humorous rant>

You are a python hacker aren't you where indentation level is block structuring

Indeed, python is my weapon of choice, despite it's many flaws. But sadly, I still end up writing considerably more code in C++...

Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]

### #30NightCreature83  Crossbones+   -  Reputation: 3287

Like
0Likes
Like

Posted 14 November 2011 - 03:41 PM

Folks, I've seen a lot of code. A lot. Over 30 years, many languages, many styles.

And you know, it really just does not matter. It's nice to have consistency within the same source file, but even that doesn't matter too much. Really.

Heathen.

Braces always. Single line statements I use the ternary operator if applicable or try to remove the branch.

Hodgman's example could have just used abs though. There's rarely a legitimate use of a single line if and in any case this is always preferable:
if (foo)
{
Foo();
}
Notice the "if (" and not "if( ", important. Also remember tabs for indentation and spaces for alignment.

Don't start the tabs vs spaces argument as that will last for ever as well, it is at the same level as these brace strategies. I like spaces everywhere and no tabs but he I have people in my team who swear by tabs and even though our coding standard says spaces instead of tabs once in a while you will still run into a file that has tabs. It doesnt really matter as long as the indentation is correct I don't really care.
Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, Mad Max

### #31mhagain  Crossbones+   -  Reputation: 8692

Like
0Likes
Like

Posted 14 November 2011 - 04:23 PM

Don't start the tabs vs spaces argument as that will last for ever as well, it is at the same level as these brace strategies.

And while we're at it, 8 vs 4 is best avoided too.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.

### #32Serapth  Crossbones+   -  Reputation: 5907

Like
0Likes
Like

Posted 14 November 2011 - 05:28 PM

Don't start the tabs vs spaces argument as that will last for ever as well, it is at the same level as these brace strategies.

And while we're at it, 8 vs 4 is best avoided too.

I mean, seriously, does anyone actually do 8?

My more Popular Tutorial Series:

### #33SiCrane  Moderators   -  Reputation: 9795

Like
0Likes
Like

Posted 14 November 2011 - 05:32 PM

I mean, seriously, does anyone actually do 8?

8 space tabs, yes, unfortunately. They usually use 4 space indents, but use tab for double indentations and end up mixing tabs and spaces. It ends up looking really funky when opened in other people's code editors that use smaller values for tab to space ratios.

### #34DarklyDreaming  Members   -  Reputation: 366

Like
0Likes
Like

Posted 14 November 2011 - 06:18 PM

I also put the braces on the same lines as the if and friends, so spacing is not a problem.

if ( dog > you ) {
run();
}
else {
pet();
}

+1 just for the hilarious statement in the code

[--snip--]
It's just not worth getting fussed over. If it's really important to you that other people follow your True Brace Style, it just indicates you're inexperienced. Go find something productive to do.

Signature worthy.

Oh, and as for me: I go with "whatever works" -- though I seriously hate to see this kind of stuff laying around:
if (x < y)
doSomething();
doSomethingElse();


"I will personally burn everything I've made to the fucking ground if I think I can catch them in the flames."
~ Gabe

"I don't mean to rush you but you are keeping two civilizations waiting!"
~ Cavil, BSG.
"If it's really important to you that other people follow your True Brace Style, it just indicates you're inexperienced. Go find something productive to do."
~ Bregma

"Well, you're not alone.

There's a club for people like that. It's called Everybody and we meet at the bar."

~ Antheus

### #35L. Spiro  Crossbones+   -  Reputation: 16254

Like
1Likes
Like

Posted 14 November 2011 - 06:42 PM

This is starting to go off on a tangent, but the guy(s) who coded my current project used both spaces and tabs, with no logical reasoning between either.

I mean literally this:
if ( blah )	// 2 spaces.
{
doIt();		// 4 spaces.  Okay.
andThat(); // 2 tabs??
}		// 1 tab??

His opening brace uses 2 spaces and his closing brace uses a tab.
Hello?
In my editor tabs are set to 4, so his code is a mangled mess.

I don't care which way you go, but go the same way everywhere.

As for me, I use tabs.
I know the evils of using them but they are not compelling enough due to the hassles of not using them.
Realignment with spaces is very difficult. I align my functions and members so that their names are all at the same level of indentation. If I have only 5 or so of them and add a 6th that needs to be further-out indented, I am willing to adjust the levels of the other functions/members.
This is not easy with spaces.

I also often copy/paste one line to the next and keep the function declarators (inline, int, __stdcall, etc.) and then change just the type of the function (changing int to bool or such).
With spaces the function name moves over one, and goes out of alignment with other functions, and needs to be adjusted.

Basically, there is no good way to maintain consistant alignment with spaces. It either takes too much of your time to make sure everything is aligned consistently, or you give up trying and alignment goes out the door.

Examine:
if ( blood )
run();
My cursor is placed just before the r in run(). I want to go to the end of the previous line and add a brace.
With tabs, that means just hitting <- (left arrow) 3 times.
With spaces, you have to either be clever and alert, or waste time pounding the left arrow key 9 times.
I know I can hit up arrow and then End, etc., but oops I wasn't really thinking this time and I already started hitting the left arrow key. Not going to abort now.

Given that 4 spaces is the standard tab size, and that is what I use, I would rather have an easier time keeping my alignments consistent and using arrow keys to navigate than to conform to a standard whose only argument (if others browse your code AND they have tabs of different sizes) is only hypothetical.
And the worst possible case is fixed simply by telling your friend, "Well, just set your tabs to 4 spaces temporarily".
Not only that, but even if his or her tabs are 8 or 10 or 40, the initial indentation inside functions will still be consistent. Statements will all be at appropriate levels.
Only function names and member names will be poorly aligned.

I will take that trade-off any day.

L. Spiro
It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

### #36taz0010  Members   -  Reputation: 277

Like
1Likes
Like

Posted 15 November 2011 - 07:01 AM

Indentation is such a powerful tool for showing code structure that including the braces is not necessary and simply wasted space. And, as pointed out already, coding IDEs these days will indent automatically, so when you add a second line to a single statement IF without braces, the lack of indentation is an immediate tipoff. I always put the closing brace on it's own line for multiple statement blocks, so any incorrect omission of braces is immediately apparent.

if(condition){
Action();
Action2();
}
AlwaysExecuted();


if(condition)
Action();
Action2();             <-- Oh oh! Where's the closing } ? Better add one now!
AlwaysExecuted();


I don't think I've ever seen a block of code above and not immediately thought "It's missing braces", for the simple reason that it doesn't match my coding style so I can see it's not what I indented to do at first glance.

### #37arbitus  Members   -  Reputation: 436

Like
1Likes
Like

Posted 15 November 2011 - 09:11 AM

I am not even sure I understand the tab vs. space debate, given that any text editor worth coding in allows you to set tabs as spaces, and then you get the benefits of both and none of the drawbacks. Also, why not take 15 minutes to set up formatting rules for a code formatter plugin for your favorite editor so that if you screw up your formatting, you can correct the entire document with a few key strokes? This really seems like an issue that was already resolved with a solution that would work for everyone.

### #38NightCreature83  Crossbones+   -  Reputation: 3287

Like
0Likes
Like

Posted 15 November 2011 - 12:00 PM

I am not even sure I understand the tab vs. space debate, given that any text editor worth coding in allows you to set tabs as spaces, and then you get the benefits of both and none of the drawbacks. Also, why not take 15 minutes to set up formatting rules for a code formatter plugin for your favorite editor so that if you screw up your formatting, you can correct the entire document with a few key strokes? This really seems like an issue that was already resolved with a solution that would work for everyone.

Not every one uses the same editor on a project especially the open source once where you run into emacs vs vi vs edit and the like.
Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, Mad Max

### #39swiftcoder  Senior Moderators   -  Reputation: 11739

Like
1Likes
Like

Posted 15 November 2011 - 12:27 PM

Not every one uses the same editor on a project especially the open source once where you run into emacs vs vi vs edit and the like.

If you have ever used Vim or Emacs, then you know that their auto-formatting and re-formatting tools are both more powerful, and more configurable than pretty much any GUI editor you care to name. Of course, all that power comes with a pretty steep learning curve, but once you're familiar with it...

On the tabs vs. spaces debate, the *only* reason I prefer tabs is because I work with several people who like different indents. I keep my tab width at 2 or 3, one of my co-workers likes 5, and a guy we used to work with likes 8. As long as our code is tab indented, we all get to look at the code exactly as we prefer, and everyone is happy. Yes, it screws up aligned inner-fields, but I really haven't run into many people who obsess over inner-field alignment.

Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]

### #40arbitus  Members   -  Reputation: 436

Like
0Likes
Like

Posted 15 November 2011 - 01:04 PM

Not every one uses the same editor on a project especially the open source once where you run into emacs vs vi vs edit and the like.

Both Emacs and Vim, as far as I can remember, have this capability, as do most of the bare bones open source editors including Notepad++. It is seriously a basic feature missing only from things like MS Notepad, which is fine for simple and quick editing, but I must question the sanity of anyone who uses it as a serious development platform.

On the tabs vs. spaces debate, the *only* reason I prefer tabs is because I work with several people who like different indents. I keep my tab width at 2 or 3, one of my co-workers likes 5, and a guy we used to work with likes 8.

I can accept this argument, and only reply with the idea I prefer consistency that comes with preventing the mixing of tabs and spaces that almost invariably occurs when tabs are allowed. But I am not one for imposing personal preference on team members.

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS