Inspectopedia 2025.3 Help

Law of Demeter

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:

boolean pay(Customer c, Invoice invoice) { int dollars = c.getWallet().contents; // violation if (dollars >= invoice.getAmount()) { Wallet w = c.getWallet(); w.subtract(invoice.getAmount()); // violation return true; } return false; }

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

Locating this inspection

By ID

Can be used to locate inspection in e.g. Qodana configuration files, where you can quickly enable or disable it, or adjust its settings.

LawOfDemeter
Via Settings dialog

Path to the inspection settings via IntelliJ Platform IDE Settings dialog, when you need to adjust inspection settings directly from your IDE.

Settings or Preferences | Editor | Inspections | Java | Data flow

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.

Inspection ID: LawOfDemeter

Inspection options

Here you can find the description of settings available for the Law of Demeter inspection, and the reference of their default values.

Ignore calls to library methods and access to library fields

Option ID:

ignoreLibraryCalls

Default value:

Selected

Suppressing Inspection

You can suppress this inspection by placing the following comment marker before the code fragment where you no longer want messages from this inspection to appear:

//noinspection LawOfDemeter

More detailed instructions as well as other ways and options that you have can be found in the product documentation:

Inspection Details

By default bundled with:

IntelliJ IDEA 2025.3, Qodana for JVM 2025.3,

Last modified: 03 December 2025