ES accessor usage guidelines (Was: Map/Set.prototype.size)

Allen Wirfs-Brock allen at wirfs-brock.com
Tue Oct 16 14:24:23 PDT 2012


On Oct 16, 2012, at 1:25 PM, Yehuda Katz wrote:

> Agreed. For example:
> 
> class Post {
> 
> }
> 
> class Comment {
> 
> }
> 
> Post.hasMany("comments");
> Comment.belongsTo("post");
> 
> let post = new Post()
> let comment = new Comment();
> 
> comment.post = post;
> post.comments //=> [comment]



A great example of where you should not use an accessor. As a rough guideline accessors (which are syntactically  models after state attributes) should be used to set/query  local characteristics of objects.  Methods should be use for complex computations and for establishing and accessing complex relationships among objects.  Why? Because experience with complex object models have shown that you have more understandable/extensible/maintainable systems when you are careful about where and how you expose coupling among objects. 

In this case, there is a bi-directional n-to-one relationship being maintained between objects. Such complex relationships are best modeled using methods because their require establishing and maintaining the relationship requires state changes in multiple objects. Expressing the operation as a method serves as a warning that something more complex than simply updating some local state is going on.  

> 
> This is similar to certain DOM APIs, and my expectation of a hypothetical version of Ember Data in ES6 would work. I don't think there is anything wrong with using an accessor here.

I don't think anybody hold the DOM up as a stelar example of Object-oriented design.   

You might not agree with the above guideline, or choose to follow it.  That's fine.  But, what you you propose as an alternative.  What are your guidelines for choosing to use an accessor rather than a method?   If you consider you example to be a reflection of "good design" what are the specific design principles it is following.  Why is that an appropriate place to use an accessor. 

Allen





> 
> Yehuda Katz
> (ph) 718.877.1325
> 
> 
> On Tue, Oct 16, 2012 at 4:12 PM, Erik Arvidsson <erik.arvidsson at gmail.com> wrote:
> On Mon, Oct 15, 2012 at 12:23 PM, Brendan Eich <brendan at mozilla.org> wrote:
> > * get/set accessor may have effects on 'set' (see the DOM) but only on the
> > receiver object (and unobservably, any children that become garbage, e.g.
> > when trimming .length on an array-like).
> 
> That is very limiting, even as a guideline. Any time there are two or
> more related objects it is very likely that a setter might affect some
> other object.
> 
> --
> erik
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
> 
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20121016/9148ebb0/attachment.html>


More information about the es-discuss mailing list