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.]
c++ refactoring c++14
|
show 1 more comment
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.]
c++ refactoring c++14
@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 anstd::functionparameter pair that you call in the body of the loop in theif/elseblocks.
– palotasb
Nov 21 at 20:37
2
btw you added some code, but there is still nocontinueorbreakin 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
|
show 1 more comment
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.]
c++ refactoring c++14
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
c++ refactoring c++14
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 anstd::functionparameter pair that you call in the body of the loop in theif/elseblocks.
– palotasb
Nov 21 at 20:37
2
btw you added some code, but there is still nocontinueorbreakin 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
|
show 1 more comment
@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 anstd::functionparameter pair that you call in the body of the loop in theif/elseblocks.
– palotasb
Nov 21 at 20:37
2
btw you added some code, but there is still nocontinueorbreakin 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
|
show 1 more comment
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.
Right, I can handle those cases with return values. Thanks.
– Carl Joshua Quines
Nov 22 at 4:12
add a comment |
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.
Right, I can handle those cases with return values. Thanks.
– Carl Joshua Quines
Nov 22 at 4:12
add a comment |
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.
Right, I can handle those cases with return values. Thanks.
– Carl Joshua Quines
Nov 22 at 4:12
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
@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::functionparameter pair that you call in the body of the loop in theif/elseblocks.– palotasb
Nov 21 at 20:37
2
btw you added some code, but there is still no
continueorbreakin 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