Search the web
Sign In
New User? Sign Up
extremeprogramming · Extreme Programming
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Show off your group to the world. Share a photo of your group with us.

Best of Y! Groups

   Check them out and nominate your group.
Having problems with message search? Fill out this form to ensure your group is one of the first to be migrated to the new message search system.

Messages

  Messages Help
Advanced
The Law of Demeter and Testability   Message List  
Reply | Forward Message #122627 of 152462 |
The Law of Demeter and Testability
<http://jayflowers.com/WordPress/?p=78> Monday,
September 11th, 2006

The Law of Demeter, also know as the Principle of Least Knowledge, is
important to controlling the type population of a test subject. Here is a
fun explain from c2.com:

- You can play with yourself.
- You can play with your own toys (but you can't take them apart),
- You can play with toys that were given to you.
- And you can play with toys you've made yourself.

------------------------------

Explanation in plain English:

- Your method can call other methods in its class directly
- Your method can call methods on its own fields directly (but not on
the fields' fields)
- When your method takes parameters, your method can call methods on
those parameters directly.
- When your method creates local objects, that method can call method
on the local objects.

------------------------------

The paper Mock Roles, not
Objects<http://www.jmock.org/oopsla2004.pdf#search=%22mock%20roles%20not%20objec\
ts%22
>
contains
a good example:

dog.Body.Tail.Wag();

should really be:

dog.ExpressHappiness();

To be clear the example code above would be contained in the test subject.
The bad example couples the test subject to DogAnimal, DogBody, and DogTail.
Where as the good example couples only to DogAnimal. This smell has always
reminded me of a violation of the Single Responsibility Principle.
These symptoms indicate a violation of the Law of Demeter:

- To Many Mock Objects
- To Much Mock Setup
- Mock Object Chaining

"Compliance to the Law of Demeter reduces the number of interfaces, the
number of stubs and drivers that may be needed, and the number of
integration test interfaces." - Reviewing Software Artifacts for
Testability<http://www.informatik.fernuni-hagen.de/pi3/PDFs/EuroSTAR99.pdf#searc\
h=%22Reviewing%20Software%20Artifacts%20for%20Testability%22
>
by
Stefan Jungmayr

Interestingly Endo-Testing Unit Testing with Mock
Objects<http://www.ccs.neu.edu/research/demeter/related-work/extreme-programming\
/MockObjectsFinal.PDF
>
by
Tim Mackinnon, Steve Freeman and, Philip Craig presents the view that "…code
developed with Mock Objects tends to conform to the Law of Demeter, as an
emergent property. The unit tests push us towards writing domain code that
refers only to local objects and parameters, without an explicit policy to
do so." I am not so sure that use of mocks naturally leads to conformance
to the Law of Demeter. Martin Fowler noticed that developers that perform
interaction based testing tend to follow the Law of Demeter (Mocks Aren't
Stubs <http://www.martinfowler.com/articles/mocksArentStubs.html>):

"Interaction-based testers do talk more about avoiding 'train wrecks' -
method chains of style of getThis().getThat().getTheOther(). Avoiding method
chains is also known as following the Law of Demeter. While method chains
are a smell, the opposite problem of middle men objects bloated with
forwarding methods is also a smell. (I've always felt I'd be more
comfortable with the Law of Demeter if it were called the Suggestion of
Demeter.) One of the hardest things for people to understand in OO design
is the "Tell Don't Ask"
principle<http://www.amazon.com/exec/obidos/ASIN/020161622X>,
which encourages you to tell an object to do something rather than rip data
out of an object to do it in client code. Interaction testers say that using
interaction testing helps promote this and avoid the getter confetti that
pervades too much of code these days."

Mock Object
Patterns<http://hillside.net/plop/plop2003/Papers/Brown-mock-objects.pdf#search=\
%22%22Law%20of%20Demeter%22%20testability%22
>
by
Matthew A. Brown and Eli Tapolcsanyi includes the pattern Pass in Mock
Collaborator. In the implementation section of the pattern they direct you
to use "… the Law of Demeter, design your method calls to permit easy
testing" of the test subject.

I think the basic underlining issue with violations of the Law of Demeter is
misplaced responsibility. I suspect that this is why it is also called the
Principle of Least Knowledge and why it reminds me of the Single
Responsibility Principle. Part of correctly assigning responsibility is
dependency management an testability is very sensitive to dependency issues.



Side Note:

Some people get concerned about interaction based testing coupling the unit
test to the implementation of the test subject. It can if a test double is
used in place of an object that the test subject uses as a helper (i.e. the
interaction of the test subject with the helper is not a responsibility of
the test subject). In general you should only ever use test doubles where
it is the responsibility of the test subject to perform the interaction.
Please notice I said responsibility of the test subject and not the system.
A test subject plays a role in a system and that role has responsibilities
which include interaction, maybe even entirely comprised of, with other
objects in the system. When you refactor a class, or test subject, in a
system it is still responsible for interacting with the same objects it did
before the refactoring. When you change its interactions, or move the
responsibility to an other class, you are refactoring the system. When
refactoring the system unit tests should break. When refactoring a class
unit tests should not break.


--
Jay Flowers
----------------------------------------------------------------------
http://jayflowers.com
---------------------------------------------------------------------


[Non-text portions of this message have been removed]




Mon Sep 11, 2006 8:00 pm

jfl0wers
Offline Offline
Send Email Send Email

Forward
Message #122627 of 152462 |
Expand Messages Author Sort by Date

The Law of Demeter and Testability <http://jayflowers.com/WordPress/?p=78> Monday, September 11th, 2006 The Law of Demeter, also know as the Principle of Least...
Jay Flowers
jfl0wers
Offline Send Email
Sep 11, 2006
8:04 pm

I'm hoping someone can clear up this nagging question ... ... I can understand that the first example is fragile in the face of code changes ... what I'm not...
jeffz_2002
Offline Send Email
Sep 12, 2006
2:48 pm

I assume it would be something like: dog.ExpressHappiness() which looks like: Body.WagTail() which looks like: Tail.Wag() David _____ From:...
David Moye
cdmoye
Offline Send Email
Sep 12, 2006
3:28 pm

I hate to reply to my own mail, but I thought I needed to add some reasoning. Mostly I think that Jeff was making one misstep. Body doesn't need to know why...
David Moye
cdmoye
Offline Send Email
Sep 12, 2006
3:44 pm

... But does body even know about tail wagging? Doesn't body just ask the tail to express something, and the tail responds by wagging? It boils down to having...
jeffz_2002
Offline Send Email
Sep 12, 2006
5:09 pm

... What is the Body for ? Why can't the Dog just have a Tail ? YAGNI Body Kevin...
Kevin Lawrence
kevinwilliam...
Offline Send Email
Sep 13, 2006
3:39 pm

... I dissgree. The structural relationships between dog body and tail are intrinsic to the problem domain and therefore afferent dependencies on them are a...
Paul Campbell
pncampbell99
Offline Send Email
Oct 6, 2006
9:50 am

I have a couple of dogs, and for sure it is not enough to just wag a tail to express happiness. Without getting too much into the design of the class, it is...
Robert Hanson
mvarobert1
Offline Send Email
Sep 12, 2006
5:03 pm

Or maybe it's dog.ExpressHappiness() { sendEmotion(happiness); } and all the body parts are subscribed to the emotion broadcast. - George ... -- ... * George...
George Dinwiddie
gdinwiddie
Offline Send Email
Sep 13, 2006
2:12 am

Yep, that absolutely appeals to me. (Though there's a little voice in my head saying that I should ideally avoid building all the infrastructure /levels of...
Walter Prins
bytejuggler
Offline Send Email
Sep 13, 2006
12:51 pm

This one looks best to me, and it does not violate the Law of Demeter. It could even do things like lule tounge. ... -- Jay Flowers ... http://jayflowers.com...
Jay Flowers
jfl0wers
Offline Send Email
Sep 12, 2006
8:16 pm

Hi, Jay, I found this interesting, thank you. More below, keep reading..... ... <snip> This ... I don't know *why*, gramatically, but the first several times I...
Kay A. Pentecost
tranzpuppy
Offline Send Email
Sep 12, 2006
8:19 pm

The first draft was something like: I often read of other peoples troubles with mock objects. Some of the issues are due to violations of the Law of Demeter. ...
Jay Flowers
jfl0wers
Offline Send Email
Sep 12, 2006
8:36 pm

Hi, Jay, ... No, I don't think you need the extra words... the one you used what was better than that. Maybe the "symptoms" word needs to be closer to the...
Kay A. Pentecost
tranzpuppy
Offline Send Email
Sep 12, 2006
8:57 pm

Kay, Ahh! Now I see what you mean. Your right, it is similar to, the imphisaas is on the wrong sylaable. ... -- Jay Flowers ... http://jayflowers.com...
Jay Flowers
jfl0wers
Offline Send Email
Sep 12, 2006
9:13 pm

From: "Michael Feathers" <mfeathers.at.mindspring.com@...> To: "extremeprogramming@yahoogroups.com" ...
yahoogroups@...
jhrothjr
Offline Send Email
Sep 21, 2006
12:42 am

From: "Paul Campbell" <yahoo.at.objectvision.co.uk@...> To: "extremeprogramming@yahoogroups.com" ...
yahoogroups@...
jhrothjr
Offline Send Email
Nov 15, 2006
10:51 pm

... Thank you! Excellent Article! I have forwarded around my colleagues begging them to read it. In someways I see LoD as the "super principle". LoD is very...
John Carter
refactored
Offline Send Email
Sep 18, 2006
11:52 pm

John, Your welcome. Thank you! I hope I can do even better with rest of the series. I like that example very much. It definitely makes it feel like a ...
Jay Flowers
jfl0wers
Offline Send Email
Sep 19, 2006
12:42 am

... It's a great article, but the thing to remember about LoD is that it is not iron-clad. There are cases where you're better off not having demetered code....
Michael Feathers
mfeathers256
Offline Send Email
Sep 19, 2006
7:09 pm

Hello Michael, thanks for your note. On Tuesday, September 19, 2006, ... Yes ... the observation about structure is a good one. If you think of what you'd have...
Ron Jeffries
RonaldEJeffries
Offline Send Email
Sep 19, 2006
11:47 pm

There is a "counterforce" that sometimes causes me to put the Law of Demeter aside: the notion of Design by Contract. In my current project, I have a situation...
Willem Bogaerts
toetah2000
Offline Send Email
Sep 20, 2006
12:14 pm

Hey I just wanted to throw something out there, I don't believe it breaks demeter. Let me know what you guys think. Public Dog { public event void...
Paul
altacus
Offline Send Email
Sep 20, 2006
3:59 pm

... I'm not saying it's an answer here, but sometimes I get mileage out of rethinking the primary responsibility of a class. Backend provides access, but what...
Michael Feathers
mfeathers256
Offline Send Email
Sep 20, 2006
6:17 pm

... In this case, no. Backend manages (and hides) three kinds of storage. Its purpose is to offer the rest of the code a central point of access to the "table...
Willem Bogaerts
toetah2000
Offline Send Email
Sep 20, 2006
8:06 pm

... I think of it this way: any algorithm should /either/ talk to its direct neighbors /or/ reach into the belly of the beast, but not both. Said differently,...
J. B. Rainsberger
nails762
Offline Send Email
Sep 28, 2006
2:01 am

... it is ... You get ... Sorry for resurecting an old topic but Ive been off line for a while ... I dont believe there is a meaningful distiction between...
Paul Campbell
pncampbell99
Offline Send Email
Nov 13, 2006
10:45 am

Hello, Paul. I don't understand your points. I'll say more near them ... I'm not clear which side you're coming down on. I take it that you would agree with...
Ron Jeffries
RonaldEJeffries
Offline Send Email
Nov 13, 2006
11:21 am

... Of course an employee would be directly resolvable via a repository in this example. But for any business logic where an employees department is pertinent...
Paul Campbell
pncampbell99
Offline Send Email
Nov 13, 2006
12:30 pm
First  | < Prev  |  Next > Last 
Advanced

Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines - Help