Reputation: 6042
It is not first time I encounter this problem and surely it won't be the last. There should be more efficient way than doing countless IF statements whenever there are multiple options for user to choose from and this user gives input for several variables but not the others, though we need to do some job for others-not-chosen too where their not-chosen state demands some work.
5 options with sub-options means our number of IF statements grows dramatically, I am not sure if SWITCH is applicable as comparison logic is still required, so we would still need to somehow evaluate variable for SWITCH prior using it which would again be IF?
If you already understood the scope please give your insight on kind of pattern to do or solution to handle these cases more efficiently? Or if you didn't I've prepared an example to explain better. This is very general question which we've all encountered so please don't ignore if you feel you don't understand - I may be just really bad at explaining it.
My first example was really long but was related to updating user details (prepopulated form+validation). When user submits some params but not all and we don't want to trigger validation for those fields which are say remain unchanged. In short this generates a number of nested IFs.
Now second example which I hope will clear all confusion around my question. Consider simple HTML table. In our table we have four columns with names:
EVENT NAME | EVENT TYPE | EVEN START DATE | EVENT END DATE
In my scenario I should allow sorting by two fields.
As a User I can generate for you 4X3=12 cases of sorting with just any two fields
with three it grows to 4x3x2=24
Obviously no IF or SWITCH statement will save us(well in a short way). In fact I rarely used SWITCH statement and asfaik it is quite limited at least in java allowing to switch by integers or strings.
But the funny side problem with SWITCH is that prior delivering variables to it I have to first evaluate them. So from HTML form I submit two variables sortByParamOne=NAME sortByParamTwo=TYPE
Then I have to declare that:
if(sortByParamOne.equals("NAME")&&sortByParamTwo.equals("TYPE"))
case = 1
else if ...
case = 2
else if ...
12x IF in total just to prepare variables
One way or another switch works only with singular input and seems to provide little help when we have two actor variable assuming different roles. Please correct me if I am wrong.
How do you deal with this kind of situations not using huge number of IFs keeping code clean and short? It seems this is sort of mathematical realm rather than design patterns or our programming tactics, possibly even related to set theory as in SQL we can ORDER BY in any conceivable way and using any number of parameters.
I think in one "Spring" frmwrk book they've dedicated whole chapter for this multi-sorting example problem with tables. Possibly another library which says little how things work. I don't have it in my possession atm so can't do look-up.
Upvotes: 0
Views: 148
Reputation: 1193
Let's take your sorting example. Even though there are many possibilities of the order in which the user would want to sort by, they are all symmetric. That means, it doesn't really matter which field you are sorting by because you will apply the same methodology to it.
So, lets say you had to write a custom comparator to perform the sorts. I would do something like:
public CustomComparator implements Comparator<TableRow> {
// this will contain the column names that the table will be sorted by, in order
// eg. "NAME", "TYPE", "STARTDATE"
private List<String> conditions;
public CustomComparator(List<String> cond) { conditions = cond; }
public int compare(TableRow row1, TableRow row2)
{
for (String condition : conditions)
{
int result = row1.getColumn(condition).compareTo(row2.getColumn(condition));
if (result != 0) return result;
}
return 0;
}
}
Of course the above example assumes that all columns are Comparable
.
Upvotes: 0
Reputation: 68715
Apart from general techniques to club conditions, use switch statements, reflections API, i would recommend to think about how to make your code modular. You can divide your code into smaller methods and may decrease the number of conditions you write.. Try to exercise the re-use of methods wherever possible. Always try to write methods in a generic way for the purpose of re-usability.
Lot of theory, now lets see some example now. Suppose you need to find the largest of three numbers. This is how we may write it without using a modular approach:
public int largest(int first, int second, int third) {
int largest = first;
if ( second > largest )
largest = second;
else if (third > largest)
largest = third;
}
Now re-write it using methods
public int largest(int first, int second, int third) {
return largest(first,largest(second,third));
}
public int largestOfTwo(int first, int second) {
int largest = first;
if( second > largest)
largest = second;
return largest;
}
The difference should be visible in the above sample. If not then I am failed to prove my thoery :-(
Upvotes: 1