16

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?

Community
  • 1
  • 1
Jayson
  • 2,021
  • 3
  • 25
  • 26

8 Answers8

27

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
  • 3,262
  • 2
  • 34
  • 36
  • I had just thought of this one... This seems like a good option too, especially since you could keep the line length down by && or || isValid with the next test on each line. – Jayson Jun 29 '09 at 16:43
  • upvoted but it doesnt seem long enough, how will you format this condition var show = (selectValue == ABOVE && currentDataValue > first) || (selectValue == BELOW && currentDataValue < first) || (selectValue == EQUAL && currentDataValue == first) || (selectValue == BETWEEN && currentDataValue > first && currentDataValue < second) – PirateApp Aug 02 '18 at 05:20
15
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();
}

:-)

  • 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 Jun 29 '09 at 17:04
  • I think my second example would work pretty good when the brace is one the same line. – Per Erik Stendahl Jun 29 '09 at 19:43
10

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

</flamebait>

Greg
  • 316,276
  • 54
  • 369
  • 333
  • This is the one thing that tipped me into the open-brace-on-its-own--line camp. – DaveE Jun 29 '09 at 16:20
  • I actually stumbled upon this answer because it was flagged as low-quality. But I just cannot bring myself to recommending deletion. :) – helmbert May 04 '15 at 18:51
5

This is the alternative I prefer:

if(
    isValid = isNull(value1) ||
    isToLong(value1) ||
    hasBadFormat(valule1)
) {
    doSomething();
}
else {
    doSomethingElse();
}
gztomas
  • 3,030
  • 3
  • 27
  • 38
2

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
  • 63
  • 4
1

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
  • 2,620
  • 1
  • 22
  • 24
1

I personally format all my if statements like this, no matter the length:

if (isNull(value1) || isToLong(value1) || hasBadFormat(value1)) {
    doSomething();
} else {
    doSomethingElse();
}
Ken Mueller
  • 3,659
  • 3
  • 21
  • 33
-3

I'm probably the only one that does it this way. It's called the Horstmann Style but I do it slightly differently.

if (bool)        // comment
{   dothis;        
    andthis;       
} else if (bool) // comment
{   dothis;       
    andthis;        
} else           // comment
{   dothis;        
    andthis;     
}
moooeeeep
  • 31,622
  • 22
  • 98
  • 187