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

Popular posts from this blog

Exception Propagation and throw-throws keyword

Exception Propagation: Sending exception from one method to another method is called exception propagation Here propagation is nothing but sending. So guys what is the situation when we are going to implement this propagation concept? Answer to this question is when we throw an exception, if it is not caught in that method it is propagated to another method and interesting thing is that we don’t need to do anything in this process we just need to throw an exception that’s it. Rule: If the propagated exception is checked exception then not only current method developer but also method’s caller method developer should caught or report that checked exception else it leads to same compiler exception….CE: Unreported Exception So according to that basic rule is “If checked exception is raised the method either directly by using throw keyword or indirectly by a method call it must be caught or reported. void m1() throws ClassNotFoundException{ ...

Restful API for Beginners

Introduction: Everyone is saying Rest API. Sometimes people say  only API to rest apis.RESTful API is for accessing blah blah service or to do blah blah functionality. In corporate world, fresher get confused when seniors give them task to create new api to call blah blah service or to do blah blah functionality. So what is this REST API? REST (REpresentational State Transfer) is an architectural style, and an approach to communications.Using this REST whatever functionality we create known as REST API. We can implement  rest-api using various providers but usually people use Jersey and Spring . As per my choice personally I would like to use jersey which keep code neat and clean. Rest follows client-server architecture with Front Controller Design Pattern. It completely depends on http protocol .REST implementation is very easy and run in  less memory compared to SOAP. Rest Support following parameter techniques to pass input for our web service...

Apache Solr Search Framework

Introduction: SOLR stands for Searching On Lucene with Replication . Its main functionalities are indexing and searching. Solr is an open source enterprise search server/ web application Created by Yonik Seeley in 2004. In 2012 4.0 version with Cloud support. Solr uses lucene search library and extends it. Solr exposes lucene Java API's as RESTful services. Terminology: Solr Instance: In Apache Solr, a Solr Instance is an instance a Solr running in the JVM. In Standalone mode, Solr contains only one instance. Solr Core: In Apache Solr, a Solr Core is also known as simply “Core”. In other words, a Solr Core = an instance of Apache Lucene Index + Solr Configuratio. Indexing: In Apache Lucene or Solr, Indexing is a technique of adding Document’s content to Solr Index so that we can search them easily. Apache Solr uses Apache Lucene Inverted Index technique to Index it’s documents. That’s why Solr provides very fast searching feature. Document: Solr...