One line if statements

I recently got involved in an argument with a colleague involving a single if line and wanted to see what I thought about what I thought about the stack.

Do you think the expression should be written as:

if(condition)
{
     statement = new assignment;
}

      

OR

if(condition)
    statement=new assignment;

      

please provide a good reason for your decision.

+2


source to share


11 replies


if you really have to use one line if

 if(condition) statement=new assignment;

      



would be better with one line, it should contain one operation.

+21


source


I always use inline curly braces to reduce the risk of someone (myself included) later submitting an error by editing the code around the if statement, regardless of which lines belong as part of the if state.

EDIT:

Here's a live example in case I just accidentally ended up in some old code:



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

      

Note that both if statements are in the main block of code, but poor formatting makes them appear to be nested at first glance. I'm sure any "good" programmer should quickly see what's going on, but why cause unnecessary confusion?

+13


source


I've always been a fan of braces. If anyone had to change the oneline statement, if so:

if(condition) statement=new assignment;

      

to

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

      

You will not get the expected behavior.

Using curly braces pretty much ensures that if someone changes the if statement, they are sure to put the correct statements in the right place.

+4


source


It really depends on your group's coding style. The group must have consistent coding standards. For my current group, we always use:

if (condition) {
  statement = new assignment;
}

      

We do this to prevent errors caused by forgetting parentheses after the if statement, for example:

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

      

Another group I worked with until recently have always used this syntax for single-line if statements:

if (condition)
    statement1;

      

I personally don't like it because it is less explicit; but the most important thing is to adhere to a standard coding standard for your team or project, so that the code you write looks like the code that your employees write and is just as easy to read for everyone in the group.

Your IDE's or framework's conventions can provide a good foundation for your coding standards, and can even be tailored to your team style.

+4


source


I always make one line if

sans-brackets statements. The presence of the parentheses indicates (syntactically correct) that "oh, I can do something else here ..." and I don't like the temptation. Anything involving multiple statements should be split across multiple lines with matching parentheses.

+1


source


if (condition)
{
    statement = new assignment;
}

      

is what I would write. Namely because I like neat code that saves reading / editing / understanding time.

In very few cases, I only make the exception okay when I quickly and dirty code something for debugging, etc.

A single line operation is always very easily damaged by how the semicolon is placed.

+1


source


I always use closed brackets and I never code a single line ifs, my approach looks like

if(condition) { 
    statement = new assignment; 
}

      

because I am coding Java and this is a convention for the language. Check:

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

Note: if statements always use curly braces {}. Avoid the following mistakes: form:

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

      

The use of parentheses prevents errors: some others may add later new clauses that can be met if the condition and forgetting of the parentheses

+1


source


I would go without parentheses.

The only reason you need parentheses is to have multiple statements within a block.

Sounds like a waste of argument.

0


source


if(condition)
    statement=new assignment;

      

or

if(condition) statement=new assignment;

      

0


source


Generally, I hate one-line ifs except for this Perl case

operation if condition;

      

0


source


I have an automatic format setting to kill your one-liner that puts it on two lines. Thus, it needs parentheses.

0


source







All Articles