The LOD description I've seen (for example, Wikipedia, C2 Wiki) talk about not calling methods. To quote Wikipedia:
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:
- O itself
- M's parameters
- any objects created/instantiated within M
- O's direct component objects
- a global variable, accessible by O, in the scope of M
But what about accessing properties, variables or enums? For example, given this:
class FirstClass {
public SecondClass GetRelatedClass() {
return new SecondClass();
}
public enum InnerEnum {
Violated,
NotViolated
}
}
class SecondClass {
public int Property {get; set;}
public string _variable = "Danny Demeter";
}
Are any/all of these LOD violations? (Ignore the direct variable access for now, if you can..)
void Violate(FirstClass first) {
SecondClass second = first.GetRelatedClass();
var x = second.Property;
var y = second._variable;
var z = FirstClass.InnerEnum.Violated;
}
I wouldn't do the first two (whether 'official' violations or not), not quite so sure about the enum though.