jeudi 27 octobre 2022

Is it better to encapsulate variables in an object before passing it by reference to a function ? which design to pick ? why? [closed]

I'm often facing the same designing issue.

Should I make a single object holding multiple members then pass it to a function or should I just pass each parameter individualy to the function ?

Usualy, I like to pass a single compound object to a function because.

  1. I feel it makes the code more readable.
  2. If I need a new parameter (I often need), I can add a new member to the object, and the function declaration stays unchanged. So my code doesn't break. Object constructor makes the job to avoid undefined behavior inside the function.
  3. I can export the function inside a dll, the client will have nothing to change if my object declaration has changed. unless I want to, I can change the constuctor, so we know a new param is expected.
  4. The execution is faster, Instead of passing every primitives to the function, I just pass a single adress.
  5. There must be many many more reason linked to the OOP I could not describe. And that's where I probably need the help.

One downside, is sometimes I tend to mix socks with apples. I make a function with 5 parameters, then later I need a 6th parameter. so I make a compound object with 6 members. but another 7th parameter comes...and has nothing to do with the class I did before. instead of having issue with my function declaration now it's my class which comes heavy.

What are the best practices ? Where do I start to learn ? Is there any good reasons to keep passing a lot of multiple parameters to a function ? Does tending to reduce the number of parameter as much as possible a good practice ?

There's 3 approaches that quickly comes to my mind.

What should I asked to myself to decide which approach is the best ?

// approach I
float computeWeight(const float &a, const float &b, const float &c, const float &d) {
    return a+b+c+d;
}

int main(){
   
    float a = 0.0;
    float b = 1.0;
    float c = 2.0;
    float d = 3.0;

    float result = computeWeight(a,b,c,d);
    return 1;
}

// approach II

struct bunchOfFlt{
    float a;
    float b;
    float c;
    float d;
} 

float computeWeight(const bunchOfFlt & myBunch) {
    return myBunch.a + myBunch.b + myBunch.c + myBunch.d;
}

int main(){
    bunchOfFlt myBunch = {0.,1.,2.,3.};
    float result = computeWeight(myBunch);
    return 1;
}


// approach III 

struct bunchOfFlt{
    float a;
    float b;
    float c;
    float d;

    float computeWeight() const {
         return a + b + c + d;
    }

} 


int main(){

    bunchOfFlt myBunch = {0.,1.,2.,3.};
    float result = myBunch.computeWeight();
    return 1;
}

What is the best approach here, is there any one faster than the other ? Which one to pick should be obvious. Sometimes depending of how the program may evolve or not, I feel very lost when I'm designing new programs and It's time for me to learn a bit of theory.

Aucun commentaire:

Enregistrer un commentaire