Avoiding multiple If statements

Is there a better way in Apex to have multiple If Statements? I know that multiple if statements will slow performance, and my scenario would require me to have 36 different scenarios.

Ex:

I have 2 objects with 2 fields each on them. Each of those fields has 3 possible values (0,X,Y). So line 1 of the 36 lines I’d need would look like :

if(obj1.field1 == 0 && obj1.field2 == 0 && obj2.field1 == 0 && obj2.field2 == 0){
  ///////scenario 1 logic
} 
if(scenario 2 conditions){
  /////scenario 2 
}

and so on until all conditions are covered. Is a switch statement my only real option, or can this be solved in a different way?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Let’s assume that all tests are equalities

You could create a simple two-level map. Note that map keys can be sobjects. The map key will be an sobject possessing only the fields+vals for a given combination to evaluate. I chose a two level map to make it easier to setup as there are two sobjecttypes involved in the OP.

// define map (most likely static) of all 36 branches.  
// Map is a map of maps where the leftmost key is the first SobjectType and
// and the rightmost key is a map of the second sobjecttype
static Map<Object1__c,Map<Object2__c,Boolean>> evaluator = 
  new Map<Object1__c,Map<Object2__c,Boolean>> {
   new Object1__c(F1__c = 'a', F2__c = 'b') =>
     new Map<Object2__c,Boolean> {
        new Object2__c(G1__c = 'x', G2__c = 'y') => false,
        new Object2__c(G1__c = 'w', G2__c = 'y') => true,
        ... enumerate all possible combos of G1 and G2 in Object2 (6 total)
     },
   new Object1__c(F1__c = 'a', F2__c = 'c') =>
     new Map<Object2__c,Boolean> {
        new Object2__c(G1__c = 'x', G2__c = 'y') => true,
        new Object2__c(G1__c = 'w', G2__c = 'y') => true,
        ... enumerate all possible combos of G1 and G2 in Object2 (6 total)
     }, 
   ... for remaining combos of values F1, F2 in Object1 (4 more)
};

Then your code to evaluate for false/true is simply

private Boolean evaluate(Object f1,Object f2, Object g1, Object g2) {
 // Build lookup keys from inputVals to method
 Object1__c primaryKey = new Object1__c(F1__c = f1, F2__c = f2);
 Object2__c secondaryKey = new Object2__c(G1__c = g1, G2__c = g2);


 return evaluator.containsKey(primaryKey) // entry for Object1 ?
    ? evaluator.get(primaryKey).containsKey(secondaryKey) // yes, entry for Object2 ?
      ? evaluator.get(primaryKey).get(secondaryKey) // yes, get result t/f
      : false // nope, default to false
    : false;  // nope, default to false
 }

and you would invoke by

evaluate(10,'abc',67,'xyz');  // args would of course be vbls populated 
                             //  from some input like json or VF controller or trigger

Code coverage is trivial although it would be a good idea to test that you set up your evaluator map with the expected results; The test method merely needs to generate 36 inputs and verify against an expected results list

This could be generalized to put the evaluator map in custom metadata as a JSON object that you could at runtime deserialize (or as a list of custom metadata that you read and build the evaluator dynamically)

N.B. if tests are inequalities or the range of fields used to test are not consistent, then the above pattern breaks


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x