Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Architecture, Design & Strategy

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « Why isn't DDD normal in .Net?An interview with Roy Osherove author of "The Art of Unit Testing" »
    comments

    Introduction

    If you have been using an OO language to program with then you should have heard of the Law of Demeter. If you haven’t then you should read some of the articles I collected on Google.


    Definition

    It is actually very simple to me. But explaining it can be a bit more daunting. This is what you can find on Wikipedia about it.

    When applied to object-oriented programs, the Law of Demeter can be more precisely called the “Law of Demeter for Functions/Methods” (LoD-F). In this case, an object, A, can request a service (call a method) of an object instance, B, but object A cannot “reach through” object B to access yet another object, C, to request its services. Doing so would mean that object A implicitly requires greater knowledge of object B’s internal structure. Instead, B’s class should be modified, if necessary, so that object A can simply make the request directly of object B, and then let object B propagate the request to any relevant subcomponents. Or A should have a direct reference to object C and make the call directly. If the law is followed, only object B knows its own internal structure.

    More formally, the Law of Demeter for functions requires that a method M of an object O may only invoke the methods of the following kinds of objects:

    1. O itself
    2. M’s parameters
    3. Any objects created/instantiated within M
    4. O’s direct component objects

    In particular, an object should avoid invoking methods of a member object returned by another method. For many modern object oriented languages that use a dot as field identifier, the law can be stated simply as “use only one dot". That is, the code “a.b.Method()” breaks the law where “a.Method()” does not. There is disagreement as to the sufficiency of this approach.


    My side of the story

    Let’s oversimplify this a bit. When you see 2 dots in a statement, then you know something is going to be wrong. Look at this:

    1. Dim _Person as Person = New Person
    2. Console.WriteLine(_Person.Addresses.Count)

    In that example _Person.Addresses.Count is a smell. Why? Because Addresses could be null and when it is null it can give a nullreferenceexception and crash our program.

    There are 2 ways around this problem.

    First.

    1. Dim _Person as Person = New Person
    2. Try
    3.    Console.WriteLine(_Person.Addresses.Count)
    4. Catch ex as NullReferenceExceptionException
    5.    Console.WriteLine(0)
    6. End Try

    or

    1. Dim _Person as Person = New Person
    2. If _Person.Addresses IsNot Nothing then
    3.    Console.WriteLine(_Person.Addresses.Count)
    4. Else
    5.    Console.WriteLine(0)
    6. End If

    What is wrong with this approach? Well every time you call Addresses.Count you have to check if Addresses isn’t null and print 0 instead. This gets boring very quickly and sometimes somebody might forget this little detail.

    So we have a better solution. Namely, add a Method/Property to our Class Person. We name it CountAddresses for example and it would look something like this.

    1. Public Function CountAddresses() as Integer
    2.    If _Addresses IsNot Nothing Then
    3.       Return _Addresses.Count
    4.    Else
    5.       Return 0
    6.    End If
    7. End Function

    Now we can call CountAdresses instead of Addresses.Count and not have to fear NullReferenceExceptions anymore.

    The downside of this is that you have to write more code in your classes. But less code in your calling classes.

    So I guess you should use it wisely.

    When not

    And when doesn’t the Law of Demeter apply?

    • Fluent interfaces (Unlimited amount of dots)
    • Factory classes (You can have 2 dots there)
    • Legacy systems (No laws apply in there)

    About the Author

    User bio imageChristiaan is a forensic technician who programs on the side, although my function description says that I do IT-things for 90% of the time . I'm an avid VB.NET fan and I use lots of the ALT.Net techniques, like unit-testing, nhibernate, logging, IoC, ...
    Social SitingsTwitterLinkedInHomePageLTD RSS Feed
    Instapaper

    1 comment

    Comment from: Hari Subramaniam [Visitor]
    Hari Subramaniam I wonder why LoD isnt applicable for Fluent interfaces?
    08/01/11 @ 05:08

    Leave a comment


    Your email address will not be revealed on this site.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)