While loop - how to remove code duplication












-1















It's not the first time I find myself in the following situation:



bool a = some_very_long_computation;
bool b = another_very_long_computation;
while (a && b) {
...
a = some_very_long_computation;
b = another_very_long_computation;
}


I don't want to compute everything in while condition, since computations are long and I want to give them appropriate names.
I don't want to create helper functions, because computation uses many local variables, and passing them all will make the code much less readable (and it will be some_huge_call).



It's unknown whether loop body will be executed at least once.



What is a good pattern in such situation? Currently I face it in C++, but I've encountered this in other languages as well. I can solve it by using additional variable isFirstPass, but it looks ugly (and, I guess, will cause some warnings):



bool a, b;
bool isFirstPass = true;
do {
if (!isFirstPass) {
...
} else {
isFirstPass = false;
}
a = some_very_long_computation;
b = another_very_long_computation;
} while (a && b);









share|improve this question




















  • 1





    I do believe helper functions are still the right way to do; but what about macros?

    – raina77ow
    Jan 18 at 18:57






  • 1





    Macros - absolutely no. I don't want to pollute defines for some local stuff (and I can accidentally override some defines).

    – dyukha
    Jan 18 at 18:59











  • Meh, move it into its own method returning a bool.

    – Will
    Jan 18 at 18:59











  • Please update your first example making it clear how/if the first pass is different

    – Lyndon White
    Jan 19 at 0:24











  • @LyndonWhite, Sorry, I don't understand you. It's not different, it's the same pass as the others.

    – dyukha
    Jan 19 at 0:25


















-1















It's not the first time I find myself in the following situation:



bool a = some_very_long_computation;
bool b = another_very_long_computation;
while (a && b) {
...
a = some_very_long_computation;
b = another_very_long_computation;
}


I don't want to compute everything in while condition, since computations are long and I want to give them appropriate names.
I don't want to create helper functions, because computation uses many local variables, and passing them all will make the code much less readable (and it will be some_huge_call).



It's unknown whether loop body will be executed at least once.



What is a good pattern in such situation? Currently I face it in C++, but I've encountered this in other languages as well. I can solve it by using additional variable isFirstPass, but it looks ugly (and, I guess, will cause some warnings):



bool a, b;
bool isFirstPass = true;
do {
if (!isFirstPass) {
...
} else {
isFirstPass = false;
}
a = some_very_long_computation;
b = another_very_long_computation;
} while (a && b);









share|improve this question




















  • 1





    I do believe helper functions are still the right way to do; but what about macros?

    – raina77ow
    Jan 18 at 18:57






  • 1





    Macros - absolutely no. I don't want to pollute defines for some local stuff (and I can accidentally override some defines).

    – dyukha
    Jan 18 at 18:59











  • Meh, move it into its own method returning a bool.

    – Will
    Jan 18 at 18:59











  • Please update your first example making it clear how/if the first pass is different

    – Lyndon White
    Jan 19 at 0:24











  • @LyndonWhite, Sorry, I don't understand you. It's not different, it's the same pass as the others.

    – dyukha
    Jan 19 at 0:25
















-1












-1








-1








It's not the first time I find myself in the following situation:



bool a = some_very_long_computation;
bool b = another_very_long_computation;
while (a && b) {
...
a = some_very_long_computation;
b = another_very_long_computation;
}


I don't want to compute everything in while condition, since computations are long and I want to give them appropriate names.
I don't want to create helper functions, because computation uses many local variables, and passing them all will make the code much less readable (and it will be some_huge_call).



It's unknown whether loop body will be executed at least once.



What is a good pattern in such situation? Currently I face it in C++, but I've encountered this in other languages as well. I can solve it by using additional variable isFirstPass, but it looks ugly (and, I guess, will cause some warnings):



bool a, b;
bool isFirstPass = true;
do {
if (!isFirstPass) {
...
} else {
isFirstPass = false;
}
a = some_very_long_computation;
b = another_very_long_computation;
} while (a && b);









share|improve this question
















It's not the first time I find myself in the following situation:



bool a = some_very_long_computation;
bool b = another_very_long_computation;
while (a && b) {
...
a = some_very_long_computation;
b = another_very_long_computation;
}


I don't want to compute everything in while condition, since computations are long and I want to give them appropriate names.
I don't want to create helper functions, because computation uses many local variables, and passing them all will make the code much less readable (and it will be some_huge_call).



It's unknown whether loop body will be executed at least once.



What is a good pattern in such situation? Currently I face it in C++, but I've encountered this in other languages as well. I can solve it by using additional variable isFirstPass, but it looks ugly (and, I guess, will cause some warnings):



bool a, b;
bool isFirstPass = true;
do {
if (!isFirstPass) {
...
} else {
isFirstPass = false;
}
a = some_very_long_computation;
b = another_very_long_computation;
} while (a && b);






c++ duplicates code-duplication






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 19 at 0:39









melpomene

59.6k54692




59.6k54692










asked Jan 18 at 18:55









dyukhadyukha

507310




507310








  • 1





    I do believe helper functions are still the right way to do; but what about macros?

    – raina77ow
    Jan 18 at 18:57






  • 1





    Macros - absolutely no. I don't want to pollute defines for some local stuff (and I can accidentally override some defines).

    – dyukha
    Jan 18 at 18:59











  • Meh, move it into its own method returning a bool.

    – Will
    Jan 18 at 18:59











  • Please update your first example making it clear how/if the first pass is different

    – Lyndon White
    Jan 19 at 0:24











  • @LyndonWhite, Sorry, I don't understand you. It's not different, it's the same pass as the others.

    – dyukha
    Jan 19 at 0:25
















  • 1





    I do believe helper functions are still the right way to do; but what about macros?

    – raina77ow
    Jan 18 at 18:57






  • 1





    Macros - absolutely no. I don't want to pollute defines for some local stuff (and I can accidentally override some defines).

    – dyukha
    Jan 18 at 18:59











  • Meh, move it into its own method returning a bool.

    – Will
    Jan 18 at 18:59











  • Please update your first example making it clear how/if the first pass is different

    – Lyndon White
    Jan 19 at 0:24











  • @LyndonWhite, Sorry, I don't understand you. It's not different, it's the same pass as the others.

    – dyukha
    Jan 19 at 0:25










