Good afternoon everyone.
I have a question regarding function overloading in C++.
There is a very big function in a class and my task is to make it more human readable and reduce it's complexity.
In that function, almost two similar code blocks exist. The only difference is in some parameters and couple of additional lines.
I want to create a function out of those blocks to avoid repeating, but how is it better to do? Should I overload that new function or add an if statement inside function so that if one parameter has this or that value, then those couple of additional lines should be executed as well?
What is the best approach?
The function looks like this
void f1(){
//some code
{
//some code block
//some other line that belongs to this block
//the code block continues
}
{
//the same kind of code block
//this line is completely different
//the code block continues
}
//some code
}
So, I want to make a function out of the code blocks, but they are not completely the same and require some different operations performing right in the middle. Example:
void boo(){
//code block
//if(blah) execute this additional line
//else this line
//code block
}
or overloading
void boo(param1, param2){
//code block
//unique line
//code block
}
void boo(param1, param2, param3){
//code block
//unique line
//code block
}
You can change
void boo(param1, param2){
//code block
//unique line
//code block
}
void boo(param1, param2, param3){
//code block
//unique line
//code block
}
into
void baa() {
// code block1
}
void bee() {
// code block2
}
void boo(param1, param2){
baa();
//unique line
bee();
}
void boo(param1, param2, param3){
baa();
//unique line
bee();
}
Or alternatively, you could pass in a lambda function for the unique line:
void boo(std::function<int()> unique) {
// code block
int bleh = unique();
// code block
}
and just call as such:
boo([]() { return 1; });
boo([]() { return 3; });
So, as I understand it, your situation is like this:
int FirstFunction(int parameter_1, bool parameter_2) {
// first large section of code shared by both
// line unique to FirstFunction
// second large section of code shared by both
}
int SecondFunction(long parameter_1, Dawg parameter_2) {
// line unique to SecondFunction
// first large section of code shared by both
// second large section of code shared by both
}
Is this accurate? If so, I would just take out each section of code that is identical (or very similar) between the two functions, put that section into its own function, and call it from both. Repeat until you have no duplicate code left:
void FirstSharedCode(/* parameters */) {
// first large section of code shared by both
}
void SecondSharedCode(/* parameters */) {
// second large section of code shared by both
}
int FirstFunction(int parameter_1, bool parameter_2) {
FirstSharedCode(/* parameters */);
// line unique to FirstFunction
SecondSharedCode(/* parameters */);
}
int SecondFunction(long parameter_1, Dawg parameter_2) {
// line unique to SecondFunction
FirstSharedCode(/* parameters */);
SecondSharedCode(/* parameters */);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With