Conditional method chaining in java












11














What is the best possible way to chain methods together? In my scenario, there are four methods. If the output of the first method is true, it has to call the second method.



For Example:



flag = method1();
if (flag){
flag = method2();
}
if (flag){
method3();
}
// and so on for next methods ..









share|improve this question
























  • Use a nested set of conditional operators. It won't be legible but it will work.
    – nicomp
    Dec 21 '18 at 19:46










  • Do you need to return flag after the last method has been executed?
    – Federico Peralta Schaffner
    Dec 21 '18 at 19:50






  • 1




    "best" by what metric?
    – TylerH
    Dec 21 '18 at 22:52
















11














What is the best possible way to chain methods together? In my scenario, there are four methods. If the output of the first method is true, it has to call the second method.



For Example:



flag = method1();
if (flag){
flag = method2();
}
if (flag){
method3();
}
// and so on for next methods ..









share|improve this question
























  • Use a nested set of conditional operators. It won't be legible but it will work.
    – nicomp
    Dec 21 '18 at 19:46










  • Do you need to return flag after the last method has been executed?
    – Federico Peralta Schaffner
    Dec 21 '18 at 19:50






  • 1




    "best" by what metric?
    – TylerH
    Dec 21 '18 at 22:52














11












11








11


0





What is the best possible way to chain methods together? In my scenario, there are four methods. If the output of the first method is true, it has to call the second method.



For Example:



flag = method1();
if (flag){
flag = method2();
}
if (flag){
method3();
}
// and so on for next methods ..









share|improve this question















What is the best possible way to chain methods together? In my scenario, there are four methods. If the output of the first method is true, it has to call the second method.



For Example:



flag = method1();
if (flag){
flag = method2();
}
if (flag){
method3();
}
// and so on for next methods ..






java






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 21 '18 at 20:00









Federico Peralta Schaffner

22k43370




22k43370










asked Dec 21 '18 at 19:42









user10821509

624




624












  • Use a nested set of conditional operators. It won't be legible but it will work.
    – nicomp
    Dec 21 '18 at 19:46










  • Do you need to return flag after the last method has been executed?
    – Federico Peralta Schaffner
    Dec 21 '18 at 19:50






  • 1




    "best" by what metric?
    – TylerH
    Dec 21 '18 at 22:52


















  • Use a nested set of conditional operators. It won't be legible but it will work.
    – nicomp
    Dec 21 '18 at 19:46










  • Do you need to return flag after the last method has been executed?
    – Federico Peralta Schaffner
    Dec 21 '18 at 19:50






  • 1




    "best" by what metric?
    – TylerH
    Dec 21 '18 at 22:52
















Use a nested set of conditional operators. It won't be legible but it will work.
– nicomp
Dec 21 '18 at 19:46




Use a nested set of conditional operators. It won't be legible but it will work.
– nicomp
Dec 21 '18 at 19:46












Do you need to return flag after the last method has been executed?
– Federico Peralta Schaffner
Dec 21 '18 at 19:50




Do you need to return flag after the last method has been executed?
– Federico Peralta Schaffner
Dec 21 '18 at 19:50




1




1




"best" by what metric?
– TylerH
Dec 21 '18 at 22:52




"best" by what metric?
– TylerH
Dec 21 '18 at 22:52












7 Answers
7






active

oldest

votes


















16














Use the && logical AND operator:



if (method1() && method2() && method3() && method4()) {
........
}


Java evaluates this condition from left to right.

If any of the methods returns false, then the evaluation stops, because the final result is false (Short Circuit Evaluation).






share|improve this answer























  • That doesn't put anything in flag.
    – nicomp
    Dec 21 '18 at 19:47






  • 8




    The flag is not needed this way.
    – forpas
    Dec 21 '18 at 19:48










  • This is hacker way :) But makes code, probably, hard to read.
    – Koziołek
    Dec 21 '18 at 19:54






  • 6




    @Koziołek If you know what you're doing, it is not hard to read.
    – forpas
    Dec 21 '18 at 19:55






  • 2




    @Koziołek as I said If you know what you're doing....
    – forpas
    Dec 21 '18 at 20:01



















6














if flag is not needed somewhere later on then @forpas's answer is the way to go otherwise I'd do:



flag = method1() && method2() && method3() && method4()





