The Artima Developer Community
Sponsored Link

Agile Buzz Forum
Law of Demeter and Smalltalk

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
James Robertson

Posts: 29924
Nickname: jarober61
Registered: Jun, 2003

David Buck, Smalltalker at large
Law of Demeter and Smalltalk Posted: Jan 4, 2004 11:38 AM
Reply to this message Reply

This post originated from an RSS feed registered with Agile Buzz by James Robertson.
Original Post: Law of Demeter and Smalltalk
Feed Title: David Buck - Blog
Feed URL: http://www.cincomsmalltalk.com/rssBlog/buck-rss.xml
Feed Description: Smalltalk can do that
Latest Agile Buzz Posts
Latest Agile Buzz Posts by James Robertson
Latest Posts From David Buck - Blog

Advertisement
The Law of Demeter states that:
Within a method, messages can only be sent to the following objects:
  • A parameter of the method, including the enclosing object (this or self);
    • For pragmatic reasons: a global object;
  • An immediate part object (computed or stored):
    • An object that a method called on the enclosing object returns, including attributes of the enclosing object;
    • An element of a collection which is an attribute of the enclosing object;
  • An object created within the method
In general, this is good advice for object oriented development. When it comes to Smalltalk, though, it seems too restrictive. When all your conditional and looping constructs are implemented with messages, the Law of Demeter becomes excessively strict.

Consider the following code:

Employee

= anEmployee
   ^(self name = anEmployee name)
      and: [self employeeID = anEmployee employeeID]

The Law of Demeter would state that the first = is ok (since the receiver is "an object that a method called on the enclosing object returns") but the and: message is bad because it sent to the result of another message send.

If I was to strictly follow the Law of Demeter, I would have to re-write this as:

Employee

= anEmployee
   ^(self nameMatches: anEmployee)
      and: [self employeeIDMatches: anEmployee]

nameMatches: anEmployee
   ^self name = anEmployee name

employeeIDMatches: anEmployee
   ^self employeeID = anEmployee employeeID

How about another case. Should this be allowed?

Object

printOn: aStream
   | title |
   title := self class printString.
   aStream nextPutAll:
      ((title at: 1) isVowel ifTrue: ['an '] ifFalse: ['a ']).
   aStream nextPutAll: title

The messages "printString", "at:", "isVowel", and "ifTrue:ifFalse:" all break the Law of Demeter.

You'd have to re-write it as:

Object

printOn: aStream
   | title |
   title := self title.
   aStream nextPutAll:
      ((self startsWithVowel: title) ifTrue: ['an '] ifFalse: ['a ']).
   aStream nextPutAll: title

To me, this seems excessive. If the Law of Demeter made an exception for primitive types and messages, it would probably be ok. As is, things that would be perfectly fine in other languages would be illegal in Smalltalk. It just doesn't feel right.

Read: Law of Demeter and Smalltalk

Topic: Beagle 2 not calling home Previous Topic   Next Topic Topic: OnStar customization

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use