According to anti-if campaign it is a best practice not to use ifs in our code. Can anyone tell me if it possible to get rid of the if in this piece of code ? (switch is also
Make use of the strategy pattern.
In Java terms:
public interface Strategy {
void execute();
}
public class SomeStrategy implements Strategy {
public void execute() {
System.out.println("Some logic.");
}
}
which you use as follows:
Map<String, Strategy> strategies = new HashMap<String, Strategy>();
strategies.put("strategyName1", new SomeStrategy1());
strategies.put("strategyName2", new SomeStrategy2());
strategies.put("strategyName3", new SomeStrategy3());
// ...
strategies.get(s).execute();
i'd like to point out that so far, every answer to this question with a code example has a solution that is far more complicated than the original code, and likely much slower.
this is a classic case of an optimization being performed in entirely the wrong context. in some cases, code will become clearer through using OO properly, such as eliminating long chains of type checks. however, simply removing all if statements for the sake of removing them only serves to obfuscate your code.
the if statements (conditional jumps) are still going to happen, be it in your code or the interpreter. keeping them lexically close has many readability and maintenance advantages that are lost through excessive OO use. there is a balance that must be struck between local vs distant logic, but it should never spill over into obfuscation.
for the question at hand, the clearest construct that will avoid the if
is probably a hash table / associative array containing anonymous functions, which, for a small number of keys, is effectively just a slow switch statement.
Make an associative data structure. Map<String, String>
in Java, IDictionary<string, string>
in C#. Initialize it at the beginning of time, and then ...
First of all, be very attentive when reading such "anti" campaigns.
C#
switch (myStringVar)
{
case "one": doSomething(); break;
case "two": doSomething(); break;
case "three": doSomething(); break;
default: doSomething(); break;
}
Finally, it reduces this code to the if s... so, only for readability is better, not for performance.
Actually, if Microsoft believes that switch (in c#) is better to replace with if's - OK, I will use (in the concrete situation that you described) the switch.
By the way, it seems that the campaign responds to your question very clear in this example
I don't think you are making a fair comparison here.
From the look of it the Anti-if campaign is just about practicing a better design approach.
However in your case you can see from all the above examples that if can not be removed from the surface and will always exist somewhere down in the center.
And why exactly is that?
Well If is a general purpose of life. I don't mean to say start coding if every where but in general without if there is no differentiation, if brings decisions and purpose, if that wasn't there then every object in the world would just execute as its suppose to without even knowing anything other then it self. And very simple you wouldn't have asked this question. :)
I read http://www.antiifcampaign.com/articles/the-simplest-anti-if-code.html and I think that the medicine is worse than the disease. Much, much worse. You required to invest up front in some heavy OO machinery to solve a possible (improbable?) future problem.