Skip to main content
edited tags
Link
Christophe
  • 80.6k
  • 11
  • 132
  • 199
Tweeted twitter.com/StackSoftEng/status/1364273854462492675

The "Law of Demeter"Law of Demeter makes sense in some obvious cases.

# better
dog.walk()

# worse
dog.legs().front().left().move()
dog.legs().back().right().move()
# etc.

But in other cases it seems to lead to an interface that is too wide, and doesn't really feel like it "hides" any knowledge.

# Is this worse?
print(account.user().fullName())
print(account.user().socialSecurityNumber())

# Is this better?
print(account.userFullname())
print(account.userSocialSecurityNumber())

It feels like account has methods for things that really aren't anything to do with accounts now, just to avoid a technical violation of the Law of Demeter. Instictively, account.userFullName() is pretty "code smelly".

Is anyone aware of any more specific guidelines or refinements of the LoD that helps account for the difference between the "dog" cases where the principle clearly makes sense, and the cases where it doesn't? And how do you avoid the LoD leading to over-wide interfaces?

One principle I have heard is that it matters less in a context of immutability, but many have disputed this.

The "Law of Demeter" makes sense in some obvious cases.

# better
dog.walk()

# worse
dog.legs().front().left().move()
dog.legs().back().right().move()
# etc.

But in other cases it seems to lead to an interface that is too wide, and doesn't really feel like it "hides" any knowledge.

# Is this worse?
print(account.user().fullName())
print(account.user().socialSecurityNumber())

# Is this better?
print(account.userFullname())
print(account.userSocialSecurityNumber())

It feels like account has methods for things that really aren't anything to do with accounts now, just to avoid a technical violation of the Law of Demeter. Instictively, account.userFullName() is pretty "code smelly".

Is anyone aware of any more specific guidelines or refinements of the LoD that helps account for the difference between the "dog" cases where the principle clearly makes sense, and the cases where it doesn't? And how do you avoid the LoD leading to over-wide interfaces?

One principle I have heard is that it matters less in a context of immutability, but many have disputed this.

The Law of Demeter makes sense in some obvious cases.

# better
dog.walk()

# worse
dog.legs().front().left().move()
dog.legs().back().right().move()
# etc.

But in other cases it seems to lead to an interface that is too wide, and doesn't really feel like it "hides" any knowledge.

# Is this worse?
print(account.user().fullName())
print(account.user().socialSecurityNumber())

# Is this better?
print(account.userFullname())
print(account.userSocialSecurityNumber())

It feels like account has methods for things that really aren't anything to do with accounts now, just to avoid a technical violation of the Law of Demeter. Instictively, account.userFullName() is pretty "code smelly".

Is anyone aware of any more specific guidelines or refinements of the LoD that helps account for the difference between the "dog" cases where the principle clearly makes sense, and the cases where it doesn't? And how do you avoid the LoD leading to over-wide interfaces?

One principle I have heard is that it matters less in a context of immutability, but many have disputed this.

Question Protected by gnat
Became Hot Network Question
Source Link
samfrances
  • 1.1k
  • 1
  • 10
  • 15

Law of Demeter and over-wide interfaces

The "Law of Demeter" makes sense in some obvious cases.

# better
dog.walk()

# worse
dog.legs().front().left().move()
dog.legs().back().right().move()
# etc.

But in other cases it seems to lead to an interface that is too wide, and doesn't really feel like it "hides" any knowledge.

# Is this worse?
print(account.user().fullName())
print(account.user().socialSecurityNumber())

# Is this better?
print(account.userFullname())
print(account.userSocialSecurityNumber())

It feels like account has methods for things that really aren't anything to do with accounts now, just to avoid a technical violation of the Law of Demeter. Instictively, account.userFullName() is pretty "code smelly".

Is anyone aware of any more specific guidelines or refinements of the LoD that helps account for the difference between the "dog" cases where the principle clearly makes sense, and the cases where it doesn't? And how do you avoid the LoD leading to over-wide interfaces?

One principle I have heard is that it matters less in a context of immutability, but many have disputed this.