Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How much impact does sending unnecessary objects have?

I often find myself in a situation where I need to have a lot of objects as parameters for a method, but all or almost all of those objects can be referenced from another object. In my case, I've got loads of objects such as particle managers and projectile managers which are all attributes of a screen object. Sometimes I just give the entire screen object as the parameter to save time, getting whatever objects I need.

Is this good practice or not? On the one hand, it saves me time, but I don't know the impact of the extra (unnecessary) information also within the screen object when I send it as an argument. Is this inefficient?

like image 219
BlueCP Avatar asked Nov 07 '22 23:11

BlueCP


1 Answers

So you have a Screen object that has lots of useful fields that your other methods need, and you are too lazy to pass those fields to those methods, so you made the methods all accept a single Screen object, then you can just pass your Screen.

Hopefully I have understood your situation correctly.

This doesn't actually mean that you are moving unnecessary data around in memory. Objects themselves aren't copied when they are passed as parameters. Only their addresses are copied, which would be the same size of data as if you pass the individual fields. Addresses are all of the same size. Therefore, this probably won't cause a performance problem.

However, this might be bad design. By passing Screen to your methods, you are making your methods dependent on Screen. If your methods has nothing to do with the UI, they should not depend on Screen, right? They should work without a Screen as well.

Also, your Screen might be a god class and break the Single Responsibility Principle. You might want to refactor that.

like image 156
Sweeper Avatar answered Nov 14 '22 23:11

Sweeper