if statement - Common If Structure -


more , more find myself writing structure of if statements looks this:

if(something) {   if(somethingelse) {      // if both evaluate true.      dosomething();   }   else {      // if first if true second not.      dosomethingelse();   } } else {      // if first evaluates false.      dosomethingdifferent(); } 

now, me, looks horrific. have cleaner way of representing logic?

the question as-is has 3 cases: something & somethingelse, something & !somethingelse, , !something. alternative break out if-else 3 branches:

if(something & somethingelse) {      // if both evaluate true.      dosomething();   } elif(something) { // explicit test of somethingelse falsity not required      // if first if true second not.      dosomethingelse();   } else {      // if first evaluates false.      dosomethingdifferent(); } 

for such simple case, prefer flatten structure above. more complex cases, can end being simpler nest, or better reduce tests kind of structure (a list or integer depending on language) , switch on value.


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -