Questions tagged [law-of-demeter]

The Law of Demeter (LoD) or Principle of Least Knowledge is a design guideline for developing software, particularly object-oriented programs. In its general form, the LoD is a specific case of loose coupling.

The Law of Demeter (LoD) or Principle of Least Knowledge is a design guideline for developing software, particularly object-oriented programs. In its general form, the LoD is a specific case of loose coupling:

  • Each unit should have only limited knowledge about other units: only units "closely" related to the current unit;
  • Each unit should only talk to its friends; don't talk to strangers;
  • Only talk to your immediate friends.

The fundamental notion is that a given object should assume as little as possible about the structure or properties of anything else (including its subcomponents).

137 questions
1
vote
1 answer

Confused about the law of Demeter principle

To explain my problem, let me show you the example code with C#. interface IConstructorInfoSelector { //ConstructorInfo is System.Reflection.ConstructorInfo class. ConstructorInfo SelectConstructorInfo(Type declaringType); } class…
Jin-Wook Chung
  • 4,196
  • 1
  • 26
  • 45
1
vote
1 answer

Reconciling Law of Demeter with Models

I have a data model object User. My app also has some other data model objects, Fork and Options, for example. Users have forks and branches. My app has to run a lot of queries with some combination of User/Fork/Options etc. information. For…
Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
0
votes
1 answer

How do I apply the Law of Demeter to this?

I have an admittedly ugly query to do, to find a particular role related to the current role. This line produces the correct result: @person_event_role.event_role.event.event_roles. joins(:mission_role).where(:mission_roles => {:title =>…
DGM
  • 26,629
  • 7
  • 58
  • 79
0
votes
1 answer

Rails Associations, nilClass, try, and Law of Demeter

So I never know what to do here. Say you have Order, which has_one Member. If you call say, my_order.member.first_name where that associated member has been deleted, you can a nilClass error. I can do my_order.member.try(:first_name).. but that just…
wejrowski
  • 439
  • 5
  • 20
0
votes
0 answers

Does a.dateTimeObject.format() violates the law of Demeter?

The class Foo has a method that must return a DateTime object. Foo can be considered a parameter object (it implements an interface) class Foo implements CustomInterface{ function dateTime():\DateTimeInterface{} } The class "Bar" requires a Foo…
Vincent
  • 1,651
  • 9
  • 28
  • 54
0
votes
1 answer

Law of demeter: Exposing intern functionality, because of earlier extending?

It's a easy question. I have there some database-framework, which gives me a few methods working with them. So now i want to extend that behavior. I write a wrapper-class and add a few more methods with that functionality i wanted to extend it. Now…
0
votes
2 answers

Is this a good example of Law of Demeter?

I'm studying for an oral exam, and I wonder if I have understood Law of Demeter correctly. In essence, I have understood that the Law of Demeter aims to loosen coupling by making classes less dependent on one another, and by not giving away…
0
votes
0 answers

Breaking the "Law of Demeter" and possible solution to this issue

Recently I have read about the "Law of Demeter". Let's suppose we have: public class Restaurant { private String name; private Address address; // getters and setters... } public class Address { private String city; private…
user14204888
0
votes
1 answer

Law of Demeter and local variables inside function

I'm trying to understand Demeter's law. This example (taken from the book called 'Pragmatic programmer') confuses me.The task is to determine if the shown method call is allowed according to the Law of Demeter. public void showBalance(BankAccount…
Nemanja
  • 3,295
  • 11
  • 15
0
votes
0 answers

How to correctly pass sockets to a function's parameters

If I create a global socket object like so.. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) and define a function like so.. def function(): Would it be considered non-dry programming if I passed the…
0
votes
2 answers

Keeping controllers clean

Let's say I have some logic that's being performed on the params hash passed into a controller action. I'd like to encapsulate the logic in some methods to keep the code understandable and to keep the controller clean. I could put the methods in…
keruilin
  • 16,782
  • 34
  • 108
  • 175
0
votes
2 answers

how to obey the law of demeter when using Canvas in Javascript?

Let's suppose that I have this piece of code: function drawToCanvas() { const canvas = document.createElement('canvas'); const context = canvas.getContext('2d'); context.rect(10, 10, 100, 100); context.fill(); const…
Shokry
  • 81
  • 1
  • 7
0
votes
1 answer

Does this change improve my design with regards to the Law Of Demeter?

Let's say I need write a Wrapper for a 3rd party class which I'm not able to change. The interface of the class looks like this class Rewriter { public List getMappings(); } The wrapper looks like this class RewriterSpec { private…
helpermethod
  • 59,493
  • 71
  • 188
  • 276
0
votes
2 answers

Law of Demeter - real world issue

I'm trying to get better understand of Law of Demeter in a real world (aka my application), but I have some confuses about reasons and benefits which I get when resigning of a chain of responsibilities. I have an example, where I am considering…
acabala
  • 149
  • 1
  • 14
0
votes
1 answer

Why is better to wrap (and multiple) code for sake of Law of Demeter?

class Point { private $x, $y; public __construction ($x, $y) { $this->x = $x; $this->y = $y; } public function getX() { return $this->x; } public function getY() { return…
John Smith
  • 6,129
  • 12
  • 68
  • 123