3 ms·
I obviously don't speak for Chris, but I took it to mean that instead of: ira_account.register_with_irs(customer.get_ssn()) He suggests: ira_account.re
by raganwald 18y ago
I obviously don't speak for Chris, but I took it to mean that instead of:
ira_account.register_with_irs(customer.get_ssn())
He suggests:
ira_account.register_with_irs(customer)
Which I think is a roundabout way of getting at looser coupling and working at a higher level of abstraction.
(update: obviously, I cast this in OO rather than functional terms. I suspect the point is isomorphic to FP).
- DanHulton 18y agoBut doesn't that stright-up violate the Law of Demeter? http://en.wikipedia.org/wiki/Law_of_Demeter http://en.wikipedia.org/wiki/Law_of_Demeter
- raganwald 18y agoGood question. That being said, I'm a long time Demeter Scofflaw...
- sysop073 18y agoI was just going to post that. If that's what he means it's the opposite of what you should usually do; in that example you're making register_with_irs() depend on the implementation of customer when it doesn't need to
- raganwald 18y agoAnd therein lies the trade-off. It may be that one day you need an address as well as an SSN to register an IRA. If you chose door A, you update the calling code to either pass both properties or pass a customer object. If you chose door B, you update the callee code to extract the address as well as the SSN. In one case we're arguing that the registration code shouldn't have to know how to work with a customer. In the other case we're arguing that code initiating a registration shouldn't have to know what the IRS wants from a customer. Seems like there's room for reasonable people to empty a bottle of single malt while debating the alternates.
- senorprogrammer 18y agoThat is indeed what I meant though of course it is not an absolute statement. As a general rule of thumb, inside your own web application, passing objects rather than raw data will lead to easier to read & debug code, quite likely at the risk of tighter coupling, you are correct. 'Course unless you're building a framework or library coupling should probably not be your main priority when building a web site. As for the Law of Demeter, like my list that too is a rule of thumb and not an absolute, requiring judgement calls.
- pg 18y agoAh. In that case, it depends on the situation. If you're going to do something that depends on a property of an object, you're going to have to pass the property itself as an argument eventually. E.g. if you want to see if the ssn is a valid one, or equal to some other ssn. I wonder if he's talking about some problem that happens with people who've converted from other languages, and that's why I don't understand it.
- raganwald 18y agoI agree, there's obviously a trade-off involved. I think the real issue is trying to identify RW-B's "Roles, responsibilities and Collaborations" in a design. There's (a) a piece of knowledge that a customer is involved, (b) a piece of knowledge that an SSN is involved, and (c) a piece of knowledge that an SSN can be extracted from a customer. I suspect the issue here is making good decisions about which entities/modules/functions/objects/thingummies should know a, which should know b, and which should know c.
- senorprogrammer 18y agoOddly I didn't expect the "property/object" one to be the most contentious! Who knew? Perhaps that one suffers more from brevity than all the others. The observation stems from seeing reams of web app code that take in form data (or raw database data) and then proceed to move it around the app without encapsulating it in any way. The result is functions like: function display_user( user_first_name, user_last_name...user_valid) Look at twenty or thirty functions like that in an app and one starts to think "perhaps you need a user object?" I certainly didn't mean to suggest we should all start tightly coupling all our classes together willy-nilly.
- raganwald 18y agoIt suffers from resembling a bikeshed ;-)
- marijn 18y agoLike almost all rules in that list, this seems way too broad -- often enough it makes more sense to pass some more primitive value, even if you get it from a property.
- deleted 18y ago[deleted]