tags:

views:

314

answers:

11

I was recently involved in an argument with a coworker involving one line if statements and wanted to see what stackoverflow thought.

Do you feel that the statement should be written as:

if(condition)
{
     statement = new assignment;
}

OR

if(condition)
    statement=new assignment;

please provide a good reason for your decision.

+12  A: 

if you should really use one line if

 if(condition) statement=new assignment;

will be better since its one line, it should contain one operation.

Cem Kalyoncu
+1, because basically, if you need braces, you don't write it in one line.
Michael Krelin - hacker
+7  A: 

I've always been a fan of braces. If someone were to modify a oneline if statement like so:

if(condition) statement=new assignment;

to

if(condition)
statement = new assignment;
another statement;

You won't get the expected behavior.

Using the braces pretty much insures that if someone modifies an if statement, they'll make sure to put the right statements in the right place.

Alan
If somebody does that, they obviously don't understand the language. Doesn't this fall under basic competence? (sincerity intended)
John MacIntyre
Understanding the language and making typo's have nothing to do with one another. Adding braces *ensures* that the correct behavior happens regardless.
Alan
Maybe. Could be a junior programmer maintaining code well after it's first written. Could also be a senior programmer having a bad day. Why create an opportunity for someone to make a mistake?
Eric J.
So you suggest littering the code with many unnecessary braces.
Cem Kalyoncu
Eh, that depends on what the expected behavior was. If you have the first clue how your language works, you should expect the first statement to be conditionally executed and the second unconditionally.
Chuck
Yes, because we should expect that *EVERYONE* who touches our code will have a clue.
Alan
@Alan: What if somebody sticks the next line outside of the braces but expects it to still be conditionally executed?
Chuck
Then that person has introduced a bug.
Alan
@Alan - We can't idiot-proof our code. Anyone who doesn't know this kind of basic language behavior needs to learn the language, and fast. They obviously don't know it, and shouldn't be playing around with your code, and any bugs they introduce can a) be easily fixed by someone competent, and b) potentially teach them something that they apparently need to learn the hard way. I use the one-liner style, and have never fallen prey to this particular bug. It's such a widespread and widely warned against "gotcha" that no one falls for it anymore.
Chris Lutz
Defensive programming is like defensive driving. You don't need it until you're careening off a cliff due to someone else's mistake.
jskaggz
@Chris: you can't make your code idiot-proof, but you *can* make it idiot resistant. We're not talking about spending hours and hours of work here. Most IDE's will auto complete braces for you, so adding braces after and if() statement requires at most one extra keystroke. Clearly braces is going to be group/personal preference, however the justification for using braces is sound: it prevents a relatively simple error.
Alan
A: 

I always do one-line if statements sans-brackets. The presence of brackets indicates (syntactically correctly) that "oh, I can do something else in here..." and I don't like the temptation. Anything that involves more than one statement should be broken up into multiple lines with proper brackets.

Adam Robinson
+9  A: 

I always use enclosing braces to reduce the risk that someone (including myself) will later introduce a bug by editing the code around the if statement without paying careful attention to which line(s) belong as part of the if-condition.

EDIT:

Here's a live example if this I just happened to come across in some old code:

if (form.validateUpload (messages, this))
    return getErrorOutcome (ctx, messages);
    if (LOG.isInfoEnabled ())
        LOG.info ("CREATING UPLOAD");

Notice how both "if" statements are in the main block of code but due to poor formatting, at first glance they appear to be nested. Sure any "good" programmer should quickly see what's happening, but why cause any unnecessary confusion?

Eric J.
A: 

I would go without the brackets.

The only reason you would need the brackets is if you had multiple statements inside the block.

Sounds like a waste of an argument though.

BLeB
A: 
if(condition)
    statement=new assignment;

or

if(condition) statement=new assignment;
John MacIntyre
A: 

As a rule, I abhor one-line ifs except in this Perl case

operation if condition;
Paul Nathan
As with JuanZe, this is not an answer. The question was about *how* to do one-liners, not if he should.
Adam Robinson
+3  A: 

This really depends on the coding style of your group. The group should have consistent coding standards. For my current group we always use:

if (condition) {
  statement = new assignment;
}

We do this to prevent mistakes caused by forgetting the braces after the if statement, such as:

if (condition)
   statement1;
   statement2; 
//statement2 is not part of the if statement, but it looks like it because of wrong indentation

Another group that I worked with until just recently always used this syntax for one-line if statements:

if (condition)
    statement1;

Personally I don't like this as much because it's less explicit; but the most important thing is to stick to a consistent coding standard for your group or project, so that code you write looks like code your co-workers write, and is just as easy to read for everyone in the group.

The conventions of your IDE or environment can provide a good basis for your coding standards, and may even be tailored to your group's style.

RMorrisey
+1 Coding standards
Stephen C
A: 

I have auto-format setup to kill your one-liner, which puts it on two lines. As such, it needs braces.

Stefan Kendall
I'm curious why so many people are replying with irrelevant answers. Again, the question is about *how* to form one-liners, not *if he should*.
Adam Robinson
It's not irrelevant. There IS no one-line in my format-controlled source repository. It simply cannot exist, and therefore worrying about the problem is irrelevant.
Stefan Kendall
Furthermore, the OP asked preference. I gave mine. It's your COMMENT that's irrelevant, as you clearly didn't read the question.
Stefan Kendall
+1  A: 
if (condition)
{
    statement = new assignment;
}

is what I would write. Namely because I like tidy code which saves time to read/edit/understand.

In very few cases I'd make an exception normally only when I'm quick and dirty coding something for debugging etc.

A one line if statement is always very easily corrupted by how the semicolon is placed.

capfu
+1  A: 

I always use enclosing brackets and I never code one line ifs, my approach looks like this

if(condition) { 
    statement = new assignment; 
}

because I code Java and that's the convention for the language. Check:

http://java.sun.com/docs/codeconv/html/CodeConventions.doc6.html#449

Note: if statements always use braces {}. Avoid the following error-prone form:

if (condition) //AVOID! THIS OMITS THE BRACES {}!
    statement;

The use of brackets prevent bugs: some else could add later new sentences that are suposed to be executed if the condition and forgetting the brackets

JuanZe
Not to be snarky, but this is entirely irrelevant. The question is about what to do when writing a one-line if statement, not about whether or not you use them.
Adam Robinson