pass-object-or-separate-params

book-code-complete-2#members-of-objects-to-routine

From section 7.5 "How to Use Routine Parameters" (Page 179)

"There are two competing schools of thought about how to pass members of an object to a routine. Suppose you have an object that exposes data through 10 access routines and the called routine needs three of those data elements to do its job. Proponents of the first school of thought argue that only the three specific elements needed by the routine should be passed. They argue that doing this will keep the connections between routines to a minimum; reduce coupling; and make them easier to understand, reuse, and so on. They say that passing the whole object to a routine violates the principle of encapsulation by potentially exposing all 10 access routines to the routine that's called."

"If the abstraction is that the routine expects you to have three specific data elements, and it is only a coincidence that those three elements happen to be provided by the same object, then you should pass the three specific data elements individually. However, if the abstraction is that you will always have that particular object in hand and the routine will do something or other with that object, then you truly do break the abstraction when you expose the three specific data elements."

 

Also, in an earlier section he talks about two different types of coupling. That section is "Kinds of Coupling"

He prefers simple-data-parameter coupling to object-parameter coupling (where you would pass in an object that could answer the messages passed to it).


We have thought about this for view models. How should we get data into them that they need?


This seems tangentially related to the question of parley-thread-pass-arguments-as-ids-or-objects, though that's more about whether you pass the id for an object or the object itself (in both cases you're ultimately inflating to an object)