The problem with the Demeter Law is that your classes know too much about each other, making them more closely connected, which in turn makes them more difficult and difficult to test.
In my experience, the most common violations of the Demeter Law are in the eyes. Since you are trying to make amends for the name, I assume the point is here. Soooo ...
Can a view helper be used? Sorry, not very good in Rails, so this is pseudocode:
def upcased_name(entity_with_name) if entity_with_name != nil entity_with_name.name.try(:upcase) end end
Then, in your opinion, you just call
<% upcased_name(person.pet) %>
You can test upcased_pet_name by entering various values in the viewhelper.
Now:
- In your view, you only know that it has access to the Face and access to a viewer called
upcased_name . - Your
Person model only knows that it has a Pet method. - Your observer only knows that he can get an object with a name method or
nil .
Boom! Your classes only know about their friends and nothing about the friends of their friends.
source share