Avoid simple if/else conditions [duplicate]










0
















This question already has an answer here:



  • How to avoid a lot of if else conditions

    8 answers



In my program, I need to check if a variable equals 1, 2 or 3 and depending on the result, perform a different method:



if (phase.equals("1")) 
PhaseOne.performPhase(inputParser.getSource(), inputParser.getTarget());
else if (phase.equals("2"))
PhaseTwo.performPhase(inputParser.getSource(), inputParser.getTarget());
else
PhaseThree.performPhase(inputParser.getSource(), inputParser.getTarget());



This code is so simple and basic but I really don't like it. Of course I could use switch conditions but it would, in my humble opinion, just display the same basic function in a different way.



My question is: is there a way to implement the function in an elegant and expandable way?



FYI, I already red this post but I did not find an answer which fits to my question.










share|improve this question















marked as duplicate by fantaghirocco, Community Nov 13 '18 at 12:20


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















  • Judging by the fact that your classes are named Phase I would suggest the State pattern: en.wikipedia.org/wiki/State_pattern

    – Jeroen Steenbeeke
    Nov 13 '18 at 9:57











  • Are those static methods? Otherwise you can just create a Map<String, Phase> phases and call phases.get("1").performPhase().

    – Robby Cornelissen
    Nov 13 '18 at 9:57











  • You can use a switch in the case of strings. switch(phase) case "1": /*something*/; break; case "2" ...

    – Michael
    Nov 13 '18 at 9:58











  • You should use case instead of if, is safer because it avoids 2 situations with the same condition. Case does not alow 2 cases with the same value.

    – Victorqedu
    Nov 13 '18 at 10:18











  • Related: Replacing if else statement with pattern

    – LuCio
    Nov 13 '18 at 11:32















0
















This question already has an answer here:



  • How to avoid a lot of if else conditions

    8 answers



In my program, I need to check if a variable equals 1, 2 or 3 and depending on the result, perform a different method:



if (phase.equals("1")) 
PhaseOne.performPhase(inputParser.getSource(), inputParser.getTarget());
else if (phase.equals("2"))
PhaseTwo.performPhase(inputParser.getSource(), inputParser.getTarget());
else
PhaseThree.performPhase(inputParser.getSource(), inputParser.getTarget());



This code is so simple and basic but I really don't like it. Of course I could use switch conditions but it would, in my humble opinion, just display the same basic function in a different way.



My question is: is there a way to implement the function in an elegant and expandable way?



FYI, I already red this post but I did not find an answer which fits to my question.










share|improve this question















marked as duplicate by fantaghirocco, Community Nov 13 '18 at 12:20


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















  • Judging by the fact that your classes are named Phase I would suggest the State pattern: en.wikipedia.org/wiki/State_pattern

    – Jeroen Steenbeeke
    Nov 13 '18 at 9:57











  • Are those static methods? Otherwise you can just create a Map<String, Phase> phases and call phases.get("1").performPhase().

    – Robby Cornelissen
    Nov 13 '18 at 9:57











  • You can use a switch in the case of strings. switch(phase) case "1": /*something*/; break; case "2" ...

    – Michael
    Nov 13 '18 at 9:58











  • You should use case instead of if, is safer because it avoids 2 situations with the same condition. Case does not alow 2 cases with the same value.

    – Victorqedu
    Nov 13 '18 at 10:18











  • Related: Replacing if else statement with pattern

    – LuCio
    Nov 13 '18 at 11:32













0












0








0


1







This question already has an answer here:



  • How to avoid a lot of if else conditions

    8 answers



In my program, I need to check if a variable equals 1, 2 or 3 and depending on the result, perform a different method:



if (phase.equals("1")) 
PhaseOne.performPhase(inputParser.getSource(), inputParser.getTarget());
else if (phase.equals("2"))
PhaseTwo.performPhase(inputParser.getSource(), inputParser.getTarget());
else
PhaseThree.performPhase(inputParser.getSource(), inputParser.getTarget());



This code is so simple and basic but I really don't like it. Of course I could use switch conditions but it would, in my humble opinion, just display the same basic function in a different way.



My question is: is there a way to implement the function in an elegant and expandable way?



