in reply to "if" Considered Harmful in OO programming

Interesting post.

Any time I see something like

if ( $x and !$y ) { .. # true, false } elsif ( !$x and $y ) { .. # false, true } else { .. # true, true or false, false }
warning bells go off because I wonder if it could have been written as
if ( $x ) { if ( $y ) { .. # true, true } else { .. # true, false } } else { if ( $y ) { .. # false, true } else { .. # false, false } }
instead. Granted, this may not be required much of the time, but it sure makes code maintenance a year later a little easier.

Alex / talexb / Toronto

"Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

Replies are listed 'Best First'.
Re^2: "if" Considered Harmful in OO programming
by ikegami (Patriarch) on Sep 21, 2004 at 23:31 UTC

    I disagree. I don't think duplicating code is good for maintenance at all.

    What if you have 3 values instead of 2. Your if would be three levels deep, and have say 5 identical pieces of code. Let's hope you don't forget to change all 5 if a changes is needed. What you suggest is not scaleable at all.

    else means all other cases. If you have 2 values in a flat if, else covers the differences of 4 cases. If you have 3 variables in a flat if, else covers the differences of 9 cases. etc. It's not that hard to process, especially compared to what you recommend.