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
0
votes
2 answers

Law of Demeter confusion with the simple classes

I'm working on computational geometry project. I have classes representing geometrical objects: Point, LineSegment and class, which performs calculations on those objects: Geometry. I'm confused with the Law of Demeter and…
Dariusz
  • 610
  • 1
  • 6
  • 14
0
votes
2 answers

Do the 'Array#dig' or 'Hash#dig' methods violate the Law of Demeter?

The dig method: Extracts the nested value specified by the sequence of idx objects by calling dig at each step, returning nil if any intermediate step is nil. This means that: [1, {foo: :bar}].dig(1, :foo) returns :bar by performing a lookup on…
gnerkus
  • 11,357
  • 6
  • 47
  • 71
0
votes
1 answer

Design question: Should the client both create the session and the socket?

I have three classes: Client Session Socket Both Session & Socket depeand on the Client to create both objects. A Session depeands on a Socket and no sockets are created without a session. Should the Client have a function that creates a Session…
the_drow
  • 18,571
  • 25
  • 126
  • 193
0
votes
2 answers

Php, why its so bad to break Demeter's law?

I know its considered bad: $this->laptop->getKeyboard()->getTouchpad()->getLbutton(); or $this->laptop->getKeyboard()->getCapslock()->isLedOn(); its explained as "we dont want tons of -> -> -> ->" - but I wanted to make this construction physical,…
John Smith
  • 6,129
  • 12
  • 68
  • 123
0
votes
0 answers

How Can I Improve This Structure Of Tables/Models?

I have the following set of Models / Associations: Whilst this makes sense from on object oriented point of view, it introduces a huge distance between, for example; a NewsItem and the Photo.image that represents it in the UI. An obvious…
0
votes
1 answer

LoD: calling components` components - allowed or not?

Is it allowed to call components' components according to the Law of Demeter? By component I mean an object which was "exclusively" injected into the container or was created in the container which has the same life cycle with it's container For…
astef
  • 8,575
  • 4
  • 56
  • 95
0
votes
1 answer

Delete item from array and law of demeter combo issue

I have my show action as below: def show @car = Car.friendly.find(params[:id]) @cars = @car.other_cars_in_showroom end My Car model with appropriate method is: def other_cars_in_showroom cars = self.showroom.cars cars.delete(self) …
rctneil
  • 7,016
  • 10
  • 40
  • 83
0
votes
2 answers

IntelliJ Idea's Law of Demeter inspection. False positive or not?

Suppose the next class interface Thing { void doSomething(); } public class Test { public void doWork() { //Do smart things here ... doSomethingToThing(index); // calls to doSomethingToThing might happen in various places across…
superjugy
  • 301
  • 4
  • 12
0
votes
1 answer

Applying Law of Demeter in a way that does not improve design

Suppose, we have the following Boy class that tries to arrange a date with a Girl by analyzing her schedule (example in Java): public class Boy { public boolean tryArrangeDate(Girl girl, Date time) { boolean success = true; for…
Volodymyr Tsukur
  • 275
  • 1
  • 3
  • 10
0
votes
1 answer

How to properly use Backbone views and router

I have a sequence of page states that essentially mimic a shopping cart checkout process like so: var ItemsCollection = Backbone.Collection.extend({ model: ItemModel, url "/items" }); var ItemsView = Backbone.View.extend({ //…
flynn
  • 1,572
  • 2
  • 12
  • 26
0
votes
2 answers

How to access variables from an object returned from a getter

I have a Quad class (for my sprites) and within this class are various variables for altering the state of said Quad. So I can simply make a sprite like so: Quad hero = new Quad(); I have a Resource class where all of my sprites are…
Zippy
  • 3,826
  • 5
  • 43
  • 96
0
votes
0 answers

Spotting law of demeter violations in c# code

I'm thinking about to build a tool that spots law of demeter violations in c# code. Obviously, I don't want to reinvent the wheel and build another tool if there is some alternative available, but I had no lucky on searching for such tool…
rpepato
  • 127
  • 4
0
votes
4 answers

How to write read-only accessor functions in an aggregate root class?

Overall design: I have an aggregate class C that contains N member variables of type M_i, i = 1 ... N that each have a common write-only update() interface as well as class-specific read-only accessor functions [F]un_i(), [F] = any letter, i = 1 ..…
TemplateRex
  • 69,038
  • 19
  • 164
  • 304
0
votes
2 answers

Ideal code following the law of demeter and is testable (Dependency injection)?

I was reading through testable code that follows LoD, but got all messed up in my head. So please any guidance regarding this piece of code would be appreciated. public class HouseConfiguration { private final int noOfDoors; private final…
Narendra Pathai
  • 41,187
  • 18
  • 82
  • 120
0
votes
3 answers

Help me refactor this loop

I am working on the redesign of an existing class. In this class about a 400-line while loop that does most of the work. The body of the loop is a minefield of if statements, variable assignments and there is a "continue" in the middle somewhere. …
WW.
  • 23,793
  • 13
  • 94
  • 121
1 2 3
9
10