FYI, I already red this post but I did not find an answer which fits to my question.










share|improve this question

















This question already has an answer here:



  • How to avoid a lot of if else conditions

    8 answers



In my program, I need to check if a variable equals 1, 2 or 3 and depending on the result, perform a different method:



if (phase.equals("1")) 
PhaseOne.performPhase(inputParser.getSource(), inputParser.getTarget());
else if (phase.equals("2"))
PhaseTwo.performPhase(inputParser.getSource(), inputParser.getTarget());
else
PhaseThree.performPhase(inputParser.getSource(), inputParser.getTarget());



This code is so simple and basic but I really don't like it. Of course I could use switch conditions but it would, in my humble opinion, just display the same basic function in a different way.



My question is: is there a way to implement the function in an elegant and expandable way?



FYI, I already red this post but I did not find an answer which fits to my question.





This question already has an answer here:



  • How to avoid a lot of if else conditions

    8 answers







java






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 9:55









Socowi

6,6202725




6,6202725










asked Nov 13 '18 at 9:54









ErikWeErikWe

100112




100112




marked as duplicate by fantaghirocco, Community Nov 13 '18 at 12:20


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by fantaghirocco, Community Nov 13 '18 at 12:20


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • Judging by the fact that your classes are named Phase I would suggest the State pattern: en.wikipedia.org/wiki/State_pattern

    – Jeroen Steenbeeke
    Nov 13 '18 at 9:57











  • Are those static methods? Otherwise you can just create a Map<String, Phase> phases and call phases.get("1").performPhase().

    – Robby Cornelissen
    Nov 13 '18 at 9:57











  • You can use a switch in the case of strings. switch(phase) case "1": /*something*/; break; case "2" ...

    – Michael
    Nov 13 '18 at 9:58











  • You should use case instead of if, is safer because it avoids 2 situations with the same condition. Case does not alow 2 cases with the same value.

    – Victorqedu
    Nov 13 '18 at 10:18











  • Related: Replacing if else statement with pattern

    – LuCio
    Nov 13 '18 at 11:32

















  • Judging by the fact that your classes are named Phase I would suggest the State pattern: en.wikipedia.org/wiki/State_pattern

    – Jeroen Steenbeeke
    Nov 13 '18 at 9:57











  • Are those static methods? Otherwise you can just create a Map<String, Phase> phases and call phases.get("1").performPhase().

    – Robby Cornelissen
    Nov 13 '18 at 9:57











  • You can use a switch in the case of strings. switch(phase) case "1": /*something*/; break; case "2" ...

    – Michael
    Nov 13 '18 at 9:58











  • You should use case instead of if, is safer because it avoids 2 situations with the same condition. Case does not alow 2 cases with the same value.

    – Victorqedu
    Nov 13 '18 at 10:18











  • Related: Replacing if else statement with pattern

    – LuCio
    Nov 13 '18 at 11:32
















Judging by the fact that your classes are named Phase I would suggest the State pattern: en.wikipedia.org/wiki/State_pattern

– Jeroen Steenbeeke
Nov 13 '18 at 9:57





Judging by the fact that your classes are named Phase I would suggest the State pattern: en.wikipedia.org/wiki/State_pattern

– Jeroen Steenbeeke
Nov 13 '18 at 9:57













Are those static methods? Otherwise you can just create a Map<String, Phase> phases and call phases.get("1").performPhase().

– Robby Cornelissen
Nov 13 '18 at 9:57





Are those static methods? Otherwise you can just create a Map<String, Phase> phases and call phases.get("1").performPhase().

– Robby Cornelissen
Nov 13 '18 at 9:57













You can use a switch in the case of strings. switch(phase) case "1": /*something*/; break; case "2" ...

– Michael
Nov 13 '18 at 9:58





You can use a switch in the case of strings. switch(phase) case "1": /*something*/; break; case "2" ...

– Michael
Nov 13 '18 at 9:58













You should use case instead of if, is safer because it avoids 2 situations with the same condition. Case does not alow 2 cases with the same value.

– Victorqedu
Nov 13 '18 at 10:18





