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 526949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:49:51+00:00 2026-05-13T08:49:51+00:00

How would you fix the following bad code that passes too many parameters around?

  • 0

How would you fix the following bad code that passes too many parameters around?

void helper1(int p1, int p3, int p5, int p7, int p9, int p10) {
  // ...
}

void helper2(int p1, int p2, int p3, int p5, int p6, int p7, int p9, int p10) {
  // ...
}

void foo(int p1, int p2, int p3, int p4, int p5, int p6, int p7, int p8, 
         int p9, int p10) {
  helper1(p1, p3, p5, p7, p9, p10);
  helper2(p1, p2, p3, p5, p6, p7, p9, p10);
}

I see two different approaches:

Approach 1: Put all functions in a class

class Foo {
  private:
    int p1, p2, p3, p4, p5, p6, p7, p8, p9, p10;
    void helper1() {}
    void helper2() {}
  public:
    void foo() {
      helper1();
      helper2();
    }
    // add constructor
};

Approach 2: Just pass parameters as a class

struct FooOptions {
    int p1, p2, p3, p4, p5, p6, p7, p8, p9, p10;
};

void helper1(const FooOptions& opt) {
  // ...
}

void helper2(const FooOptions& opt) {
  // ...
}

void foo(const FooOptions& opt) {
  helper1(opt);
  helper2(opt);
}

What are the advantages and disadvantages of the approaches?

An advantage of Approach 1 is that — if you make the helper functions virtual — you can subclass and overload them, adding flexibility. But then, in my case (outside of the toy mini example that I gave) such helpers are often templated, so they cannot be virtual anyway.

An advantage of Approach 2 is that the helper functions can easily be called from other functions, too.

(This question is related, but does not discuss these two alternatives.)

  • 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-05-13T08:49:51+00:00Added an answer on May 13, 2026 at 8:49 am

    Short Answer:

    Happy New Year! I’d avoid option #1 and only go with option #2 if the parameters can be separated into clear and logical groups that make sense away from your function.

    Long Answer

    I have seen many examples of functions as you described from coworkers. I’ll agree with you on the fact that it’s a bad code smell. However, grouping parameters into a class just so you don’t have to pass parameters and deciding rather arbitrarily to group them based on those helper functions can lead to more bad smells. You have to ask yourself if you’re improving readability and understanding for other that come after you.

    calcTime(int p1, int p2, int p3, int p4, int p5, int p6) {
        dist = calcDistance( p1, p2, p3 );
        speed = calcSpeed( p4, p5, p6 );
        return speed == 0 : 0 ? dist/speed; }
    

    There you can group things to be more understandable because there is a clear distinction amongst parameters. Then I would suggest approach #2.

    On the other hand, code in which I’ve been handed often looks like:

    calcTime(int p1, int p2, int p3, int p4, int p5, int p6) {
        height = height( p1, p2, p3, p6 );
        type = getType( p1, p4, p5, p6 );
        if( type == 4 ) {
            return 2.345; //some magic number
        }
        value = calcValue( p2, p3, type ); //what a nice variable name...
        a = resetA( p3, height, value );
        return a * value; }
    

    which leaves you with a feeling that these parameters aren’t exactly friendly to breaking up into something meaningful class-wise. Instead you’d be better off served flipping things around such as

    calcTime(Type type, int height, int value, int p2, int p3)
    

    and then calling it

    calcTime(
        getType( p1, p4, p5, p6 ),
        height( p1, p2, p3, p6 ),
        p3,
        p4 );
    

    which may send shivers up your spine as that little voice inside your head screams “DRY, DRY, DRY!” Which one is more readable and thus maintainable?

    Option #1 is a no-go in my head as there is a very good possibility someone will forget to set one of the parameters. This could very easily lead to a hard to detect bug that passes simple unit tests. YMMV.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 357k
  • Answers 357k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The other answers are correct. Here is some code you… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer you ruin the noConflict concept by reassigning the jquery to… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer If you get that particular error, you don't actually have… May 14, 2026 at 9:40 am

Related Questions

No related questions found

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.