In our project there are deep and lengthy if-else structures which can be significantly shortened by a simple rearrangement. To me the shorter version would be easier to understand also.
As an example, this:
if(a) {
if(b) {
doSomething1;
}
else {
doSomething2;
}
}
else {
if(b) {
doSomething1;
}
else {
if(c) {
doSomething3;
}
else {
doSomething2;
}
}
}
could be refactored to:
if(b) {
doSomething1;
}
else {
if(!a && c) {
doSomething3;
}
else {
doSomething2;
}
}
Is it worth to do this kind of rearrangements?
Is there a name for this kind of "rearranging" if else structures to shorter form?
I am looking for arguments to convince colleges that it is worth to take the effort to do the changes. Or - of course - arguments against it, to convince me that it is not worth to do this.
I think I can agree with Ewan that it's probably not worth to change working code for this.
However if it's new code or we are doing a major refactor (and proper re-testing of every execution path is guaranteed) then I would shorten it like above, OR -- especially if there is no way to significantly shorten the structure -- go for the "self documenting logic table" solution pointed out by David:
if ( a && b && c) doSomething1;
else if( a && b && !c) doSomething1;
else if( a && !b && c) doSomething2;
else if( a && !b && !c) doSomething2;
else if(!a && b && c) doSomething1;
else if(!a && b && !c) doSomething1;
else if(!a && !b && c) doSomething3;
else if(!a && !b && !c) doSomething2;
Besides being self documented, I think subsequent changes will be easier to follow in code revisioning system logs for this too.