I have a program with a few if-statements similar to the one I'm about to present to you. I was wondering if you guys could help me simplify this equation in any way. The reason why I ask is because in my Notepad++, it continues on for 443 columns and it's just really frustrating to edit if needed, and just keeps getting longer as I keep adding variables. Basically, for this example of one of my many similar if statements, I just want to do an action when ANY of my SliderBars try to raise in value when an int (rpg.StatisticPoints) is equal to, or less then 0. The method I'm using to find out if the sliders are rising in value is to just compare its current value to an int associated with the slider, and check if the result is 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 are a lot of variables here you're not familiar with because you didn't see my full code, but my full source is 100's of lines long and my question is just based on logistics, and not really issues with the syntax.
The problem with your current solution isn't just a long line, is that it's hard for someone to read and understand what is actually being validated.
Instead of using all the conditions in an if statement, you can create an auxiliary method that build the boolean value of that validation, and at the same time give it a meaningful name.
For instance:
private boolean isValidSomething(){
boolean result = firstCondition;
result &= secondCondition;
...
return result;
}
This way all your checks will be concentrated in one place, AND it will be a lot more readable, since your if will become:
if(isValidSomething()) {...}
Of course, create the method with a name that makes sense in your application.
In case you're validating several different conditions that make sense to be separated, go the extra mile and factor them out in to their own methods.
The main thing is to break that logic into pieces that make sense together, like:
private boolean validStatistics() {
return statistics > 0;
}
private boolean validWill() {
return will > 0;
}
....
And your main validation would be something like:
private boolean validCharacter() {
boolean valid = validStatistics();
valid &= validWill();
...
return valid;
}
What about
if (rpg.StatisticPoints <= 0 &&
(rpg.WillSlider.getValue() < rpg.Will) &&
(rpg.PerceptionSlider.getValue() < rpg.Perception) &&
(rpg.StrengthSlider.getValue() < rpg.Strength) &&
(rpg.DexteritySlider.getValue() < rpg.Dexterity) &&
(rpg.ConstitutionSlider.getValue() < rpg.Constitution) &&
(rpg.CharismaSlider.getValue() < rpg.Charisma) &&
(rpg.IntelligenceSlider.getValue() < rpg.Intelligence))
{
//Do actions
}
Furthermore, you could move some code to a seperate function:
bool CheckSliderValue(TypeOfSlider slider, TypeOfSliderValue value)
{
return slider.getValue() < value;
}
and call this by CheckSliderValue(rpg.WillSlider, rpg.Will) a.s.o. Such you could extent the check with a minimum value or easily change "<" to "<=" or alike.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With