Can I always merge two conditions into one row in Kotlin?











up vote
1
down vote

favorite












The Code A is good, I hope to optimize it, so I write the Code B.



I'm not sure whether the Code B is always correct.



It will be OK if Kotlin check clipboard.hasPrimaryClip() first, then check clipboard.primaryClipDescription.hasMimeType(MIMETYPE_TEXT_PLAIN) next.



It maybe crash if Kotlin check clipboard.primaryClipDescription.hasMimeType(MIMETYPE_TEXT_PLAIN) first, the check clipboard.hasPrimaryClip() next, right?



Code A



clipboard.addPrimaryClipChangedListener {
if (clipboard.hasPrimaryClip() ) {
if (clipboard.primaryClipDescription.hasMimeType(MIMETYPE_TEXT_PLAIN)) {

}
}
}


Code B



clipboard.addPrimaryClipChangedListener {
if (clipboard.hasPrimaryClip() && clipboard.primaryClipDescription.hasMimeType(MIMETYPE_TEXT_PLAIN) ) {

}
}









share|improve this question






















  • if you want to search for this, the term is "short circuiting"
    – Tim Castelijns
    2 days ago















up vote
1
down vote

favorite












The Code A is good, I hope to optimize it, so I write the Code B.



I'm not sure whether the Code B is always correct.



It will be OK if Kotlin check clipboard.hasPrimaryClip() first, then check clipboard.primaryClipDescription.hasMimeType(MIMETYPE_TEXT_PLAIN) next.



It maybe crash if Kotlin check clipboard.primaryClipDescription.hasMimeType(MIMETYPE_TEXT_PLAIN) first, the check clipboard.hasPrimaryClip() next, right?



Code A



clipboard.addPrimaryClipChangedListener {
if (clipboard.hasPrimaryClip() ) {
if (clipboard.primaryClipDescription.hasMimeType(MIMETYPE_TEXT_PLAIN)) {

}
}
}


Code B



clipboard.addPrimaryClipChangedListener {
if (clipboard.hasPrimaryClip() && clipboard.primaryClipDescription.hasMimeType(MIMETYPE_TEXT_PLAIN) ) {

}
}









share|improve this question






















  • if you want to search for this, the term is "short circuiting"
    – Tim Castelijns
    2 days ago













up vote
1
down vote

favorite









up vote
1
down vote

favorite











The Code A is good, I hope to optimize it, so I write the Code B.



I'm not sure whether the Code B is always correct.



It will be OK if Kotlin check clipboard.hasPrimaryClip() first, then check clipboard.primaryClipDescription.hasMimeType(MIMETYPE_TEXT_PLAIN) next.



It maybe crash if Kotlin check clipboard.primaryClipDescription.hasMimeType(MIMETYPE_TEXT_PLAIN) first, the check clipboard.hasPrimaryClip() next, right?



Code A



clipboard.addPrimaryClipChangedListener {
if (clipboard.hasPrimaryClip() ) {
if (clipboard.primaryClipDescription.hasMimeType(MIMETYPE_TEXT_PLAIN)) {

}
}
}


Code B



clipboard.addPrimaryClipChangedListener {
if (clipboard.hasPrimaryClip() && clipboard.primaryClipDescription.hasMimeType(MIMETYPE_TEXT_PLAIN) ) {

}
}









share|improve this question













The Code A is good, I hope to optimize it, so I write the Code B.



I'm not sure whether the Code B is always correct.



It will be OK if Kotlin check clipboard.hasPrimaryClip() first, then check clipboard.primaryClipDescription.hasMimeType(MIMETYPE_TEXT_PLAIN) next.



It maybe crash if Kotlin check clipboard.primaryClipDescription.hasMimeType(MIMETYPE_TEXT_PLAIN) first, the check clipboard.hasPrimaryClip() next, right?



Code A



clipboard.addPrimaryClipChangedListener {
if (clipboard.hasPrimaryClip() ) {
if (clipboard.primaryClipDescription.hasMimeType(MIMETYPE_TEXT_PLAIN)) {

}
}
}


Code B



clipboard.addPrimaryClipChangedListener {
if (clipboard.hasPrimaryClip() && clipboard.primaryClipDescription.hasMimeType(MIMETYPE_TEXT_PLAIN) ) {

}
}






android kotlin






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 2 days ago









HelloCW

17342790




17342790












  • if you want to search for this, the term is "short circuiting"
    – Tim Castelijns
    2 days ago


















  • if you want to search for this, the term is "short circuiting"
    – Tim Castelijns
    2 days ago
















