What is Law of Demeter?

The 5th is difficult to represent in C# or Java, since they don't technically support global variables. However, in a design pattern that is similar in principle, you could have e.g. a configuration class that just contains globally-accessible static configuration values, such as (C#):

internal class MyConfiguration
{
    private static String MyConfigurationValue; // set in constructor
    MyConfiguration(){ MyConfigurationValue = DoSomethingToLoadValue(); }
    public static String GetMyConfigurationValue(){ return MyConfigurationValue; }
}

In this case (assuming the design pattern was acceptable in all other ways), the Law of Demeter would allow this, since it is globally accessible and intended to be that way.


"Tell don't ask" is a bit different.

Demeter: don't get something to get something from that to do something on the final thing.

TDA: don't retrieve "information" from another object to then make a decision on that. Simple example:

if (someList.size() == 0) { bla

vs.

if (someList.isEmpty()) { bla

In both cases you are calling a method on some other object; but there is a key difference: the first call exposes "internal" state of that other object to you; on which you then make some decision. Whereas, in the "TDA" improved second version; you leave that "status evaluation" within that other object; thereby somehow reducing coupling.

But just for the record: that second example still makes a decision based on the state of that list. From that point of view, it is just a slightly better version than option 1. Ideally, you wouldn't need such checks.