Inspectopedia
 
2024.3

Law of Demeter

Warning
New
Last modified: 03 December 2024

Reports Law of Demeter violations.

The Law of Demeter is not really a law, but specifies a style guideline: never call a method on an object received from another call. The code that follows this guideline is easier to maintain, adapt, and refactor, has less coupling between methods, less duplication, and better information hiding. On the other hand, you may need to write many wrapper methods to meet this guideline.

Example:

The above example might be better implemented as a method payInvoice(Invoice invoice) in Customer.

Use the Ignore calls to library methods and access to library fields option to ignore Law of Demeter violations that can't be fixed without changing a library.