I have a function called myFunction1
whose definition is the following:
function myFunction1()
{
if condition1 then
doSomething1();
myFunction1();
else if condition2 then
doSomething2();
myFunction1();
else if condition3 then
doSomething3();
myFunction1();
else throw Exception;
}
Now I want to write a second function myFunction2
which is almost identical to myFunction1
except for a single case, i.e.,
function myFunction2()
{
if condition1 then
doSomething4();
myFunction2();
else if condition2 then
doSomething2();
myFunction2();
else if condition3 then
doSomething3();
myFunction2();
else throw Exception;
}
Note that it is only the first if
which differs from myFuction1
. How could I write myFuction2
so as to avoid duplicating code?