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?
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.
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 ? .
Try to look at the strategy pattern.
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);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With