Is it considered bad form to execute a function within a conditional statement?
- by michael
Consider a situation in which you need to call successive routines and stop as soon as one returns a value that could be evaluated as positive (true, object, 1, str(1)).
It's very tempting to do this:
if (fruit = getOrange())
elseif (fruit = getApple())
elseif (fruit = getMango())
else fruit = new Banana();
return fruit;
I like it, but this isn't a very recurrent style in what can be considered professional production code. One is likely to rather see more elaborate code like:
fruit = getOrange();
if(!fruit){
fruit = getApple();
if(!fruit){
fruit = getMango();
if(!fruit){
fruit = new Banana();
}
}
}
return fruit;
According to the dogma on basic structures, is the previous form acceptable? Would you recommend it?