I have an existing method (or function in general) which I need to grow additional functionality, but I don't want to break any use of the method elsewhere in the code. Example:
int foo::bar(int x)
{
// a whole lot of code here
return 2 * x + 4;
}
is widely used in the codebase. Now I need to make the 4 into a parameter, but any code that already calls foo::bar should still receive what it expects. Should I extend and rename the old method and wrap it into a new one like
int foo::extended_bar(int x, int y)
{
// ...
return 2 * x + y;
}
int foo::bar(int x)
{
return extended_bar(x,4);
}
or should I declare a default argument in the header file like
int bar(int x, int y=4);
and just extend the function
int foo::bar(int x, int y)
{
// ...
return 2 * x + y;
}
What are advantages and disadvantages of each variant?