Skip to main content
Removed unneeded parentheses in code snippet
Source Link
CaTs
  • 197
  • 3

Adding more methods to an interface is not always the solution to the Law of Demeter. You do not need to distinguish these cases, you need to find a different way to resolve them.

From the wikipedia page:

On the other hand, at the class level, if the LoD is not used correctly, wide (i.e. enlarged) interfaces may be developed that require introducing many auxiliary methods. This is due to poor design rather than a consequence of the LoD per se. If a wrapper method is being used, it means that the object being called through the wrapper should have been a dependency in the calling class.

Take your second example:

print(account.user().fullName())
print(account.user().socialSecurityNumber())

Does your caller here need to interact with an account at all? Remove account as a dependency and provide user as an argument instead:

print(user().fullname())
print(user().socialSecurityNumber())

Adding more methods to an interface is not always the solution to the Law of Demeter. You do not need to distinguish these cases, you need to find a different way to resolve them.

From the wikipedia page:

On the other hand, at the class level, if the LoD is not used correctly, wide (i.e. enlarged) interfaces may be developed that require introducing many auxiliary methods. This is due to poor design rather than a consequence of the LoD per se. If a wrapper method is being used, it means that the object being called through the wrapper should have been a dependency in the calling class.

Take your second example:

print(account.user().fullName())
print(account.user().socialSecurityNumber())

Does your caller here need to interact with an account at all? Remove account as a dependency and provide user as an argument instead:

print(user().fullname())
print(user().socialSecurityNumber())

Adding more methods to an interface is not always the solution to the Law of Demeter. You do not need to distinguish these cases, you need to find a different way to resolve them.

From the wikipedia page:

On the other hand, at the class level, if the LoD is not used correctly, wide (i.e. enlarged) interfaces may be developed that require introducing many auxiliary methods. This is due to poor design rather than a consequence of the LoD per se. If a wrapper method is being used, it means that the object being called through the wrapper should have been a dependency in the calling class.

Take your second example:

print(account.user().fullName())
print(account.user().socialSecurityNumber())

Does your caller here need to interact with an account at all? Remove account as a dependency and provide user as an argument instead:

print(user.fullname())
print(user.socialSecurityNumber())
added helpful quote from wikipedia page
Source Link
CaTs
  • 197
  • 3

Adding more methods to an interface is not always the solution to the Law of Demeter. You do not need to distinguish these cases, you need to find a different way to resolve them.

From the wikipedia page:

On the other hand, at the class level, if the LoD is not used correctly, wide (i.e. enlarged) interfaces may be developed that require introducing many auxiliary methods. This is due to poor design rather than a consequence of the LoD per se. If a wrapper method is being used, it means that the object being called through the wrapper should have been a dependency in the calling class.

Take your second example:

print(account.user().fullName())
print(account.user().socialSecurityNumber())

Does your caller here need to interact with an account at all? Remove account as a dependency and provide user as an argument instead:

print(user().fullname())
print(user().socialSecurityNumber())

Adding more methods to an interface is not always the solution to the Law of Demeter. You do not need to distinguish these cases, you need to find a different way to resolve them.

Take your second example:

print(account.user().fullName())
print(account.user().socialSecurityNumber())

Does your caller here need to interact with an account at all? Remove account as a dependency and provide user as an argument instead:

print(user().fullname())
print(user().socialSecurityNumber())

Adding more methods to an interface is not always the solution to the Law of Demeter. You do not need to distinguish these cases, you need to find a different way to resolve them.

From the wikipedia page:

On the other hand, at the class level, if the LoD is not used correctly, wide (i.e. enlarged) interfaces may be developed that require introducing many auxiliary methods. This is due to poor design rather than a consequence of the LoD per se. If a wrapper method is being used, it means that the object being called through the wrapper should have been a dependency in the calling class.

Take your second example:

print(account.user().fullName())
print(account.user().socialSecurityNumber())

Does your caller here need to interact with an account at all? Remove account as a dependency and provide user as an argument instead:

print(user().fullname())
print(user().socialSecurityNumber())
Source Link
CaTs
  • 197
  • 3

Adding more methods to an interface is not always the solution to the Law of Demeter. You do not need to distinguish these cases, you need to find a different way to resolve them.

Take your second example:

print(account.user().fullName())
print(account.user().socialSecurityNumber())

Does your caller here need to interact with an account at all? Remove account as a dependency and provide user as an argument instead:

print(user().fullname())
print(user().socialSecurityNumber())