How to indent long conditionals for 'if' statements?

℡╲_俬逩灬. 提交于 2019-12-21 07:03:03

问题


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?


回答1:


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.




回答2:


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();
}

:-)




回答3:


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

</flamebait>




回答4:


This is the alternative I prefer:

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



回答5:


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.




回答6:


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



回答7:


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

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



回答8:


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;     
}


来源:https://stackoverflow.com/questions/1059166/how-to-indent-long-conditionals-for-if-statements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!