Skip to main content

Why You Should Avoid If-else Statements

Introduction:

  • Developers tend to use a lot of conditional statements in their code.Of course, each flow has one specific path, its own specific validations, etc. 
  • It is very natural that your code has a lot of decisions to be made at run time. but, mostly, when you use a lot of conditional statements in specific area of your code, probably you are not using it right. 
  • When using lot of "if-else" statement , most of the code is common in between those blocks and compiling this common code repeatedly is obviously waste of timing as well as waste of bandwidth and it decreases the performance as well.
Conditional statements should be used for small decisions, not for big decisions.
(It really hurts than lose girlfriend to see chained else ifs in a method.Really...!!!)

Want an example? Let's check the code below:
---------------------------------------------------------------------------------------------
   String sexInfo = Person.getSexDetails().getValue();
  
    if(sexInfo.equals("Straight or heterosexual")) 
                  Person.getSexDetails().setValue("STRAIGHT_OR_HETEROSEXUAL");
   else if(sexInfo.equals("Lesbian, gay or homosexual"))
                  Person.getSexDetails().setValue("LESBIAN_GAY_OR_HOMOSEXUAL");
   else if(sexInfo.equals("Bisexual"))
                  Person.getSexDetails().setValue("BISEXUAL");
   else if(sexInfo.equals("Something else, please describe"))
                  Person.getSexDetails().setValue("SOMETHING_ELSE");
   else if(sexInfo.equals("Don't know"))
                  Person.getSexDetails().setValue("UNKNOWN");
   else if(sexInfo.equals("Choose not to disclose"))
                  Person.getSexDetails().setValue("NA");
--------------------------------------------------------------------------------------------         

How We can Implement                        


The first step you need to do is: try not to use the else if.
No, I'm not crazy. You can just do this and make your code considerably more readable:
So let us see how we can achieve this goal.

Step 1- Declare HashMap

              private static HashMap<String, String> sexDetailsMap;

Step 2- Give If condition as key and Action as value to the map in static block so that every
             time when code run it should not create map everytime.

---------------------------------------------------------------------------------------------
 static {
             //sexDetailsMap
             sexDetailsMap = new HashMap<String, String>();
             sexDetailsMap.put("Straight or heterosexual", "STRAIGHT_OR_HETEROSEXUAL");
             sexDetailsMap.put("Lesbian, gay or homosexual","LESBIAN_GAY_OR_HOMOSEXUAL");
             sexDetailsMap.put("Bisexual", "BISEXUAL");
             sexDetailsMap.put("Something else, please describe", "SOMETHING_ELSE");
             sexDetailsMap.put("Don't know", "UNKNOWN");
             sexDetailsMap.put("Choose not to disclose", "NA");
 }
-------------------------------------------------------------------------------------------- 

Step 3- Just take the key and match with the elements in Map 

             Person.getSexDetails().setValue(sexDetailsMap.get(sexInfo));

   Now it looks like meeting girlfriend after long time right...!!!


Comments