title: What is the best way to replace or substitute if..else if..else trees in programs?
body: <p>This question is motivated by something I've lately started to see a bit too often, the <code>if..else if..else</code> structure. While it's simple and has its uses, something about it keeps telling me again and again that it could be substituted with something that's more fine-grained, elegant and just generally easier to keep up-to-date.</p>
  
  <p>To be as specific as possible, this is what I mean:</p>
  
  <pre><code>if (i == 1) {
      doOne();
  } else if (i == 2) {
      doTwo();
  } else if (i == 3) {
      doThree();
  } else {
      doNone();
  }
  </code></pre>
  
  <p>I can think of two simple ways to rewrite that, either by ternary (which is just another way of writing the same structure):</p>
  
  <pre><code>(i == 1) ? doOne() : 
  (i == 2) ? doTwo() :
  (i == 3) ? doThree() : doNone();
  </code></pre>
  
  <p>or using Map (in Java and I think in C# too) or Dictionary or any other K/V structure like this:</p>
  
  <pre><code>public interface IFunctor() {
      void call();
  }
  
  public class OneFunctor implemets IFunctor() {
      void call() {
          ref.doOne();
      }
  }
  
  /* etc. */    
  
  Map&lt;Integer, IFunctor&gt; methods = new HashMap&lt;Integer, IFunctor&gt;();
  methods.put(1, new OneFunctor());
  methods.put(2, new TwoFunctor());
  methods.put(3, new ThreeFunctor());
  /* .. */
  (methods.get(i) != null) ? methods.get(i).call() : doNone();
  </code></pre>
  
  <p>In fact the Map method above is what I ended up doing last time but now I can't stop thinking that there has to be better alternatives in general for this exact issue.</p>
  
  <p>So, which other -and most likely better- ways to replace the if..else if..else are out there and which one is your favorite?</p>
  
  <h2>Your thoughts below this line!</h2>
  
  <hr>
  
  <p>Okay, here are <strong>your</strong> thoughts:</p>
  
  <p>First, most popular answer was switch statement, like so:</p>
  
  <pre><code>switch (i) {
      case 1:  doOne(); break;
      case 2:  doTwo(); break;
      case 3:  doThree(); break;
      default: doNone(); break;
  }
  </code></pre>
  
  <p>That only works for values which can be used in switches, which at least in Java is quite a limiting a factor. Acceptable for simple cases though, naturally.</p>
  
  <p>The other and perhaps a bit fancier way you seem to sugges is to do it using polymorphism. The Youtube lecture linked by CMS is an excellent watch, go see it here: <a href="http://www.youtube.com/watch?v=4F72VULWFvc" rel="noreferrer">"The Clean Code Talks -- Inheritance, Polymorphism, &amp; Testing"</a> As far as I understood, this would translate to something like this:</p>
  
  <pre><code>public interface Doer {
      void do();
  }
  
  public class OneDoer implements Doer {
      public void do() {
          doOne();
      }
  }
  /* etc. */
  
  /* some method of dependency injection like Factory: */
  public class DoerFactory() {
      public static Doer getDoer(int i) {
          switch (i) {
              case 1: return new OneDoer();
              case 2: return new TwoDoer();
              case 3: return new ThreeDoer();
              default: return new NoneDoer();
          }
      }
  }
  
  /* in actual code */
  
  Doer operation = DoerFactory.getDoer(i);
  operation.do();
  </code></pre>
  
  <p>Two interesting points from the Google talk:</p>
  
  <ul>
  <li>Use Null Objects instead of returning nulls (and please throw only Runtime Exceptions)</li>
  <li>Try to write a small project without if:s.</li>
  </ul>
  
  <p>Also in addition one post worth mentioning in my opinion is CDR who provided his perverse habits with us and while not recommended to use, it's just very interesting to look at.</p>
  
  <p>Thank you all for the answers (so far), I think I might have learned something today!</p>