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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:19:07+00:00 2026-05-11T00:19:07+00:00

I’m refactoring a 500-lines of C++ code in main() for solving a differential equation.

  • 0

I’m refactoring a 500-lines of C++ code in main() for solving a differential equation. I’d like to encapsulate the big ideas of our solver into smaller functions (i.e. ‘SolvePotential(…)’ instead of 50 lines of numerics code).

Should I code this sequentially with a bunch of functions taking very long parameters lists, such as:

int main(int *argc, void **argv){    interpolate(x,y,z, x_interp, y_interp, z_interp, potential, &newPotential);    compute_flux(x,y,z, &flux)    compute_energy(x,y,z, &eng)    ...    // 10 other high-level function calls with long parameter lists    ...    return 0; }     

Or should I create a ‘SolvePotential’ class that would be called like so:

int main(int *argc, void **argv){    potential = SolvePotential(nx, ny, nz, nOrder);    potential.solve();    return 0; } 

Where I would define functions in SolvePotential that uses member variables rather than long parameter lists, such as:

SolverPotential::solve(){   SolvePotential::interpolate()   SolverPotential::compute_flux()   SolverPotential::compute_energy()   // ...    //  10 other high-level function calls with NO parameter lists (just use private member variables) } 

In either case, I doubt I’ll re-use the code very much… really, I’m just refactoring to help with code clarity down the road.

Maybe this is like arguing ‘Is it ’12’ or ‘one dozen’?’, but what do you think?

  • 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. 2026-05-11T00:19:08+00:00Added an answer on May 11, 2026 at 12:19 am

    Neither. ‘Move all my code from one single function to one single class’ is not OOP. One of the fundamental rules of OOP is that a class should have one single area of responsibility. This is not a single responsibility, it is around 15:

    SolverPotential::solve(){ SolvePotential::interpolate() SolverPotential::compute_flux() SolverPotential::compute_energy() // ...  //  10 other high-level function calls with NO parameter lists (just use private member variables) } 

    It also makes it pretty much impossible to maintain a class invariant, doesn’t it? When is it valid to call compute_flux? Solve? Interpolate? What’s to stop me from doing it in the wrong order? Will the class be in a valid state if I do? Will I get valid data out of it?

    However, why is it an either-or? Why can’t you make multiple classes and functions?

    // This struct could be replaced with something like typedef boost::tuple<double,double,double> coord3d struct coord3d { double x, y, z; };  coord3d interpolate(const coord3d& coord, const coord3d& interpolated, double potential); // Just return the potential, rather than using messy output parameters double compute_flux(const coord3d coord&flux); // Return the flux instead of output params double compute_energy(const coord3d& coord); // And return the energy directly as well 

    Of course, these functions don’t have to be functions. If necessary/convenient, each could be made a class, or probably better still, a functor, to maintain the necessary state, and perhaps to allow you to pass them as arguments to other functions efficiently.

    If optimal performance is important, you may have to be careful with directly returning larger structures, rather than using output parameters, but I’d definitely profile first, to see if it is a problem, and even if it is, you could probably avoid output params with expression templates.

    If you have an conceptual object on which a number of independent operations can be performed, it’s probably a hint that you need some OOP, that it should be modelled as a class with a number of member functions, each of which of course maintain the class invariant, no matter how, when and why they’re called.

    If you need to compose a number of functions, gluing them together to form new, larger, pieces of functionality, functional programming and functors are most likely what you need. One common reason (but definitely not the only one) to desire composable functions is if you need to perform the same operation on many different sets of data (perhaps even several different types, all implementing the same concept). Making a functor do the heavy lifting allows it to be used with std::transform or std::for_each. You may also want to use currying to gradually assemble your functions (perhaps some of the functions can be parametrized with a set of fixed parameters, which don’t vary between calls). Again, create a functor which is initialized with these fixed parameters, and then supply the varying data in operator().

    And finally, if you simply need to perform a sequence of operations on some mutable data, plain old procedural programming may be what best suits your needs.

    Finally, sprinkle with generic programming, templating the necessary classes and functions to allow them to work together without having to jump through hoops like pointer indirection or inheritance.

    Don’t get too hung up on OOP. Use the tools at your disposal.

    I don’t know enough of the context of your question to say for sure, but it seems to me that what you really need isn’t a class, it’s just a hierarchy of functions. Your user code calls solve(). solve() internally calls, say (made up, for the sake of example), interpolate() and compute_energy(). compute_energy() internally calls compute_flux(), and so on. Each function only makes a couple of calls to perform the logical steps that make up the responsibility of the function. So nowhere do you have a huge class with a dozen different responsibilities, or a big monolithic function which does everything sequentially.

    In any case, there is nothing wrong with ‘very long parameter lists’ (You can usually shorten them by grouping some of them together, but even if you can’t, there is nothing ‘un-OOP’ about passing a lot of parameters. On the contrary, it means the function is well encapsulated from everything else. All it needs is passed in the parameters, so it isn’t really tied to the rest of the application.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I sent an e-mail to our local representatives from Embarcadero… May 11, 2026 at 9:55 pm
  • Editorial Team
    Editorial Team added an answer destroy() is called first. destroy() will be called by the… May 11, 2026 at 9:55 pm
  • Editorial Team
    Editorial Team added an answer This will solve the particular problem that is not possible… May 11, 2026 at 9:55 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

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.