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?