I have the following pattern repeated many times:
for() {
//Same Code Here
for() {
//Same Code Here
if() {
//Only Part That Is Different
}
}
}
I was thinking they way not repeat myself would be to beak in into it's own method and return something inside the if:
public MyObject myMethod(MyObject myObject) {
for() {
//Same Code Here
for() {
//Same Code Here
if() {
return myObject;
}
}
}
}
That way I can call myMethod(), assign the returned value to a variable and do what I want with it. This won't compile because nothing is returned when the if condition isn't met. I don't want anything to happen when the if condition isn't met.
Am I going down the right path, or is there a better way to remove my duplicate code?
returninside theifwill break the loop, not sure if that's what you want. Can you add a more "realistic" code?