share|improve this answer































    3














    To use the short circuit but keep it more readable, you can do



    boolean flag = method1();
    flag = flag && method2();
    flag = flag && method3();


    And so on. Note you can't use flag &= methodX() because that doesn't short circuit.






    share|improve this answer































      3














      First and foremost I'd like to state that this answer is by no means more efficient than this answer or this answer rather it's just another way in which you can accomplish the task at hand while maintaining the short-shircuting requirement of yours.



      So first step is to create a method as such:



      public static boolean apply(BooleanSupplier... funcs){
      return Arrays.stream(funcs).allMatch(BooleanSupplier::getAsBoolean);
      }


      This function consumes any number of functions that have the signature () -> boolean i.e. a supplier (a function taking no inputs and returns a boolean result).



      Then you can call it as follows using a method reference:



      if(apply(Main::method1, 
      Main::method2,
      Main::method3,
      Main::method4)){ ... };


      Where Main is the class where the methods are defined.



      or lambda:



      if(apply(() -> method1(), 
      () -> method2(),
      () -> method3(),
      () -> method4())){ ... };





      share|improve this answer























      • This one is likely more readable if the list is more than 3 or 4.
        – jpmc26
        Dec 22 '18 at 1:18





















      2














      Nested conditional operators would solve this:



      flag = method1() ? (method2() ? method3(): false): false;





      share|improve this answer





















      • nice +1, you can simplify it to flag = method1() && (method2() && method3());. assuming flag is needed after the condtions then this is a good approach.
        – Aomine
        Dec 21 '18 at 19:51












      • @Aomine I think that would be better answer than this
        – eis
        Dec 21 '18 at 19:52










      • @Aomine but the conditional operator get so little respect! :)
        – nicomp
        Dec 21 '18 at 19:53






      • 1




        @nicomp right, i'll post it as an answer then :). #eis sure thing.
        – Aomine
        Dec 21 '18 at 19:54



















      0














      You can use the && operator :



      boolean flag = method1() && method2() && method3() && method4() && ...;


      Do whatever with flag.






      share|improve this answer





























        0














        incase the methods truth values have to be saved or other side-effects have to happen:






        boolean flag;
        switch(1){
        case 1: flag = method1();
        //additional code goes here

        if(!flag) break;
        case 2: flag = method2();
        //additional code goes here

        if(!flag) break;
        ....
        default: flag = method10();
        if(!flag) break;
        }





        you shouldn't want this and it's... unexpected.
        but this is a readable way of adding additional statements to each code path.



        otherwise: do it differently or fall back on ifs.






        share|improve this answer





















          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53890008%2fconditional-method-chaining-in-java%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          7 Answers
          7






          active

          oldest

          votes








          7 Answers
          7






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          16














          Use the && logical AND operator:



          if (method1() && method2() && method3() && method4()) {
          ........
          }


          Java evaluates this condition from left to right.

          If any of the methods returns false, then the evaluation stops, because the final result is false (Short Circuit Evaluation).






          share|improve this answer























          • That doesn't put anything in flag.
            – nicomp
            Dec 21 '18 at 19:47






          • 8




            The flag is not needed this way.
            – forpas
            Dec 21 '18 at 19:48










          • This is hacker way :) But makes code, probably, hard to read.
            – Koziołek
            Dec 21 '18 at 19:54






          • 6




            @Koziołek If you know what you're doing, it is not hard to read.
            – forpas
            Dec 21 '18 at 19:55






          • 2




            @Koziołek as I said If you know what you're doing....
            – forpas
            Dec 21 '18 at 20:01
















          16














          Use the && logical AND operator:



          if (method1() && method2() && method3() && method4()) {
          ........
          }


          Java evaluates this condition from left to right.

          If any of the methods returns false, then the evaluation stops, because the final result is false (Short Circuit Evaluation).






          share|improve this answer























          • That doesn't put anything in flag.
            – nicomp
            Dec 21 '18 at 19:47






          • 8




            The flag is not needed this way.
            – forpas
            Dec 21 '18 at 19:48










          • This is hacker way :) But makes code, probably, hard to read.
            – Koziołek
            Dec 21 '18 at 19:54






          • 6




            @Koziołek If you know what you're doing, it is not hard to read.
            – forpas
            Dec 21 '18 at 19:55






          • 2




            @Koziołek as I said If you know what you're doing....
            – forpas
            Dec 21 '18 at 20:01














          16












          16








          16






          Use the && logical AND operator:



          if (method1() && method2() && method3() && method4()) {
          ........
          }


          Java evaluates this condition from left to right.

          If any of the methods returns false, then the evaluation stops, because the final result is false (Short Circuit Evaluation).






          share|improve this answer














          Use the && logical AND operator:



          if (method1() && method2() && method3() && method4()) {
          ........
          }


          Java evaluates this condition from left to right.

          If any of the methods returns false, then the evaluation stops, because the final result is false (Short Circuit Evaluation).







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 21 '18 at 20:02

























          answered Dec 21 '18 at 19:46









          forpas

          8,8441421




          8,8441421












          • That doesn't put anything in flag.
            – nicomp
            Dec 21 '18 at 19:47






          • 8




            The flag is not needed this way.
            – forpas
            Dec 21 '18 at 19:48










          • This is hacker way :) But makes code, probably, hard to read.
            – Koziołek
            Dec 21 '18 at 19:54






          • 6




            @Koziołek If you know what you're doing, it is not hard to read.
            – forpas
            Dec 21 '18 at 19:55






          • 2




            @Koziołek as I said If you know what you're doing....
            – forpas
            Dec 21 '18 at 20:01


















          • That doesn't put anything in flag.
            – nicomp
            Dec 21 '18 at 19:47






          • 8




            The flag is not needed this way.
            – forpas
            Dec 21 '18 at 19:48










          • This is hacker way :) But makes code, probably, hard to read.
            – Koziołek
            Dec 21 '18 at 19:54






          • 6




            @Koziołek If you know what you're doing, it is not hard to read.
            – forpas
            Dec 21 '18 at 19:55






          • 2




            @Koziołek as I said If you know what you're doing....
            – forpas
            Dec 21 '18 at 20:01
















          That doesn't put anything in flag.
          – nicomp
          Dec 21 '18 at 19:47




          That doesn't put anything in flag.
          – nicomp
          Dec 21 '18 at 19:47




          8




          8




          The flag is not needed this way.
          – forpas
          Dec 21 '18 at 19:48




          The flag is not needed this way.
          – forpas
          Dec 21 '18 at 19:48












          This is hacker way :) But makes code, probably, hard to read.
          – Koziołek
          Dec 21 '18 at 19:54




          This is hacker way :) But makes code, probably, hard to read.
          – Koziołek
          Dec 21 '18 at 19:54




          6




          6




          @Koziołek If you know what you're doing, it is not hard to read.
          – forpas
          Dec 21 '18 at 19:55




          @Koziołek If you know what you're doing, it is not hard to read.
          – forpas
          Dec 21 '18 at 19:55




          2




          2




          @Koziołek as I said If you know what you're doing....
          – forpas
          Dec 21 '18 at 20:01




          @Koziołek as I said If you know what you're doing....
          – forpas
          Dec 21 '18 at 20:01













          6














          if flag is not needed somewhere later on then @forpas's answer is the way to go otherwise I'd do:



          flag = method1() && method2() && method3() && method4()





          share|improve this answer




























            6














            if flag is not needed somewhere later on then @forpas's answer is the way to go otherwise I'd do:



            flag = method1() && method2() && method3() && method4()





            share|improve this answer


























              6












              6








              6






              if flag is not needed somewhere later on then @forpas's answer is the way to go otherwise I'd do:



              flag = method1() && method2() && method3() && method4()





              share|improve this answer














              if flag is not needed somewhere later on then @forpas's answer is the way to go otherwise I'd do:



              flag = method1() && method2() && method3() && method4()






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Dec 21 '18 at 20:00

























              answered Dec 21 '18 at 19:54









              Aomine

              40.3k73870




              40.3k73870























                  3














                  To use the short circuit but keep it more readable, you can do



                  boolean flag = method1();
                  flag = flag && method2();
                  flag = flag && method3();


                  And so on. Note you can't use flag &= methodX() because that doesn't short circuit.






                  share|improve this answer




























                    3














                    To use the short circuit but keep it more readable, you can do



                    boolean flag = method1();
                    flag = flag && method2();
                    flag = flag && method3();


                    And so on. Note you can't use flag &= methodX() because that doesn't short circuit.






                    share|improve this answer


























                      3












                      3








                      3






                      To use the short circuit but keep it more readable, you can do



                      boolean flag = method1();
                      flag = flag && method2();
                      flag = flag && method3();


                      And so on. Note you can't use flag &= methodX() because that doesn't short circuit.






                      share|improve this answer














                      To use the short circuit but keep it more readable, you can do



                      boolean flag = method1();
                      flag = flag && method2();
                      flag = flag && method3();


                      And so on. Note you can't use flag &= methodX() because that doesn't short circuit.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Dec 21 '18 at 21:30

























                      answered Dec 21 '18 at 20:05









                      daniu

                      7,28021635




                      7,28021635























                          3














                          First and foremost I'd like to state that this answer is by no means more efficient than this answer or this answer rather it's just another way in which you can accomplish the task at hand while maintaining the short-shircuting requirement of yours.



                          So first step is to create a method as such:



                          public static boolean apply(BooleanSupplier... funcs){
                          return Arrays.stream(funcs).allMatch(BooleanSupplier::getAsBoolean);
                          }


                          This function consumes any number of functions that have the signature () -> boolean i.e. a supplier (a function taking no inputs and returns a boolean result).



                          Then you can call it as follows using a method reference:



                          if(apply(Main::method1, 
                          Main::method2,
                          Main::method3,
                          Main::method4)){ ... };


                          Where Main is the class where the methods are defined.



                          or lambda:



                          if(apply(() -> method1(), 
                          () -> method2(),
                          () -> method3(),
                          () -> method4())){ ... };





                          share|improve this answer























                          • This one is likely more readable if the list is more than 3 or 4.
                            – jpmc26
                            Dec 22 '18 at 1:18


















                          3














                          First and foremost I'd like to state that this answer is by no means more efficient than this answer or this answer rather it's just another way in which you can accomplish the task at hand while maintaining the short-shircuting requirement of yours.



                          So first step is to create a method as such:



                          public static boolean apply(BooleanSupplier... funcs){
                          return Arrays.stream(funcs).allMatch(BooleanSupplier::getAsBoolean);
                          }


                          This function consumes any number of functions that have the signature () -> boolean i.e. a supplier (a function taking no inputs and returns a boolean result).



                          Then you can call it as follows using a method reference:



                          if(apply(Main::method1, 
                          Main::method2,
                          Main::method3,
                          Main::method4)){ ... };


                          Where Main is the class where the methods are defined.



                          or lambda:



                          if(apply(() -> method1(), 
                          () -> method2(),
                          () -> method3(),
                          () -> method4())){ ... };





                          share|improve this answer























                          • This one is likely more readable if the list is more than 3 or 4.
                            – jpmc26
                            Dec 22 '18 at 1:18
















                          3












                          3








                          3






                          First and foremost I'd like to state that this answer is by no means more efficient than this answer or this answer rather it's just another way in which you can accomplish the task at hand while maintaining the short-shircuting requirement of yours.



                          So first step is to create a method as such:



                          public static boolean apply(BooleanSupplier... funcs){
                          return Arrays.stream(funcs).allMatch(BooleanSupplier::getAsBoolean);
                          }


                          This function consumes any number of functions that have the signature () -> boolean i.e. a supplier (a function taking no inputs and returns a boolean result).



                          Then you can call it as follows using a method reference:



                          if(apply(Main::method1, 
                          Main::method2,
                          Main::method3,
                          Main::method4)){ ... };


                          Where Main is the class where the methods are defined.



                          or lambda:



                          if(apply(() -> method1(), 
                          () -> method2(),
                          () -> method3(),
                          () -> method4())){ ... };





                          share|improve this answer














                          First and foremost I'd like to state that this answer is by no means more efficient than this answer or this answer rather it's just another way in which you can accomplish the task at hand while maintaining the short-shircuting requirement of yours.



                          So first step is to create a method as such:



                          public static boolean apply(BooleanSupplier... funcs){
                          return Arrays.stream(funcs).allMatch(BooleanSupplier::getAsBoolean);
                          }


                          This function consumes any number of functions that have the signature () -> boolean i.e. a supplier (a function taking no inputs and returns a boolean result).



                          Then you can call it as follows using a method reference:



                          if(apply(Main::method1, 
                          Main::method2,
                          Main::method3,
                          Main::method4)){ ... };


                          Where Main is the class where the methods are defined.



                          or lambda:



                          if(apply(() -> method1(), 
                          () -> method2(),
                          () -> method3(),
                          () -> method4())){ ... };






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Dec 21 '18 at 22:44

























                          answered Dec 21 '18 at 22:27









                          Aomine

                          40.3k73870




                          40.3k73870












                          • This one is likely more readable if the list is more than 3 or 4.
                            – jpmc26
                            Dec 22 '18 at 1:18




















                          • This one is likely more readable if the list is more than 3 or 4.
                            – jpmc26
                            Dec 22 '18 at 1:18


















                          This one is likely more readable if the list is more than 3 or 4.
                          – jpmc26
                          Dec 22 '18 at 1:18






                          This one is likely more readable if the list is more than 3 or 4.
                          – jpmc26
                          Dec 22 '18 at 1:18













                          2














                          Nested conditional operators would solve this:



                          flag = method1() ? (method2() ? method3(): false): false;





                          share|improve this answer





















                          • nice +1, you can simplify it to flag = method1() && (method2() && method3());. assuming flag is needed after the condtions then this is a good approach.
                            – Aomine
                            Dec 21 '18 at 19:51












                          • @Aomine I think that would be better answer than this
                            – eis
                            Dec 21 '18 at 19:52










                          • @Aomine but the conditional operator get so little respect! :)
                            – nicomp
                            Dec 21 '18 at 19:53






                          • 1




                            @nicomp right, i'll post it as an answer then :). #eis sure thing.
                            – Aomine
                            Dec 21 '18 at 19:54
















                          2














                          Nested conditional operators would solve this:



                          flag = method1() ? (method2() ? method3(): false): false;





                          share|improve this answer





















                          • nice +1, you can simplify it to flag = method1() && (method2() && method3());. assuming flag is needed after the condtions then this is a good approach.
                            – Aomine
                            Dec 21 '18 at 19:51












                          • @Aomine I think that would be better answer than this
                            – eis
                            Dec 21 '18 at 19:52










                          • @Aomine but the conditional operator get so little respect! :)
                            – nicomp
                            Dec 21 '18 at 19:53






                          • 1




                            @nicomp right, i'll post it as an answer then :). #eis sure thing.
                            – Aomine
                            Dec 21 '18 at 19:54














                          2












                          2








                          2






                          Nested conditional operators would solve this:



                          flag = method1() ? (method2() ? method3(): false): false;





                          share|improve this answer












                          Nested conditional operators would solve this:



                          flag = method1() ? (method2() ? method3(): false): false;






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Dec 21 '18 at 19:50









                          nicomp

                          2,51921230




                          2,51921230












                          • nice +1, you can simplify it to flag = method1() && (method2() && method3());. assuming flag is needed after the condtions then this is a good approach.
                            – Aomine
                            Dec 21 '18 at 19:51












                          • @Aomine I think that would be better answer than this
                            – eis
                            Dec 21 '18 at 19:52










                          • @Aomine but the conditional operator get so little respect! :)
                            – nicomp
                            Dec 21 '18 at 19:53






                          • 1




                            @nicomp right, i'll post it as an answer then :). #eis sure thing.
                            – Aomine
                            Dec 21 '18 at 19:54


















                          • nice +1, you can simplify it to flag = method1() && (method2() && method3());. assuming flag is needed after the condtions then this is a good approach.
                            – Aomine
                            Dec 21 '18 at 19:51












                          • @Aomine I think that would be better answer than this
                            – eis
                            Dec 21 '18 at 19:52










                          • @Aomine but the conditional operator get so little respect! :)
                            – nicomp
                            Dec 21 '18 at 19:53






                          • 1




                            @nicomp right, i'll post it as an answer then :). #eis sure thing.
                            – Aomine
                            Dec 21 '18 at 19:54
















                          nice +1, you can simplify it to flag = method1() && (method2() && method3());. assuming flag is needed after the condtions then this is a good approach.
                          – Aomine
                          Dec 21 '18 at 19:51






                          nice +1, you can simplify it to flag = method1() && (method2() && method3());. assuming flag is needed after the condtions then this is a good approach.
                          – Aomine
                          Dec 21 '18 at 19:51














                          @Aomine I think that would be better answer than this
                          – eis
                          Dec 21 '18 at 19:52




                          @Aomine I think that would be better answer than this
                          – eis
                          Dec 21 '18 at 19:52












                          @Aomine but the conditional operator get so little respect! :)
                          – nicomp
                          Dec 21 '18 at 19:53




                          @Aomine but the conditional operator get so little respect! :)
                          – nicomp
                          Dec 21 '18 at 19:53




                          1




                          1




                          @nicomp right, i'll post it as an answer then :). #eis sure thing.
                          – Aomine
                          Dec 21 '18 at 19:54




                          @nicomp right, i'll post it as an answer then :). #eis sure thing.
                          – Aomine
                          Dec 21 '18 at 19:54











                          0














                          You can use the && operator :



                          boolean flag = method1() && method2() && method3() && method4() && ...;


                          Do whatever with flag.






                          share|improve this answer


























                            0














                            You can use the && operator :



                            boolean flag = method1() && method2() && method3() && method4() && ...;


                            Do whatever with flag.






                            share|improve this answer
























                              0












                              0








                              0






                              You can use the && operator :



                              boolean flag = method1() && method2() && method3() && method4() && ...;


                              Do whatever with flag.






                              share|improve this answer












                              You can use the && operator :



                              boolean flag = method1() && method2() && method3() && method4() && ...;


                              Do whatever with flag.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Dec 21 '18 at 20:03









                              fastcodejava

                              24k19109162




                              24k19109162























                                  0














                                  incase the methods truth values have to be saved or other side-effects have to happen:






                                  boolean flag;
                                  switch(1){
                                  case 1: flag = method1();
                                  //additional code goes here

                                  if(!flag) break;
                                  case 2: flag = method2();
                                  //additional code goes here

                                  if(!flag) break;
                                  ....
                                  default: flag = method10();
                                  if(!flag) break;
                                  }





                                  you shouldn't want this and it's... unexpected.
                                  but this is a readable way of adding additional statements to each code path.



                                  otherwise: do it differently or fall back on ifs.






                                  share|improve this answer


























                                    0














                                    incase the methods truth values have to be saved or other side-effects have to happen:






                                    boolean flag;
                                    switch(1){
                                    case 1: flag = method1();
                                    //additional code goes here

                                    if(!flag) break;
                                    case 2: flag = method2();
                                    //additional code goes here

                                    if(!flag) break;
                                    ....
                                    default: flag = method10();
                                    if(!flag) break;
                                    }





                                    you shouldn't want this and it's... unexpected.
                                    but this is a readable way of adding additional statements to each code path.



                                    otherwise: do it differently or fall back on ifs.






                                    share|improve this answer
























                                      0












                                      0








                                      0






                                      incase the methods truth values have to be saved or other side-effects have to happen:






                                      boolean flag;
                                      switch(1){
                                      case 1: flag = method1();
                                      //additional code goes here

                                      if(!flag) break;
                                      case 2: flag = method2();
                                      //additional code goes here

                                      if(!flag) break;
                                      ....
                                      default: flag = method10();
                                      if(!flag) break;
                                      }





                                      you shouldn't want this and it's... unexpected.
                                      but this is a readable way of adding additional statements to each code path.



                                      otherwise: do it differently or fall back on ifs.






                                      share|improve this answer












                                      incase the methods truth values have to be saved or other side-effects have to happen:






                                      boolean flag;
                                      switch(1){
                                      case 1: flag = method1();
                                      //additional code goes here

                                      if(!flag) break;
                                      case 2: flag = method2();
                                      //additional code goes here

                                      if(!flag) break;
                                      ....
                                      default: flag = method10();
                                      if(!flag) break;
                                      }





                                      you shouldn't want this and it's... unexpected.
                                      but this is a readable way of adding additional statements to each code path.



                                      otherwise: do it differently or fall back on ifs.






                                      boolean flag;
                                      switch(1){
                                      case 1: flag = method1();
                                      //additional code goes here

                                      if(!flag) break;
                                      case 2: flag = method2();
                                      //additional code goes here

                                      if(!flag) break;
                                      ....
                                      default: flag = method10();
                                      if(!flag) break;
                                      }





                                      boolean flag;
                                      switch(1){
                                      case 1: flag = method1();
                                      //additional code goes here

                                      if(!flag) break;
                                      case 2: flag = method2();
                                      //additional code goes here

                                      if(!flag) break;
                                      ....
                                      default: flag = method10();
                                      if(!flag) break;
                                      }






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Dec 21 '18 at 21:13









                                      RecurringError

                                      623




                                      623






























                                          draft saved

                                          draft discarded




















































                                          Thanks for contributing an answer to Stack Overflow!


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid



                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.


                                          To learn more, see our tips on writing great answers.





                                          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                          Please pay close attention to the following guidance:


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid



                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.


                                          To learn more, see our tips on writing great answers.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function () {
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53890008%2fconditional-method-chaining-in-java%23new-answer', 'question_page');
                                          }
                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          Popular posts from this blog

                                          Morgemoulin

                                          Scott Moir

                                          Souastre