Let's suppose that I have this function:
void foo (struct1 &v1, struct2 &v2, ..., structn &vn){
//do something with a subset some of the arguments...
foo1(v1, v2, ..., vn);
}
void foo1 (struct1 &v1, struct2 &v2, ..., structn &vn){
//do something with a subset some of the arguments...
foo2(v1, v2, ..., vn);
}
...
void foom (struct1 &v1, struct2 &v2, ..., structn &vn){
//do something with a subset some of the arguments...
}
Is it a good practice to encapsulate all these arguments inside a dedicated structure and use it instead as only argument? Something like:
struct wrapper{
strcut1 v1;
struct2 v2;
...
structn vn;
}
void foo (wrapper & w){
//use some of w's fields
foo1(w);
}
void foo1 (wrapper & w){
//use some of w's fields
foo2(w);
}
...
void foom (wrapper & w){
//use some of w's fields
}
I think that this approach has 2 advantages:
- It's more maintainable: with the first method, if I have to add a new argument to
foom
, then I have to modify also all the othersm-1
foo
headers...a nightmare. With this method instead the only thing to do is to add a new field towrapper
. - It's more efficient: supposing that both
n
(the number of arguments) andm
(the number of functions) is big (say both 10) this means that we have to copym*n
addresses, which is not a big overhead, but for sure bigger thanm
.
Is everything correct? Did I miss some advantage/disadvantage? Notice that performance here are the priority
Why I need this?
Well, read this question to know more about it.