How do I refactor duplicate control structures?











up vote
0
down vote

favorite












I have duplicated control structures which are exactly the same, and the difference is how things happen inside. In particular, I don’t know how to deal with break, continue, and return. For example:



int f() {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (isFoo(i, j)) {
// doSomethingF1
} else {
// doSomethingF2
}
}
}
}

int g() {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (isFoo(i, j)) {
// doSomethingG1
} else {
// doSomethingG2
}
}
}
}


where doSomethings rely on i and j, and need to break or continue through the j loop, and return out of the function. How do I factor this out to get rid of the duplication?



I thought of writing a function that would take the doSomethings as arguments, but I'm not sure how to deal with the breaks, continues, or returns.



[I'm not fine with the duplication since there are some checks on i and j in f(), g(), and like four other functions that are exactly the same; the only difference is a few lines of code.]










share|improve this question
























  • @drescherjm I accidentally pressed submit when I wasn't finished writing the question yet. Oops!
    – Carl Joshua Quines
    Nov 21 at 19:45








  • 1




    Take a template or an std::function parameter pair that you call in the body of the loop in the if/else blocks.
    – palotasb
    Nov 21 at 20:37






  • 2




    btw you added some code, but there is still no continue or break in your example ;)
    – user463035818
    Nov 21 at 20:48






  • 3




    how to refactor the code depends on the part you dont show
    – user463035818
    Nov 21 at 20:50






  • 1




    Not sure what's unclear here... avoiding duplication of control structures is a valid question (with non-obvious answers).
    – Peter Ruderman
    Nov 21 at 21:07















up vote
0
down vote

favorite












I have duplicated control structures which are exactly the same, and the difference is how things happen inside. In particular, I don’t know how to deal with break, continue, and return. For example:



int f() {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (isFoo(i, j)) {
// doSomethingF1
} else {
// doSomethingF2
}
}
}
}

int g() {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (isFoo(i, j)) {
// doSomethingG1
} else {
// doSomethingG2
}
}
}
}


where doSomethings rely on i and j, and need to break or continue through the j loop, and return out of the function. How do I factor this out to get rid of the duplication?



I thought of writing a function that would take the doSomethings as arguments, but I'm not sure how to deal with the breaks, continues, or returns.



[I'm not fine with the duplication since there are some checks on i and j in f(), g(), and like four other functions that are exactly the same; the only difference is a few lines of code.]










share|improve this question
























  • @drescherjm I accidentally pressed submit when I wasn't finished writing the question yet. Oops!
    – Carl Joshua Quines
    Nov 21 at 19:45








  • 1




    Take a template or an std::function parameter pair that you call in the body of the loop in the if/else blocks.
    – palotasb
    Nov 21 at 20:37






  • 2




    btw you added some code, but there is still no continue or break in your example ;)
    – user463035818
    Nov 21 at 20:48






  • 3




    how to refactor the code depends on the part you dont show
    – user463035818
    Nov 21 at 20:50






  • 1




    Not sure what's unclear here... avoiding duplication of control structures is a valid question (with non-obvious answers).
    – Peter Ruderman
    Nov 21 at 21:07













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have duplicated control structures which are exactly the same, and the difference is how things happen inside. In particular, I don’t know how to deal with break, continue, and return. For example:



int f() {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (isFoo(i, j)) {
// doSomethingF1
} else {
// doSomethingF2
}
}
}
}

int g() {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (isFoo(i, j)) {
// doSomethingG1
} else {
// doSomethingG2
}
}
}
}


where doSomethings rely on i and j, and need to break or continue through the j loop, and return out of the function. How do I factor this out to get rid of the duplication?



I thought of writing a function that would take the doSomethings as arguments, but I'm not sure how to deal with the breaks, continues, or returns.



[I'm not fine with the duplication since there are some checks on i and j in f(), g(), and like four other functions that are exactly the same; the only difference is a few lines of code.]










share|improve this question















I have duplicated control structures which are exactly the same, and the difference is how things happen inside. In particular, I don’t know how to deal with break, continue, and return. For example:



int f() {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (isFoo(i, j)) {
// doSomethingF1
} else {
// doSomethingF2
}
}
}
}

int g() {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (isFoo(i, j)) {
// doSomethingG1
} else {
// doSomethingG2
}
}
}
}


where doSomethings rely on i and j, and need to break or continue through the j loop, and return out of the function. How do I factor this out to get rid of the duplication?



I thought of writing a function that would take the doSomethings as arguments, but I'm not sure how to deal with the breaks, continues, or returns.



