Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid a lot of if else conditions

I have read a lot of topics about code refactoring and avoiding of if else statements. Actually, I have a class where I am using a lot of if - else conditions.

More details: I am using the pull parser and on each line of my soap response, I will check if there is a tag I am interested on, if not, check another tag etc:

 if(eventType == XmlPullParser.START_TAG) {             soapResponse= xpp.getName().toString();                          if (soapResponse.equals("EditorialOffice")){                   eventType = xpp.next();                 if (xpp.getText()!=null){                 editorialOffice += xpp.getText();                 }             }                else if (soapResponse.equals("EditorialBoard")){                   eventType = xpp.next();                 if (xpp.getText()!=null){                 editorialBoard += xpp.getText();                 }             }             else if (soapResponse.equals("AdvisoryBoard")){                   eventType = xpp.next();                 if (xpp.getText()!=null){                 advisoryBoard += xpp.getText();                 }             }            }         eventType = xpp.next();      } 

Now, I would like to use something else, instead of those if else conditions, but I don't know what.

Can you please give me an example?

like image 436
Milos Cuculovic Avatar asked Apr 16 '12 14:04

Milos Cuculovic


People also ask

Why should we avoid if else?

In my experience, using if-else statements are not a good practice. It makes the code less readable and it makes developers lazy in thinking of better ways of making their code more readable.

How do you shorten if else statements?

The ternary operator, also known as the conditional operator, is used as shorthand for an if...else statement. A ternary operator is written with the syntax of a question mark ( ? ) followed by a colon ( : ), as demonstrated below. In the above statement, the condition is written first, followed by a ? .


2 Answers

Try to look at the strategy pattern.

  • Make an interface class for handling the responses (IMyResponse)
    • Use this IMyResponse to create AdvisoryBoardResponse, EditorialBoardResponse classes
  • Create an dictionary with the soapresponse value as key and your strategy as value
  • Then you can use the methods of the IMyResponse class by getting it from the dictionary

Little Example:

// Interface public interface IResponseHandler {    public void handleResponse(XmlPullParser xxp);  }  // Concrete class for EditorialOffice response private class EditorialOfficeHandler implements IResponseHandler {    public void handleResponse(XmlPullParser xxp) {        // Do something to handle Editorial Office response    } }  // Concrete class for EditorialBoard response private class EditorialBoardHandler implements IResponseHandler {    public void handleResponse(XmlPullParser xxp) {        // Do something to handle Editorial Board response    } } 

On a place you need to create the handlers:

Map<String, IResponseHandler> strategyHandlers = new HashMap<String,IResponseHandler>(); strategyHandlers.put("EditorialOffice", new EditorialOfficeHandler()); strategyHandlers.put("EditorialBoard", new EditorialBoardHandler()); 

Where you received the response:

IResponseHandler responseHandler = strategyHandlers.get(soapResponse); responseHandler.handleResponse(xxp); 
like image 119
hwcverwe Avatar answered Sep 30 '22 15:09

hwcverwe


In this particular case, since the code is essentially identical for all 3 case except for the String being appended to, I would have a map entry for each of the Strings being built:

Map<String,String> map = new HashMap<String,String>(); map.put("EditorialOffice",""); map.put("EditorialBoard",""); map.put("AdvisoryBoard",""); // could make constants for above Strings, or even an enum 

and then change your code to the following

if(eventType == XmlPullParser.START_TAG) {     soapResponse= xpp.getName().toString();     String current = map.get(soapResponse);     if (current != null && xpp.getText()!=null) {         map.put( soapResponse, current += xpp.getText());     }     eventType = xpp.next(); } 

No "if... then... else". Not even the added complexity of multiple classes for strategy patterns, etc. Maps are your friend. Strategy is great in some situations, but this one is simple enough to be solved without.

like image 34
Kevin Welker Avatar answered Sep 30 '22 15:09

Kevin Welker