Why is it bad to pass all parameters in a struct and just use what I need?

I have a project where I have a few static arrays and their sizes. I also have some functions where I need to work with some of those arrays (e.g., add items to an array and increment its size, modify an array item, delete an item and decrement its size, etc).

I've been told it's really bad design to create a struct that holds all of those arrays and their sizes, and then pass a reference to that struct to each function so they will use just what they need and don't touch what they don't need.

Let's say I have to add an item to an array. Then I pass the whole struct as a parameter and take only the array I need from it, add the item, increment the array size, but I don't even touch the other arrays and sizes in the struct. Why is that so bad? I've read about tight coupling but I still don't get how de-coupling those variables would help improve my code.

Thanks!!
You are on the border between a class and global variables. Basically you get the worst of both worlds: your global data is bundled and accessed by many functions that act on it.

If a class is not what you need, why are unrelated arrays grouped together?

If global state is not what you need, why are you passing it to all your functions?

I hope you also already know that global data is bad in a majority of cases. It seems like you are just hiding it from yourself in this case.
Last edited on
Thanks! That makes it more clear :)
One more question: I've also been told that my code isn't reusable if I do what I was doing. I'm not quite getting why it wouldn't be reusable, when I see just the opposite: a function that gets all parameters can work with any of them and don't need changes in their parameter list or argument list when it's called...
Reuseable means being able to take the code out of your program and place it into another program with minimal changes. In your case, your code entirely depends on that structure, so it can only ever work in your program.

Writing reusable code doesn't just benefit others, it benefits you too: you may find another use for a function you already wrote, and thus not have to reinvent the wheel every time you need the function to work with a slightly different thing.
Last edited on
Thanks again. That was very useful :)
Topic archived. No new replies allowed.