[I'm not fine with the duplication since there are some checks on i and j in f(), g(), and like four other functions that are exactly the same; the only difference is a few lines of code.]







c++ refactoring c++14






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 at 19:45

























asked Nov 21 at 19:34









Carl Joshua Quines

334




334












  • @drescherjm I accidentally pressed submit when I wasn't finished writing the question yet. Oops!
    – Carl Joshua Quines
    Nov 21 at 19:45








  • 1




    Take a template or an std::function parameter pair that you call in the body of the loop in the if/else blocks.
    – palotasb
    Nov 21 at 20:37






  • 2




    btw you added some code, but there is still no continue or break in your example ;)
    – user463035818
    Nov 21 at 20:48






  • 3




    how to refactor the code depends on the part you dont show
    – user463035818
    Nov 21 at 20:50






  • 1




    Not sure what's unclear here... avoiding duplication of control structures is a valid question (with non-obvious answers).
    – Peter Ruderman
    Nov 21 at 21:07


















  • @drescherjm I accidentally pressed submit when I wasn't finished writing the question yet. Oops!
    – Carl Joshua Quines
    Nov 21 at 19:45








  • 1




    Take a template or an std::function parameter pair that you call in the body of the loop in the if/else blocks.
    – palotasb
    Nov 21 at 20:37






  • 2




    btw you added some code, but there is still no continue or break in your example ;)
    – user463035818
    Nov 21 at 20:48






  • 3




    how to refactor the code depends on the part you dont show
    – user463035818
    Nov 21 at 20:50






  • 1




    Not sure what's unclear here... avoiding duplication of control structures is a valid question (with non-obvious answers).
    – Peter Ruderman
    Nov 21 at 21:07
















@drescherjm I accidentally pressed submit when I wasn't finished writing the question yet. Oops!
– Carl Joshua Quines
Nov 21 at 19:45






@drescherjm I accidentally pressed submit when I wasn't finished writing the question yet. Oops!
– Carl Joshua Quines
Nov 21 at 19:45






1




1




Take a template or an std::function parameter pair that you call in the body of the loop in the if/else blocks.
– palotasb
Nov 21 at 20:37




Take a template or an std::function parameter pair that you call in the body of the loop in the if/else blocks.
– palotasb
Nov 21 at 20:37




2




2




btw you added some code, but there is still no continue or break in your example ;)
– user463035818
Nov 21 at 20:48




btw you added some code, but there is still no continue or break in your example ;)
– user463035818
Nov 21 at 20:48




3




3




how to refactor the code depends on the part you dont show
– user463035818
Nov 21 at 20:50




how to refactor the code depends on the part you dont show
– user463035818
Nov 21 at 20:50




1




1




Not sure what's unclear here... avoiding duplication of control structures is a valid question (with non-obvious answers).
– Peter Ruderman
Nov 21 at 21:07




Not sure what's unclear here... avoiding duplication of control structures is a valid question (with non-obvious answers).
– Peter Ruderman
Nov 21 at 21:07












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










If you really want/insist to have the same inner control in the loop context with break, continue, and return then the proposed solution is macros :-(.



On the other hand, if the doSomethingFG12 functions all have a defined interface, then compliment your generic control structure to handle the case with break, continue, and return to make a choice from there return values.






share|improve this answer





















  • Right, I can handle those cases with return values. Thanks.
    – Carl Joshua Quines
    Nov 22 at 4:12











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%2f53419349%2fhow-do-i-refactor-duplicate-control-structures%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








up vote
0
down vote



accepted










If you really want/insist to have the same inner control in the loop context with break, continue, and return then the proposed solution is macros :-(.



On the other hand, if the doSomethingFG12 functions all have a defined interface, then compliment your generic control structure to handle the case with break, continue, and return to make a choice from there return values.






share|improve this answer





















  • Right, I can handle those cases with return values. Thanks.
    – Carl Joshua Quines
    Nov 22 at 4:12















up vote
0
down vote



accepted










If you really want/insist to have the same inner control in the loop context with break, continue, and return then the proposed solution is macros :-(.



On the other hand, if the doSomethingFG12 functions all have a defined interface, then compliment your generic control structure to handle the case with break, continue, and return to make a choice from there return values.






share|improve this answer





















  • Right, I can handle those cases with return values. Thanks.
    – Carl Joshua Quines
    Nov 22 at 4:12













up vote
0
down vote



accepted







up vote
0
down vote



accepted






If you really want/insist to have the same inner control in the loop context with break, continue, and return then the proposed solution is macros :-(.



On the other hand, if the doSomethingFG12 functions all have a defined interface, then compliment your generic control structure to handle the case with break, continue, and return to make a choice from there return values.






share|improve this answer












If you really want/insist to have the same inner control in the loop context with break, continue, and return then the proposed solution is macros :-(.



On the other hand, if the doSomethingFG12 functions all have a defined interface, then compliment your generic control structure to handle the case with break, continue, and return to make a choice from there return values.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 at 22:35









Bo R

606110




606110












  • Right, I can handle those cases with return values. Thanks.
    – Carl Joshua Quines
    Nov 22 at 4:12


















  • Right, I can handle those cases with return values. Thanks.
    – Carl Joshua Quines
    Nov 22 at 4:12
















Right, I can handle those cases with return values. Thanks.
– Carl Joshua Quines
Nov 22 at 4:12




Right, I can handle those cases with return values. Thanks.
– Carl Joshua Quines
Nov 22 at 4:12


















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%2f53419349%2fhow-do-i-refactor-duplicate-control-structures%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

Sphinx de Gizeh

Dijon

Guerrita