Yes, it applies to properties as well, since the client of this code:
var isValid = new A().B.IsValid();
is coupled to A
and also to B
.
When fixing the law of Demeter violations, you have to balance the need for decoupling and the need to keep responsibilities clearly separated. Sometimes you can create Demeter transmogrifiers: classes that have too many unrelated methods just to comply with the law of Demeter.
Update: An example of a Demeter transmogrifier can be found in this post:
Consider, for example, someone who’s trying to kiss up to his boss:
sendFlowers(john.getManager().getSpouse())
. Applying Hide Delegate here would yield agetManagersSpouse()
method in Employee. Yuck.