Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8746469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:11:55+00:00 2026-06-13T12:11:55+00:00

There is a C++ function that calculates something (I am not sure if C++

  • 0

There is a C++ function that calculates something (I am not sure if C++ matters here at all, anyway…). It is called in 50 or more places. Now it turned out that this function works wrongly. And in order to work correctly it needs three more arguments.

How can this code be refactored most efficiently in terms of the number of necessary changes and compactness.

BTW newly added arguments are such that it is not reasonable to have default values for them. They should be always passed to the function.

Many people asked for an example. Here it is:

//old syntax of function
int f(int a1, int a2)
{
    return a + b;
}

//new syntax of function
int f(int a1, int a2, int a3, int a4, int a5)
{
    if (a3 == 10)
    {
        return a1 + a2;
    }
    else
    {
        return a1 + a2 + a4 + a5;
    }
}

Does this example help? I need a way of doing this using a general approach, like a design patterns, like a principle of refactoring, but not for a specific example…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T12:11:56+00:00Added an answer on June 13, 2026 at 12:11 pm

    It really depends on where the three arguments come from. If we can’t have default values and it is not possible to create a common pattern for the extra parameters, then you may have no alternative but to attack each of the 50 calls in groups. In this case, you’d keep the original call and make a direct copy with a slightly different name. You then move gradually over so that eventually all the calls call the new function with the extra parameters. You can then retire the old one.

    On the other hand, if we can start with defaults or at least make them independent of the calling code then the following might be a good plan. The thing to bear in mind is that as a large change, this would presumably have to be done in phases to control the potential impact if anything went wrong.

    First, I would change the name of the function from xxxx to xxxx_<tag> where <tag> is a handle for the change – possibly a bug # from a defect tracker or change management system. Then I’d create a new function called xxxx which simply calls xxxx_<tag> recompile everything. So far so good:

    void xxxx_tag(int p1, int p2)
    {
        // ....
    }
    
    void xxxx(int p1, int p2)
    {
        xxxx_tag(p1, p2);
    }
    

    Next I’d change the signature of xxxx_<tag> to add the extra three parameters and the call to it. Now I’d rebuild again:

    void xxxx_tag(int p1, int p2, int p3, int p4, int p5)
    {
        // ....
    }
    
    void xxxx(int p1, int p2)
    {
        // XXX, YYY, and ZZZ and constants or at least can be derived at this point.
        xxxx_tag(p1, p2, XXX, YYY, ZZZ);
    }
    

    Key point here is also to add comments for future maintainers describing why this wrapper exists. Unfortunately, this is as far as some of these changes get so code gets left behind the purpose of which isn’t immediately obvious.

    I would then plan to phase in the 50 changes in say groups of five or ten so that you change your original call to the new call:

    xxxx(p1, p2);
    

    becomes:

    xxxx_tag(p1, p2, p3, p4, p5);
    

    Each section of calling code can be individually tested so that your are happy that (a) it works like it always did (i.e. fully backwards compatible) and (b) it fixes any problems.

    Finally, once all this is done, you can then do a single change to remove the new function xxxx() and rename xxxx_<tag> to xxxx Again, you’d have to fully rebuild and test.

    Conclusion

    Whichever way you go I’d recommend:

    1. Do it in stages – this minimises the risk of something going wrong.
    2. Test, test and test again – again, this reduces your exposure to problems.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

is there any function in C++ that calculates a fingerprint or hash of a
TTR has some excellent TA indicators. Is there a package or function that calculates
Is there a function that can round a float in Fsharp? Something like round(3.21156,3)
Is there any function that does the inverse operation of array_diff() ? I mean,
Is there a function that can truncate or round a Double? At one point
Is there a function that tells what class you are extending? Like function_exists? I
Is there a function that takes 1,2,3 and returns 3,1,2 respectively and vice-verse. eg:
Is there some function that returns the parent directory of a file in Perl?
Is there any function that do the opposite job of QString::split() . I mean
Say we're writing a simple recursive function fib(n) that calculates the nth Fibonacci number.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.