62

The wikipedia article about Law of Demeter says:

The law can be stated simply as "use only one dot".

However a simple example of a fluent interface may look like this:

static void Main(string[] args)
{
   new ZRLabs.Yael.Pipeline("cat.jpg")
        .Rotate(90)
        .Watermark("Monkey")
        .RoundCorners(100, Color.Bisque)
        .Save("test.png");
}

So does this goes together?

1
  • 1
    This is an important question because violating the Law of Demeter can make unit tests that contain mocks hard to maintain (though maybe fluent chains are an exception). Commented Sep 27, 2018 at 18:10

7 Answers 7

90

Well, the short definition of the law shortens it too much. The real "law" (in reality advice on good API design) basically says: Only access objects you created yourself, or were passed to you as an argument. Do not access objects indirectly through other objects. Methods of fluent interfaces often return the object itself, so they don't violate the law, if you use the object again. Other methods create objects for you, so there's no violation either.

Also note that the "law" is only a best practices advice for "classical" APIs. Fluent interfaces are a completely different approach to API design and can't be evaluated with the Law of Demeter.

2
  • 16
    One could argue that a fluent interface does not violate the Law of Demeter because you are only accessing the same object in a dot chain.
    – MSN
    Commented Jan 29, 2009 at 6:53
  • 1
    A second way to look at this is that a fluent interface is simply an alternate, compact way of performing sequenced operations in such a way that state mutation is abstracted away, since the results of each intermediate step are implicit. Commented Apr 7, 2016 at 18:59
28

Not necessarily. "Only use one dot" is an inaccurate summary of the Law of Demeter.

The Law of Demeter discourages the use of multiple dots when each dot represents the result of a different object, e.g.:

  • First dot is a method called from ObjectA, returning an object of type ObjectB
  • Next dot is a method only available in ObjectB, returning an object of type ObjectC
  • Next dot is a property available only in ObjectC
  • ad infinitum

However, at least in my opinion, the Law of Demeter is not violated if the return object of each dot is still the same type as the original caller:

var List<SomeObj> list = new List<SomeObj>();
//initialize data here
return list.FindAll( i => i == someValue ).Sort( i1, i2 => i2 > i1).ToArray();

In the above example, both FindAll() and Sort() return the same type of object as the original list. The Law of Demeter is not violated: the list only talked to its immediate friends.

That being said not all fluent interfaces violate the Law of Demeter, just as long as they return the same type as their caller.

9

The spirit of Demeter's Law is that, given an object reference or class, you should avoid accessing the properties of a class that's more than one sub-property or method away since that will tightly couple the two classes, which might be unintended and can cause maintainability problems.

Fluent interfaces are an acceptable exception to the law since they're meant to be at least somewhat tightly coupled as all the properties and methods are the terms of a mini-language that are composed together to form functional sentences.

8

Yes, although you have to apply some pragmatism to the situation. I always take the Law of Demeter as a guideline as opposed to a rule.

Certainly you may well want to avoid the following:

CurrentCustomer.Orders[0].Manufacturer.Address.Email(text);

perhaps replace with:

CurrentCustomer.Orders[0].EmailManufacturer(text);

As more of us use ORM which generally presents the entire domain as an object graph it might be an idea to define acceptable "scope" for a particular object. Perhaps we should take the law of demeter to suggest that you shouldn't map the entire graph as reachable.

1
  • 4
    I think the Single Responsibility Principle might be taken to mean "don't have a method on your Business Object to send emails". Commented Jan 18, 2011 at 14:57
6

1) It does not violate it at all.

The code is equivalent to

var a = new ZRLabs.Yael.Pipeline("cat.jpg");
a = a.Rotate(90);
a = a.Watermark("Monkey");
a = a.RoundCorners(100, Color.Bisque);
a = a.Save("test.png");

2) As Good Ol' Phil Haack says : The Law of Demeter Is Not A Dot Counting Exercise

1

There's no problem with your example. After all, you're rotating, watermarking, etc... always the same image. I believe you're talking to a Pipeline object all the while, so as long as your code only depends on the class of the Pipeline, you're not violating LoD.

1

At heart, an object shouldn't expose its internals (data) but rather expose functions to operate with the internals.

Taking that into account, Fluent API is asking the object to work on something with its data, not asking its data.

And that doesn't violate any of the Laws of Demeter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.