In my current case, I've got two variables. Let's call them action
and status
for ease of use. They can have any value between 1 and 9. Depending on business logic, I've to either call subActionA()
or subActionB()
. I'm trying to write this in the most readable way, and so far I'm not happy with the results. What I've effectively gotten down to is the following
if(action==1){
if(status==1){
}else if(status==2{
}else...
}else if(action==2){
if(status==1){
}else if(status==2{
}else...
}else....
This solution, while it works, doesn't seem particularly clean. Is there a better way to represent this type of decision tree? The only thing that comes to mind for me is a nested switch, which would be as bad as this nested if.
How would this be extended for more variables, or if there were more than two subactions?
We're basically dealing with handling medical prescriptions. The actual 'action's are 'update','add','duplicate','sign','reject' etc. The 'status' is one of 9 statuses of the prescription as defined by the business. Based on the status and the action, we either edit the current prescription or create a new one.
EDIT 1: While How to tackle a 'branched' arrow head anti-pattern might seem similar, I think it's only superficially so. That question deals with branching based of sub conditions that only occur under some combination of earlier conditions, with each set variable being true or false. My question deals with writing a clean solution for all possible combinations of a set of variables that can have more that the fixed true
and false
values. Additionally, the linked question doesn't mention if the result of this conditional checking returns a different result for each set of conditions or, like mine, calls one of two functions based on the combination.
EDIT 2: The values for action
and status
are not actually integers. I've just represented them as so for ease of understanding. There's no mathematical relation between the values for both variables and which subaction
gets called.