I have a little simple question.
Let’s say I have a data object with about 10 properties, and I want to pass data from my object to a function.
Most of the time I only need one of these values in the receiving function, and could just as well pass only that value (let’s say an int). So what is the pros and cons with always sending the whole object vs only sending one of the contained values?
Is there a best practice?
When you “pass an object”, you are actually just passing the reference to the object – so 4 bytes on x86 and 8 bytes on x64. So if the method feels natural to take the object, just pass the object; that’s fine. Any other approach is probably going to be worse.
Of course, if it feels “natural” that the method only needs a single
int, and could only ever need the singleint, then passing anintis fine too.My point is: write the method to support what makes sense for the method.