Combinations of if/else can seriously damage readability and maintainability.
I (all the time) face verbose code that pretend to be readable and that requires lots of attention just to find out you are setting a single variable across 10-20 lines.
Example:
if (cond1)
{
myVar = 1;
}
else if (cond2)
{
if (cond3)
{
myVar = 10;
}
else
{
myVar = 100;
}
...
}
else
{
myVar = defaultValue;
}
* huge number of lines* not obvious (until we read all) that we setup a single variable
* combination of conditions difficult to trace for each given value
* high risk of forgetting cases...
Instead of
myVar =
(cond1) ? 1 : // comment case blabla
(cond2) && (cond3) ? 10 : // comment case bla
(cond2) && (!cond3) ? 100 : // comment case blablabla
...
defaultValue; // comment default
* one line per given value* operation on a single variable obvious
* match between conditions and values obvious
* high visibility on all the cases
* even more readable with proper vertical alignment
* a comment can be added on each line to document the case
[sorry for the multiple edits: I had a hard time getting { } properly display, they ate up the new lines]