https://stackoverflow.com/a/2891786/3924108. The Law of Demeter was refined from "Only talk to your friends" to "Only talk to your friends who share your concerns" and this refined form is called the Law of Demeter for Concerns (LoDC). All the following method calls, including the last build() call are all using the returned objects from the previous call. This is when applying the Law blends together with Object-Oriented Design. There are well-known abstract concepts in Object-Oriented programming, like encapsulation, cohesion, and coupling, that could be theoretically used to generate clear designs and good code. It seems getter methods violate the Law of Demeter almost by design. You can always update your selection by clicking Cookie Preferences at the bottom of the page. I'll also add a few links to add perspective to my above comment: https://alvinalexander.com/java/java-law-of-demeter-java-examples, https://stackoverflow.com/questions/47347068/optional-monad-and-the-law-of-demeter-in-java, https://stackoverflow.com/questions/53529338/a-factory-method-may-violate-the-law-of-demeter, https://stackoverflow.com/questions/33034777/law-of-demeter-static-property-access, https://www.tedinski.com/2018/12/18/the-law-of-demeter.html, https://programmingideaswithjake.wordpress.com/2015/02/28/law-of-demeter-and-how-to-work-with-it/, https://enterprisecraftsmanship.com/posts/law-of-demeter-and-immutability/, https://stackoverflow.com/questions/45491555/can-multiple-operations-with-streaming-break-the-law-of-demeter, https://dzone.com/articles/the-genius-of-the-law-of-demeter, https://stackoverflow.com/questions/26021140/law-of-demeter-with-data-model-objects/26021695#26021695 List of JDK classes to be exempt from this rule (I'll add classes as I go along): And the second is that direct access to fields arguably still qualifies as “sending a message,” since it is still just communication between two objects. Have a question about this project? This is a false positive as well. This would help mitigate all those calls to newOutputStream and newInputStream that return 'objects created elsewhere'. BigDecimal, They're just wrapper methods returning non-null objects. Every time a presentation tier client needs a nested business object, the business class has to change to provide the adaptor (forwarding) method. customer.getMailingAddressLine1(). It helps to Demeter. The authors of the Law managed to take these abstract concepts and distil the essence of them into a clear set of rules that are universally applicable to Object-Oriented code. Reassigning a variable with another object instance has the same effect. they're used to gather information about the pages you visit and how many clicks you need to accomplish a task. So that means while calling the getter itself might be technically legal, we can’t actually use the result. So let’s reformulate the whole Law so all points stand independently: For all classes C, and for all methods M attached to C, all objects to which M sends a message must be: Well, the law formulates what we are allowed to do in any given method M. So, let’s work backward and find out what it is exactly what this law prohibits. This rule can detect possible violations of the Law of Demeter. This would mitigate a lot of those 'object created elsewhere' flags especially when the object is simply a bean. java.lang would definitely be at the top of the list. As in, you shouldn’t even know it’s there. For more information, see our Privacy Statement. We use optional third-party analytics cookies to understand how you use GitHub.com so we can build better products.