Questions tagged [law-of-demeter]

13 questions
35
votes
1 answer

Is this a good scenario to violate the Law of Demeter?

I have this code in some part of an application: long sum1 = new Multiples().ofAny(new long[] { 3, 5 }).until(32768).sum(); long sum2 = new Multiples().ofAll(new long[] { 3, 5 }).until(32768).sum(); long sum3 = new…
user218158
29
votes
4 answers

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…
14
votes
5 answers

According to Demeter's law, is a class allowed to return one of its members?

I have three questions concerning Demeter's law. Apart from classes which were specifically appointed to return objects - such as factory and builder classes - is it okay for a method to return an object, e.g. an object held by one of the class's…
user2180613
  • 1,752
  • 3
  • 14
  • 17
7
votes
3 answers

Does this violate the Law of Demeter?

Let's say I have a class SelectableEntity which has three methods, select, deselect, isSelected and count. To take a somewhat contrived example, let's say I'm building an emergency messaging application that allows me to message to…
NRaf
  • 301
  • 2
  • 10
4
votes
1 answer

What does the Law of Demeter have to do with Demeter?

Is there a story from Greek mythology about the goddess Demeter that somehow motivates the Law of Demeter? If so, I'd love to know it so I can tell it to my students when I teach this topic.
dinosaur
  • 149
  • 1
4
votes
6 answers

How to deal with Law of Demeter in the product - owner relationship?

I want to display the product, and the product card has a lot of information about the product and the owner. How to deal with Law of Demeter in this product - owner relationship? In controller I currently have: Product product =…
4
votes
1 answer

Dealing with a large interface

I'm working on a program that solves a certain type of systems of equations. The main data objects are Equation, Variable, Solution. Then I have this interface, which represents all things that I want to expose to the UI layer. public interface…
3
votes
2 answers

Demeter's law vs method chaining: when to use which?

Given this code from the Symfony framework: use Symfony\Component\HttpFoundation\Request; public function indexAction(Request $request) { $request->isXmlHttpRequest(); // is it an Ajax request? $request->getPreferredLanguage(array('en',…
agoldev
  • 189
  • 8
2
votes
4 answers

Is using getter method violating the law of Demeter?

Suppose I have a Attendance class public class Attendance { private PersonInfo personInfo; public PersonInfo getPersonInfo() { return personInfo; } } And I want to check if person is registered in the Conference class: public class…
user3153970
  • 361
  • 1
  • 2
  • 9
2
votes
2 answers

Law of Demeter and its applicability

Let's say I'd like to perform the following command: house.getFloor(0).getWall(WEST).getDoor().getDoorknob(); To avoid a NullPointerException, I'd have to do the following if: if (house != null && house.getFloor(0) &&…
q126y
  • 1,713
  • 3
  • 14
  • 24
1
vote
3 answers

Python: Is returning self in method chaining a violation of Demeter's law?

In Python it is very common to see code that uses method chaining, the main difference with code elsewhere is that this is also combined with returning an object of the same type but modified. This approach usually assumes that objects are…
0
votes
2 answers

Using Spring Boot's @ConfigurationProperties without violating Law of Demeter

My apps commonly have one or more prop holders. These holders contain config data used in the app such as: @Component @ConfigurationProperties(prefix="app.orders") @Data //lombok public class OrderProps { private int maxItems; // other…
James
  • 285
  • 1
  • 5
  • 13
0
votes
2 answers

Should an object keep a reference to a sibling object, or access through mutual owner's method

For context, I'm building a GTK+ application in C where a subclass of GtkApplicationWindow creates and displays a subclass of GtkToolbar and a GtkNotebook (a widget with multiple pages that can be displayed alternately by a member function). There…