java - Conditionally adding extra else if statements -
the following called every level of app (a game)
pseudo code example
public void checkcollisions(){ if (somecondition) else if (somecondition2) something2 else if (somecondition3) something3 }
it's called so:
maingame.checkcollisions();
however, in selected levels, need throw in additional condition, instead of duplicating whole method like..........
public void checkcollisions2(){ if (somecondition) else if (somecondition2) something2 else if (somecondition3) something3 else if (somecondition4) something4 }
and calling like..........
maingame.checkcollisions2();
i need find way more efficiently (i'm sure there one) way can think of taking in boolean , either carrying out 4th condition (or not) depending on value of boolean is, doesn't seem great way of doing either (say, example if want add more conditions in future).
this has 'else if' statement can't separate method can call in addition original method.
options appreciated
have checkcollisions
return boolean
indicates whether "did something".
public boolean checkcollisions(){ boolean didsomething = false; if (somecondition) { dosomething(); didsomething = true; } else if (condition2) { dosomething2(); didsomething = true; } else if (condition3){ dosomething3(); didsomething = true; } return didsomething; }
then, checkcollisions2
can call checkcollisions
, check if did something. else if
can work normally:
public void checkcollisions2(){ if (checkcollisions()) { // yes, done! } else if (condition4) { dosomething4(); } }
or shorter:
public void checkcollisions2(){ if (!checkcollisions() && condition4) { dosomething4(); } }
Comments
Post a Comment