if you want to search for this, the term is "short circuiting"
– Tim Castelijns
2 days ago




if you want to search for this, the term is "short circuiting"
– Tim Castelijns
2 days ago












2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










As mentioned in the comments, the principle behind this is described as "short circuiting":




Short-circuit evaluation [...] is the semantics of some Boolean operators in some programming languages in which the second argument is executed or evaluated only if the first argument does not suffice to determine the value of the expression.




That means clipboard.hasPrimaryClip() will always be evaluated. If it's false
, the condition fails without looking any further. If it is true though, clipboard.primaryClipDescription.hasMimeType(MIMETYPE_TEXT_PLAIN) will be evaluated as well.






share|improve this answer




























    up vote
    2
    down vote













    if conditions run sequentially. That means it will first check the left condition and if the operator is AND and left condition return false then it won't check the right condition. So yes, you can merge two conditions.






    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',
      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%2f53203477%2fcan-i-always-merge-two-conditions-into-one-row-in-kotlin%23new-answer', 'question_page');
      }
      );

      Post as a guest
































      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      1
      down vote



      accepted










      As mentioned in the comments, the principle behind this is described as "short circuiting":




      Short-circuit evaluation [...] is the semantics of some Boolean operators in some programming languages in which the second argument is executed or evaluated only if the first argument does not suffice to determine the value of the expression.




      That means clipboard.hasPrimaryClip() will always be evaluated. If it's false
      , the condition fails without looking any further. If it is true though, clipboard.primaryClipDescription.hasMimeType(MIMETYPE_TEXT_PLAIN) will be evaluated as well.






      share|improve this answer

























        up vote
        1
        down vote



        accepted










        As mentioned in the comments, the principle behind this is described as "short circuiting":




        Short-circuit evaluation [...] is the semantics of some Boolean operators in some programming languages in which the second argument is executed or evaluated only if the first argument does not suffice to determine the value of the expression.




        That means clipboard.hasPrimaryClip() will always be evaluated. If it's false
        , the condition fails without looking any further. If it is true though, clipboard.primaryClipDescription.hasMimeType(MIMETYPE_TEXT_PLAIN) will be evaluated as well.






        share|improve this answer























          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          As mentioned in the comments, the principle behind this is described as "short circuiting":




          Short-circuit evaluation [...] is the semantics of some Boolean operators in some programming languages in which the second argument is executed or evaluated only if the first argument does not suffice to determine the value of the expression.




          That means clipboard.hasPrimaryClip() will always be evaluated. If it's false
          , the condition fails without looking any further. If it is true though, clipboard.primaryClipDescription.hasMimeType(MIMETYPE_TEXT_PLAIN) will be evaluated as well.






          share|improve this answer












          As mentioned in the comments, the principle behind this is described as "short circuiting":




          Short-circuit evaluation [...] is the semantics of some Boolean operators in some programming languages in which the second argument is executed or evaluated only if the first argument does not suffice to determine the value of the expression.




          That means clipboard.hasPrimaryClip() will always be evaluated. If it's false
          , the condition fails without looking any further. If it is true though, clipboard.primaryClipDescription.hasMimeType(MIMETYPE_TEXT_PLAIN) will be evaluated as well.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 2 days ago









          s1m0nw1

          23.3k53696




          23.3k53696
























              up vote
              2
              down vote













              if conditions run sequentially. That means it will first check the left condition and if the operator is AND and left condition return false then it won't check the right condition. So yes, you can merge two conditions.






              share|improve this answer

























                up vote
                2
                down vote













                if conditions run sequentially. That means it will first check the left condition and if the operator is AND and left condition return false then it won't check the right condition. So yes, you can merge two conditions.






                share|improve this answer























                  up vote
                  2
                  down vote










                  up vote
                  2
                  down vote









                  if conditions run sequentially. That means it will first check the left condition and if the operator is AND and left condition return false then it won't check the right condition. So yes, you can merge two conditions.






                  share|improve this answer












                  if conditions run sequentially. That means it will first check the left condition and if the operator is AND and left condition return false then it won't check the right condition. So yes, you can merge two conditions.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 2 days ago









                  Kunu

                  2,37631747




                  2,37631747






























                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53203477%2fcan-i-always-merge-two-conditions-into-one-row-in-kotlin%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest




















































































                      Popular posts from this blog

                      Landwehr

                      Reims

                      Javascript gets undefined on array