I was in the shower and thought about something.
The deferred / promise pattern is to decrease callback hell, by allowing the developer to chain call functions, as mentioned here:
Parse.User.logIn("user", "pass").then(function(user) {
return query.find();
}).then(function(results) {
return results[0].save({ key: value });
}).then(function(result) {
// the object was saved.
});
From the top of my head - correct me if I am wrong - but it seems like using deferred / promises is an easy way to break the Law of Demeter?
The Law of Demeter states:
A method of an object may only call methods of:
- The object itself.
- An argument of the method.
- Any object created within the method.
- Any direct properties/fields of the object.
Each unit should have only limited knowledge about other units: only units "closely" related to the current unit. Or: Each unit should only talk to its friends; Don’t talk to strangers.
Any comments concerning this?
Update December 1 2013:
A summarized version of my question. The Promise framework is designed to simplify asynchronous coding and avoid "callback hell". One of the most beneficial features of Promise is that you can chain-call events by using .then()
, as seen in my example above.
Given all code/functions are now using Promise (like Benjamin Gruenbaum (author below) is currently doing), won't it open it up to make chain-calling functions really easy by going .then().then().then()
etc.
Writing code that chain-call functions after each other (.then().then().then()
) has to be a text-book example of how to break the Law of Demeter.
Hence my question; Does the Promise framework promote / open up / make it easier to abuse / break the Law of Demeter?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With