1




1





I do believe helper functions are still the right way to do; but what about macros?

– raina77ow
Jan 18 at 18:57





I do believe helper functions are still the right way to do; but what about macros?

– raina77ow
Jan 18 at 18:57




1




1





Macros - absolutely no. I don't want to pollute defines for some local stuff (and I can accidentally override some defines).

– dyukha
Jan 18 at 18:59





Macros - absolutely no. I don't want to pollute defines for some local stuff (and I can accidentally override some defines).

– dyukha
Jan 18 at 18:59













Meh, move it into its own method returning a bool.

– Will
Jan 18 at 18:59





Meh, move it into its own method returning a bool.

– Will
Jan 18 at 18:59













Please update your first example making it clear how/if the first pass is different

– Lyndon White
Jan 19 at 0:24





Please update your first example making it clear how/if the first pass is different

– Lyndon White
Jan 19 at 0:24













@LyndonWhite, Sorry, I don't understand you. It's not different, it's the same pass as the others.

– dyukha
Jan 19 at 0:25







@LyndonWhite, Sorry, I don't understand you. It's not different, it's the same pass as the others.

– dyukha
Jan 19 at 0:25














1 Answer
1






active

oldest

votes


















3














The direct simplification of your code is:



while (
some_very_long_computation &&
another_very_long_computation
) {
...
}


If you want to keep the variables a and b:



bool a, b;
while (
(a = some_very_long_computation) &&
(b = another_very_long_computation)
) {
...
}


If you don't want to put the conditions into the while condition:



while (true) {
bool a = some_very_long_computation;
bool b = another_very_long_computation;
if (!(a && b)) {
break;
}
...
}


You could also create helper lambdas (which have access to local variables):



auto fa = [&]() { return some_very_long_computation; };
auto fb = [&]() { return another_very_long_computation; };
while (fa() && fb()) {
...
}





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%2f54259922%2fwhile-loop-how-to-remove-code-duplication%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    The direct simplification of your code is:



    while (
    some_very_long_computation &&
    another_very_long_computation
    ) {
    ...
    }


    If you want to keep the variables a and b:



    bool a, b;
    while (
    (a = some_very_long_computation) &&
    (b = another_very_long_computation)
    ) {
    ...
    }


    If you don't want to put the conditions into the while condition:



    while (true) {
    bool a = some_very_long_computation;
    bool b = another_very_long_computation;
    if (!(a && b)) {
    break;
    }
    ...
    }


    You could also create helper lambdas (which have access to local variables):



    auto fa = [&]() { return some_very_long_computation; };
    auto fb = [&]() { return another_very_long_computation; };
    while (fa() && fb()) {
    ...
    }





    share|improve this answer




























      3














      The direct simplification of your code is:



      while (
      some_very_long_computation &&
      another_very_long_computation
      ) {
      ...
      }


      If you want to keep the variables a and b:



      bool a, b;
      while (
      (a = some_very_long_computation) &&
      (b = another_very_long_computation)
      ) {
      ...
      }


      If you don't want to put the conditions into the while condition:



      while (true) {
      bool a = some_very_long_computation;
      bool b = another_very_long_computation;
      if (!(a && b)) {
      break;
      }
      ...
      }


      You could also create helper lambdas (which have access to local variables):



      auto fa = [&]() { return some_very_long_computation; };
      auto fb = [&]() { return another_very_long_computation; };
      while (fa() && fb()) {
      ...
      }





      share|improve this answer


























        3












        3








        3







        The direct simplification of your code is:



        while (
        some_very_long_computation &&
        another_very_long_computation
        ) {
        ...
        }


        If you want to keep the variables a and b:



        bool a, b;
        while (
        (a = some_very_long_computation) &&
        (b = another_very_long_computation)
        ) {
        ...
        }


        If you don't want to put the conditions into the while condition:



        while (true) {
        bool a = some_very_long_computation;
        bool b = another_very_long_computation;
        if (!(a && b)) {
        break;
        }
        ...
        }


        You could also create helper lambdas (which have access to local variables):



        auto fa = [&]() { return some_very_long_computation; };
        auto fb = [&]() { return another_very_long_computation; };
        while (fa() && fb()) {
        ...
        }





        share|improve this answer













        The direct simplification of your code is:



        while (
        some_very_long_computation &&
        another_very_long_computation
        ) {
        ...
        }


        If you want to keep the variables a and b:



        bool a, b;
        while (
        (a = some_very_long_computation) &&
        (b = another_very_long_computation)
        ) {
        ...
        }


        If you don't want to put the conditions into the while condition:



        while (true) {
        bool a = some_very_long_computation;
        bool b = another_very_long_computation;
        if (!(a && b)) {
        break;
        }
        ...
        }


        You could also create helper lambdas (which have access to local variables):



        auto fa = [&]() { return some_very_long_computation; };
        auto fb = [&]() { return another_very_long_computation; };
        while (fa() && fb()) {
        ...
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 19 at 0:38









        melpomenemelpomene

        59.6k54692




        59.6k54692






























            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54259922%2fwhile-loop-how-to-remove-code-duplication%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

            Homophylophilia

            Updating UILabel text programmatically using a function

            Cloud Functions - OpenCV Videocapture Read method fails for larger files from cloud storage