Interesting puzzle + function pointers?

OK. Say my current algo is:

1
2
3
4
5
6
7
  function func_a() {
    // 100 lines of code
  }
  if (a==0) {
    // call func_a
    a();
  }


Now, I want to introduce another variable 'b' which will be contributing for calling of another function func_b as like
1
2
3
4
5
6
7
  function func_b() {
    // 100 lines of code
  }
  if (b == 0) {
    // call func_b
    b();
  }


OK. Now the point is these 100 lines of code between two functions i.e. func_a and func_b, is almost same. Almost 90 lines are same as like:

1
2
3
4
5
  function a() {
    // Some 80 odd lines
    // 10 lines are different now.
    // Same 10 lines
  }


I hope it is clear until now. Question is:
Is there any way to share this code between two functions. I can think of three options as of now:
1) Forget everything and live with it.
2) Pass some param.
3) Do something with function pointers.

I do not want to implement option no. 2 since it is possible that later I will introduce another param. How can function pointers or something else work here?

Anyone up for challenge?


- Hemant
I think pointers are not your problem here

Is there any way to share this code between two functions
?
yes
4) Implement a function that contain those 80 lines

1
2
3
4
functionThatContainesLines()
{
      // 80 lines
}


1
2
3
4
5
6
7
function func_a() {
  functionThatContainesLines()
  }
  if (a==0) {
    // call func_a
    a();
  }


1
2
3
4
5
6
7
function func_b() {
  functionThatContainesLines()
  }
  if (b == 0) {
    // call func_b
    b();
  }
Nopes. Not accepted. Because then I may need to pass many params so and forth and as I said, common lines lies at end of func_a and func_b as well. So, basically you are suggesting to call me two functions. :)


What I was thinking is this:

Define two other functions fn_a and fn_b which will have only different lines across them. Create a function pointer. Keep a function as like:
[code]
func_a (pass function pointer) {
// Same lines
// Call function pointer
// Same lines.
}

Make sense?
Templates would help you pass objects of unknown type, so long as they define or are compatible with the functions your own function requires.
Last edited on
I think my question is not clear to everyone here who is trying to answer it. Let me try to explain it one more time in simpler terms.

Say I have something like this:
if (variable a == 1) {
// Call function_a
Int param1, param2, param3 = something;
function_a(param_1, param_2, param_3)
}

Now I want to add another piece of code to this when variable b is set as like:
if ( variable b == 1) {
// Call function_b
Int param1; // only need one param in this call.
function_b(param_1);
}


Not definitions of function_a and function_b.

<retrun_type> function_a(param_1, param_2, param_3) {
// Some 40 lines of code (*)
// Some 20 lines of code which are using above 40 lines (**)
// Lastly, some 40 lines of code which are using above 20 lines. (***)
}

<return_type> function_b(param_1) {
// Some 40 lines of code which are same 40 lines of function function_a (Same as of (*))
// Some 20 lines of code which are using above 40 lines and are different 20 lines from function_a (Different from (**))
// Some 40 lines of code which are using above 20 lines and are same as of function_a. (Same as of (***))
}

I hope it is very clear until now.

My concern is to manage this code in the best possible way. I have given
enough thinking to below options and do not want to do.
1) Pass parameters and live with it.
2) Create two functions of same lines across function_a and function_b. (Not accepted)
3) Waiting..

My big question here is:
Can function pointers/functors make my code more clean? And if yes, how?
Last edited on
Topic archived. No new replies allowed.