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
2
votes
1 answer

When dealing with the Law of Demeter, what's an example of a situation where you'd need to change the consumer class, due to over-delegation?

I often find myself doing lots of delegating. In Ruby Science, it says: Many delegate methods to the same object are an indicator that your object graph may not accurately reflect the real world relationships they represent. and If you find…
ben
  • 29,229
  • 42
  • 124
  • 179
2
votes
2 answers

Deep class composition and the Law of Demeter

Evening. I'm having trouble finding an appropriate design pattern for some situations of deep composition. Let me present an example. Let's say we have a class of type Corporation that has many classes of type Subsidiary that have many classes of…
2
votes
3 answers

Does the law of Demeter also apply to standard ActiveRecord object methods?

Say you have a class, Car, which has a Driver. If you wanted to access the driver's age, you would do: @car.driver_age Instead of @car.driver.age If you have delegated the driver's age attribute in the Car model (with prefix set to true). However,…
railsuser400
  • 1,013
  • 1
  • 14
  • 31
2
votes
1 answer

The Law of Demeter

I recently posted a question on stackoverflow where I did something to effect of @period_registration.period.event However, it was suggested that I do something like the following: def event period.event end @period_registration.event My general…
Noah Clark
  • 8,101
  • 14
  • 74
  • 116
2
votes
3 answers

Trying to understand the Law of Demeter as it applies to my code

I have a simple Store class that contains an Inventory. The Inventory contains a list of Items. In order to modify one of the Items in the Inventory, I'd have to write: Store store( /*parameters*/…
JamesGold
  • 795
  • 2
  • 8
  • 24
1
vote
1 answer

Law of Demeter doesn't make sense in my case

Looking on this answer I understand that you should not copy private pointers using friendship in C++ like I did in my program: class bar; class foo { private: some_smart_pointer state; public: foo(int value) : state(new int(value)) {} …
the_drow
  • 18,571
  • 25
  • 126
  • 193
1
vote
2 answers

Does the RSpec DSL violate the law of Demeter?

This may be a naive question, but does RSpec's testing DSL violate the law of Demeter? Here's an example of the RSpec DSL from http://rspec.info: bowling.score.should eq(0) From a Demeter perspective, this seems to me indistinguishable from this…
dan
  • 43,914
  • 47
  • 153
  • 254
1
vote
3 answers

What is the name of this programming rule?

There is a programming "rule" that says that a method should instead of asking for 'x' when it needs to know 'x.y.z', ask directly for 'z'. I just can't remember the name.
Sverre Rabbelier
  • 1,456
  • 2
  • 16
  • 22
1
vote
1 answer

Does this method call violate the Law Of Demeter?

Say you have something like the following (sadly, I'm not allowed to post the original code): public void foo() { MyObject obj = getMyObject(); bar(obj); } public void bar(MyObject obj) { Type type = new…
helpermethod
  • 59,493
  • 71
  • 188
  • 276
1
vote
1 answer

Is has_many => :through a violation of the Law of Demeter?

If it is, what's its defence? And if it isn't, why isn't it?
snowangel
  • 3,452
  • 3
  • 29
  • 72
1
vote
1 answer

How Law of Demeter and Vector interacts?

So i have this code: Public class Worker{ private String gender; ... public Boolean isMale(){ return gender=="Male"; } } Public class Business{ private Vector vWorkers; ... public void showMaleWorkers(){ …
Tegu
  • 11
  • 1
1
vote
1 answer

Understanding Demeter's Law

I'm trying to understand the SOLID principles behind OOP and came across with this doubt. Following the previous class diagram, I am going to calculate the base_cost for a Vehicle. For such, I need to access the horsepower and the tax and then…
1
vote
1 answer

Do objects saved in state/passed as props in React violate the law of Demeter?

so I've started using the law of Demeter recently(heard about it a month ago) and I am currently also learning react. But let's take this component function CatImages(props) { return props.data.imgSrc.map((cat, index) => (
Mihai Marinescu
  • 741
  • 8
  • 22
1
vote
1 answer

Direct component object

I don't understand what phrase direct component object means in context of Law of Demeter's article. As I can see the term was taken from David Block's article. So, what is the term and where can I get real life examples and more info about it?
Fox Amadeus
  • 196
  • 1
  • 1
  • 11
1
vote
1 answer

React native navigation params and demeter law

When you want to access params in a screen component passed through navigation (react native navigation), you have to do it like this for example: this.myParameter = this.navigation.state.params.myParameter Does this break demeter law ? And what…