views:

207

answers:

5

My question relates to this previous question, but the solutions offered don't address the problem I have outlined below. After a google search I haven't found any code style guidelines that address the specific problem of long conditionals in an if statement like this.

if( isNull(value1) ||
    isToLong(value1) ||
    hasBadFormat(valule1)){
    doSomething();
}else{
    doSomethingElse();
}

OR:

if( isNull(value1) || isToLong(value1) || hasBadFormat(valule1) ){
    doSomething();
}else{
    doSomethingElse();
}

The problem I have with these two styles is it makes it hard for my eye to find the code in the true block and separate it from the conditionals, or it is too difficult for the eye to determine the correct next line after a long conditional on a single line, especially if the if statement is already indented a few tabs inside a function or other if statements.

Would it be preferable to do something like this:

if(     isNull(value1) ||
     isToLong(value1) ||
     hasBadFormat(valule1)){
    doSomething();
}else{
    doSomethingElse();
}

or would this style be better to indent each new condition in either of these ways:

if( isNull(value1) ||
     isToLong(value1) ||
      hasBadFormat(valule1)){
    doSomething();
}else{
    doSomethingElse();
}

if( isNull(value1) 
     || isToLong(value1) 
      || hasBadFormat(valule1) ){
    doSomething();
}else{
    doSomethingElse();
}

Does anyone have a coding style guideline (maybe a company coding style policy) that addresses this issue in a different or better way than I've proposed? Which one is preferable and can you find any cons or pros to the solutions I've mentioned?

+5  A: 

The obvious solution is to move the open-brace onto the next line, just as God intended!

</flamebait>

Greg
This is the one thing that tipped me into the open-brace-on-its-own--line camp.
DaveE
+7  A: 
if( isNull(value1) ||
    isToLong(value1) ||
    hasBadFormat(valule1))
{
    doSomething();
}
else
{
    doSomethingElse();
}

Now you see the true-block easily I think.

Of course, I prefer:

if( isNull(value1)
    || isToLong(value1)
    || hasBadFormat(valule1))
{
    doSomething();
}
else
{
    doSomethingElse();
}

:-)

Per Erik Stendahl
What if your company coding policy states that you must emulate pure if blocks, so that braces appear on the same line as the if statement? Which of my two solutions would you prefer then and are there any problems with them I haven't recognized?
Jayson
I think my second example would work pretty good when the brace is one the same line.
Per Erik Stendahl
+2  A: 

It really depends on preference and convention of people you work with, but the first two are the two most common forms I have seen. I tend to prefer to move the conditional to multi-lines only if it is so long that it requires left-right scrolling in my ide.

This is how I would write it:

if(isNull(value1) ||    
   isToLong(value1) ||
   hasBadFormat(valule1))
{    
    doSomething();
}
else
{    
    doSomethingElse();
}

Unless that conditional is not long enough to force me to scroll to see it all. If not, I'd do this:

if(isNull(value1) || isToLong(value1) || hasBadFormat(valule1))
{    
    doSomething();
}
else
{    
    doSomethingElse();
}

And in this case, since it seems short enough, I'd do the latter.

Drithyin
+10  A: 

How about something like this?

bool isValid = isNull(value1) || isToLong(value1) || hasBadFormat(valule1);
if( isValid )
{
   doSomething();
}
else
{
   doSomethingElse();
}

The conditional gets moved onto another line, which might make it easier to read.

Soo Wei Tan
This is much more readable IMO.
Fabio Gomes
+1 Just what i was going to answer.
Nifle
Jayson
A: 

I tend to put the operators at the start of the line so they all up.

So, here's one suggestion:

if(isNull(value1)
   || isTooLong(value1)
   || hasBadFormat(valule1))
{
   doSomething();
} /* if */
else
{
   doSomethingElse();
} /* else */

Here's another:

if(0
   || isNull(value1)
   || isTooLong(value1)
   || hasBadFormat(valule1))
/* ...etc... */

(For &&, it would be if(1 && a && b), etc.)

Or this:

if
(
   isNull(value1)
   || isTooLong(value1)
   || hasBadFormat(valule1)
)
/* ...etc... */
Steve Melnikoff