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

Started by
46 comments, last by Vectorian 12 years, 5 months ago
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.
Advertisement

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, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

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


throw table_exception("(? ???)? ? ???");

Ah, the old religious argument about The One True Brace Style (see also: tar vs. cpio).

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


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

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

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.

[quote name='swiftcoder' timestamp='1321291378' post='4883842']
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.[/quote]
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 :P


[quote name='swiftcoder' timestamp='1321291378' post='4883842']
<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[/quote]
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. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]


[quote name='Bregma' timestamp='1321300233' post='4883901']
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.
[/quote]




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, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

This topic is closed to new replies.

Advertisement