java - Branching efficiency -
the couple weeks ago submit function code review looked this.
function{ if (condition) else if (condition) else if (condition) else return value }
my lead giving code review said "i hate else if," didn't why or should have done instead. gave me go ahead upload function.
my question alternatives bunch of "else ifs" make code more elegant , maybe perform better?
i tried pulling code idea of have done , noticed several times did
if (condition) if (condition) if (condition)
should avoid writing "else"? going ask him no longer works here.
thank you
i try avoid "else", "else if" , "if" , "for". in point of view, branching , looping add complexity code. it's everytime depends on want do. examples:
if thing like:
if fruit.isbanana() fruit.eatbanana() else if fruit.isorange() fruit.eatorange() ...
instead of this, can use inheritance:
class banana extends fruit { function eat() { ... yum yum yum banana ... } } class orange extends fruit { function eat() { ... yum yum yum orange ... } }
and then, if have instance:
fruit.eat()
another example: if use "if" , "for" filtering:
longfruits = [] fruit in fruits { if fruit.isbanana() longfruits.add(fruit) }
then can work collections instead:
longfruits = collectionutils.select(fruits, precicate.isbanana)
this couple examples , technics.
Comments
Post a Comment