java - Simplifying an if-statment -
i have program few if
-statements similar 1 i'm present you. wondering if guys me simplify equation in way. reason why ask because in notepad++, continues on 443 columns , it's frustrating edit if needed, , keeps getting longer keep adding variables. basically, example of 1 of many similar if
statements, want action when of sliderbars
try raise in value when int
(rpg.statisticpoints
) equal to, or less 0. method i'm using find out if sliders rising in value compare current value int
associated slider, , check if result positive.
if (rpg.statisticpoints <= 0 && ((rpg.willslider.getvalue() - rpg.will) < 0) && ((rpg.perceptionslider.getvalue() - rpg.perception) < 0) && ((rpg.strengthslider.getvalue() - rpg.strength) < 0) && ((rpg.dexterityslider.getvalue() - rpg.dexterity) < 0) && ((rpg.constitutionslider.getvalue() - rpg.constitution) < 0) && ((rpg.charismaslider.getvalue() - rpg.charisma) < 0) && ((rpg.intelligenceslider.getvalue() - rpg.intelligence) < 0)) { //do actions }
i understand there lot of variables here you're not familiar because didn't see full code, full source 100's of lines long , question based on logistics, , not issues syntax.
the problem current solution isn't long line, it's hard read , understand being validated.
instead of using conditions in if
statement, can create auxiliary method build boolean
value of validation, , @ same time give meaningful name.
for instance:
private boolean isvalidsomething(){ boolean result = firstcondition; result &= secondcondition; ... return result; }
this way checks concentrated in 1 place, , lot more readable, since if
become:
if(isvalidsomething()) {...}
of course, create method name makes sense in application.
in case you're validating several different conditions make sense separated, go mile , factor them out in own methods.
the main thing break logic pieces make sense together, like:
private boolean validstatistics() { return statistics > 0; } private boolean validwill() { return > 0; } ....
and main validation like:
private boolean validcharacter() { boolean valid = validstatistics(); valid &= validwill(); ... return valid; }
Comments
Post a Comment