maksims
maksims

Reputation: 31

C++ should a function be overloaded or if statement is enough?

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
}

Upvotes: 3

Views: 515

Answers (3)

Pandrei
Pandrei

Reputation: 4951

From the question statement the initial complex function kind of looks like this:

void f1(){
  //some code 
  {
     { <common_code_block_0> }
     <unique_code>
     { <common_code_block_1> }
  }
  {
     { <common_code_block_0> }
     <unique_code>
     { <common_code_block_1> }
  }

  //some code
}

There are two common code blocks which are repeated. These need to be isolated to reduce complexity and the likelihood of future bugs. Jut put each one of them in a separate function:

void function_0
{
   { <common_code_block_0> }
} 

void function_1
{
   { <common_code_block_1> }
} 

Taking it one step further declare these two functions as inline to avoid any overhead from the function call.

 inline  void function_0();    
 inline  void function_1();

Since there is some unique code executed between the two common_code blocks on each branch, probably the there are parameters to be passed in/returned to/from these new functions. So they probably look something like:

//parameters are I/O
inline void function_0(data_type* param1, data_type* param2, etc);
inline void function_1(data_type* param1, data_type* param2, etc);

And the initial f1() now looks like:

  void f1(){
  //some code 
  {
     function_0(&param1, &param2, etc);
     <unique_code>
     function_1(&param3, &param4, etc);
  }
  {
     function_0(&param1, &param2, etc);
     <unique_code>
     function_1(&param3, &param4, etc);
  }

  //some code
}

Upvotes: 1

Sam Estep
Sam Estep

Reputation: 13324

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 */);
}

Upvotes: 1

kotakotakota
kotakotakota

Reputation: 780

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; });

Upvotes: 1

Related Questions