You should use case instead of if, is safer because it avoids 2 situations with the same condition. Case does not alow 2 cases with the same value.

– Victorqedu
Nov 13 '18 at 10:18













Related: Replacing if else statement with pattern

– LuCio
Nov 13 '18 at 11:32





Related: Replacing if else statement with pattern

– LuCio
Nov 13 '18 at 11:32












3 Answers
3






active

oldest

votes


















13














In my opinion, the accepted answer on your linked question fits you very well. Store references to the functions in the map:



Map<String,BiConsumer<T,U>> map = new HashMap<>();
map.put("1",PhaseOne::performPhase);
map.put("2",PhaseTwo::performPhase);
map.put("3",PhaseThree::performPhase);
map.get(phase).accept(inputParser.getSource(), inputParser.getTarget());


Replace T and U by the types of inputParser.getSource() and inputParser.getTarget().



With this approach, the Phase… classes don't need a common superclass or interface.






share|improve this answer
































    2














    If your PhaseOne/PhaseTwo/PhaseThree classes all implement the same interface (let's say Phase), and the method performPhase is defined on the interface you could do the following:



    final Phase targetPhase;
    switch(phase)
    case "1": targetPhase = myInstanceOfPhaseOne; break;
    case "2": targetPhase = myInstanceOfPhaseTwo; break;
    case "3": targetPhase = myInstanceOfPhaseThree; break;
    default: throw new IllegalStateException("Unrecognised phase "+phase);

    targetPhase.performPhase(inputParser.getSource(), inputParser.getTarget()));





    share|improve this answer























    • There are no instances. They're static methods.

      – Michael
      Nov 13 '18 at 9:59












    • @Michael Not necessarily. OP might also just be violating naming conventions.

      – Robby Cornelissen
      Nov 13 '18 at 10:02











    • @RobbyCornelissen Seems unlikely given that he's used the correct naming conventions everywhere else.

      – Michael
      Nov 13 '18 at 10:02











    • To be honest, this was more of a suggestion rather than a question :)

      – Rich
      Nov 13 '18 at 10:08


















    -1














    Another option is to create a Class for each Phase and an IPhase Interface for them to implement.
    Create a List<IPhase> with all your different Phase instances.
    Run a loop and if the id matches, execute the overwritten method.



    public interface IPhase 
    public void performPhase();
    public String getId();


    for (IPhase phase : phasesList)
    if (phase.equals(phase.getId()))
    phase.performPhase();
    // either break or continue the loop







    share|improve this answer































      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      13














      In my opinion, the accepted answer on your linked question fits you very well. Store references to the functions in the map:



      Map<String,BiConsumer<T,U>> map = new HashMap<>();
      map.put("1",PhaseOne::performPhase);
      map.put("2",PhaseTwo::performPhase);
      map.put("3",PhaseThree::performPhase);
      map.get(phase).accept(inputParser.getSource(), inputParser.getTarget());


      Replace T and U by the types of inputParser.getSource() and inputParser.getTarget().



      With this approach, the Phase… classes don't need a common superclass or interface.






      share|improve this answer





























        13














        In my opinion, the accepted answer on your linked question fits you very well. Store references to the functions in the map:



        Map<String,BiConsumer<T,U>> map = new HashMap<>();
        map.put("1",PhaseOne::performPhase);
        map.put("2",PhaseTwo::performPhase);
        map.put("3",PhaseThree::performPhase);
        map.get(phase).accept(inputParser.getSource(), inputParser.getTarget());


        Replace T and U by the types of inputParser.getSource() and inputParser.getTarget().



        With this approach, the Phase… classes don't need a common superclass or interface.






        share|improve this answer



























          13












          13








          13







          In my opinion, the accepted answer on your linked question fits you very well. Store references to the functions in the map:



          Map<String,BiConsumer<T,U>> map = new HashMap<>();
          map.put("1",PhaseOne::performPhase);
          map.put("2",PhaseTwo::performPhase);
          map.put("3",PhaseThree::performPhase);
          map.get(phase).accept(inputParser.getSource(), inputParser.getTarget());


          Replace T and U by the types of inputParser.getSource() and inputParser.getTarget().



          With this approach, the Phase… classes don't need a common superclass or interface.






          share|improve this answer















          In my opinion, the accepted answer on your linked question fits you very well. Store references to the functions in the map:



          Map<String,BiConsumer<T,U>> map = new HashMap<>();
          map.put("1",PhaseOne::performPhase);
          map.put("2",PhaseTwo::performPhase);
          map.put("3",PhaseThree::performPhase);
          map.get(phase).accept(inputParser.getSource(), inputParser.getTarget());


          Replace T and U by the types of inputParser.getSource() and inputParser.getTarget().



          With this approach, the Phase… classes don't need a common superclass or interface.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 13 '18 at 10:12

























          answered Nov 13 '18 at 10:01









          SocowiSocowi

          6,6202725




          6,6202725























              2














              If your PhaseOne/PhaseTwo/PhaseThree classes all implement the same interface (let's say Phase), and the method performPhase is defined on the interface you could do the following:



              final Phase targetPhase;
              switch(phase)
              case "1": targetPhase = myInstanceOfPhaseOne; break;
              case "2": targetPhase = myInstanceOfPhaseTwo; break;
              case "3": targetPhase = myInstanceOfPhaseThree; break;
              default: throw new IllegalStateException("Unrecognised phase "+phase);

              targetPhase.performPhase(inputParser.getSource(), inputParser.getTarget()));





              share|improve this answer























              • There are no instances. They're static methods.

                – Michael
                Nov 13 '18 at 9:59












              • @Michael Not necessarily. OP might also just be violating naming conventions.

                – Robby Cornelissen
                Nov 13 '18 at 10:02











              • @RobbyCornelissen Seems unlikely given that he's used the correct naming conventions everywhere else.

                – Michael
                Nov 13 '18 at 10:02











              • To be honest, this was more of a suggestion rather than a question :)

                – Rich
                Nov 13 '18 at 10:08















              2














              If your PhaseOne/PhaseTwo/PhaseThree classes all implement the same interface (let's say Phase), and the method performPhase is defined on the interface you could do the following:



              final Phase targetPhase;
              switch(phase)
              case "1": targetPhase = myInstanceOfPhaseOne; break;
              case "2": targetPhase = myInstanceOfPhaseTwo; break;
              case "3": targetPhase = myInstanceOfPhaseThree; break;
              default: throw new IllegalStateException("Unrecognised phase "+phase);

              targetPhase.performPhase(inputParser.getSource(), inputParser.getTarget()));





              share|improve this answer























              • There are no instances. They're static methods.

                – Michael
                Nov 13 '18 at 9:59












              • @Michael Not necessarily. OP might also just be violating naming conventions.

                – Robby Cornelissen
                Nov 13 '18 at 10:02











              • @RobbyCornelissen Seems unlikely given that he's used the correct naming conventions everywhere else.

                – Michael
                Nov 13 '18 at 10:02











              • To be honest, this was more of a suggestion rather than a question :)

                – Rich
                Nov 13 '18 at 10:08













              2












              2








              2







              If your PhaseOne/PhaseTwo/PhaseThree classes all implement the same interface (let's say Phase), and the method performPhase is defined on the interface you could do the following:



              final Phase targetPhase;
              switch(phase)
              case "1": targetPhase = myInstanceOfPhaseOne; break;
              case "2": targetPhase = myInstanceOfPhaseTwo; break;
              case "3": targetPhase = myInstanceOfPhaseThree; break;
              default: throw new IllegalStateException("Unrecognised phase "+phase);

              targetPhase.performPhase(inputParser.getSource(), inputParser.getTarget()));





              share|improve this answer













              If your PhaseOne/PhaseTwo/PhaseThree classes all implement the same interface (let's say Phase), and the method performPhase is defined on the interface you could do the following:



              final Phase targetPhase;
              switch(phase)
              case "1": targetPhase = myInstanceOfPhaseOne; break;
              case "2": targetPhase = myInstanceOfPhaseTwo; break;
              case "3": targetPhase = myInstanceOfPhaseThree; break;
              default: throw new IllegalStateException("Unrecognised phase "+phase);

              targetPhase.performPhase(inputParser.getSource(), inputParser.getTarget()));






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 13 '18 at 9:57









              RichRich

              9,8291165116




              9,8291165116












              • There are no instances. They're static methods.

                – Michael
                Nov 13 '18 at 9:59












              • @Michael Not necessarily. OP might also just be violating naming conventions.

                – Robby Cornelissen
                Nov 13 '18 at 10:02











              • @RobbyCornelissen Seems unlikely given that he's used the correct naming conventions everywhere else.

                – Michael
                Nov 13 '18 at 10:02











              • To be honest, this was more of a suggestion rather than a question :)

                – Rich
                Nov 13 '18 at 10:08

















              • There are no instances. They're static methods.

                – Michael
                Nov 13 '18 at 9:59












              • @Michael Not necessarily. OP might also just be violating naming conventions.

                – Robby Cornelissen
                Nov 13 '18 at 10:02











              • @RobbyCornelissen Seems unlikely given that he's used the correct naming conventions everywhere else.

                – Michael
                Nov 13 '18 at 10:02











              • To be honest, this was more of a suggestion rather than a question :)

                – Rich
                Nov 13 '18 at 10:08
















              There are no instances. They're static methods.

              – Michael
              Nov 13 '18 at 9:59






              There are no instances. They're static methods.

              – Michael
              Nov 13 '18 at 9:59














              @Michael Not necessarily. OP might also just be violating naming conventions.

              – Robby Cornelissen
              Nov 13 '18 at 10:02





              @Michael Not necessarily. OP might also just be violating naming conventions.

              – Robby Cornelissen
              Nov 13 '18 at 10:02













              @RobbyCornelissen Seems unlikely given that he's used the correct naming conventions everywhere else.

              – Michael
              Nov 13 '18 at 10:02





              @RobbyCornelissen Seems unlikely given that he's used the correct naming conventions everywhere else.

              – Michael
              Nov 13 '18 at 10:02













              To be honest, this was more of a suggestion rather than a question :)

              – Rich
              Nov 13 '18 at 10:08





              To be honest, this was more of a suggestion rather than a question :)

              – Rich
              Nov 13 '18 at 10:08











              -1














              Another option is to create a Class for each Phase and an IPhase Interface for them to implement.
              Create a List<IPhase> with all your different Phase instances.
              Run a loop and if the id matches, execute the overwritten method.



              public interface IPhase 
              public void performPhase();
              public String getId();


              for (IPhase phase : phasesList)
              if (phase.equals(phase.getId()))
              phase.performPhase();
              // either break or continue the loop







              share|improve this answer





























                -1














                Another option is to create a Class for each Phase and an IPhase Interface for them to implement.
                Create a List<IPhase> with all your different Phase instances.
                Run a loop and if the id matches, execute the overwritten method.



                public interface IPhase 
                public void performPhase();
                public String getId();


                for (IPhase phase : phasesList)
                if (phase.equals(phase.getId()))
                phase.performPhase();
                // either break or continue the loop







                share|improve this answer



























                  -1












                  -1








                  -1







                  Another option is to create a Class for each Phase and an IPhase Interface for them to implement.
                  Create a List<IPhase> with all your different Phase instances.
                  Run a loop and if the id matches, execute the overwritten method.



                  public interface IPhase 
                  public void performPhase();
                  public String getId();


                  for (IPhase phase : phasesList)
                  if (phase.equals(phase.getId()))
                  phase.performPhase();
                  // either break or continue the loop







                  share|improve this answer















                  Another option is to create a Class for each Phase and an IPhase Interface for them to implement.
                  Create a List<IPhase> with all your different Phase instances.
                  Run a loop and if the id matches, execute the overwritten method.



                  public interface IPhase 
                  public void performPhase();
                  public String getId();


                  for (IPhase phase : phasesList)
                  if (phase.equals(phase.getId()))
                  phase.performPhase();
                  // either break or continue the loop








                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 13 '18 at 10:22

























                  answered Nov 13 '18 at 10:01









                  NikiforosNikiforos

                  328215




                  328215













                      Popular posts from this blog

                      Kleinkühnau

                      Makov (Slowakei)

                      Deutsches Schauspielhaus