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

Is this a violation of the Law of Demeter? vs. readable code

The code below brakes apparently the Law of Demeter, i.e. methods getServer().methodx(...). From other side it looks pretty compact = better readable? abstract class BaseManager { ResultSet find(String searchText) { return…
partinis
  • 139
  • 12
1
vote
1 answer

Demeter Law - Calling a method of a class inside another method of a different class

If I have a class C containing a method f that takes as argument an object of type D (another class I defined) If I call the methods of the object D inside of the method f, will I be violating the law of Demeter? and why? Ex: public C { public…
1
vote
2 answers

Preserving Law of Demeter with ArrayLists

If I have an ArrayList of objects then any time I need to call any method on a member of the ArrayList I need to do it like this: list.get(i).doSomething(); This looks suspiciously like a Law of Demeter violation. I can not see any way of getting…
kabeersvohra
  • 1,049
  • 1
  • 14
  • 31
1
vote
1 answer

law of demeter and planeshift

The source code of this game is open source, so I decided to check it out. In it, I found something like: // This ActionManager is basically a controller like in the MVC pattern. void ActionManager::HandleQueryMessage(csString xml, Client*…
Gam
  • 1,254
  • 1
  • 9
  • 18
1
vote
1 answer

How Adapter Pattern help in implementing Law Of Demeter

Law of Demeter (LOD) discourages long chain of calling. It says to call methods only on the objects directly composed within the class, or the objects created inside a method, objects passed as parameters in methods. If B b; is a field in class A…
nits.kk
  • 5,204
  • 4
  • 33
  • 55
1
vote
1 answer

How to apply law of demeter in a rails for loop

I've got a rails app that has a Users model that have one User_profile. When I want to show multiple users on a page i use a rails loop like: <% @users.each do |user| %> <%= user.user_profile.city %> <% end %> Which shows the users city of…
Mischa
  • 2,069
  • 4
  • 23
  • 32
1
vote
1 answer

Law of Demeter - is it really communicating only to friends?

I read some article on Law of Demeter and it gets me confused. It states that something like this: var width = mapControl.get_mapState().getMapRange().getSize().get_width(); Should be replaced by this: var mapState = mapControl.get_mapState(); var…
RobertPorter
  • 542
  • 6
  • 16
1
vote
1 answer

Law of Demeter and angular controller DI

I was reading http://misko.hevery.com/attachments/Guide-Writing%20Testable%20Code.pdf (see, especially, page8) and watching Misko's Youtube videos on writing testable code, and it occurs to me that the way Angular does DI forces you to break the law…
Robert Balicki
  • 1,583
  • 2
  • 16
  • 24
1
vote
1 answer

Alternative for multiple 'proxy' methods in Java

If I have 3 classes, let's for arguments sake call them: MainActivity GLRenderer OtherClass Please Note that GLRenderer is not purely a proxy-class but does contain some proxy-methods. The MainActivity initialises an int called value. Now, if…
Zippy
  • 3,826
  • 5
  • 43
  • 96
1
vote
3 answers

How can I keep separation of concerns when using a grid in the presentation layer (esp. .NET)?

In a three-tier model (presentation-business-data access layers), I can consistently keep my lower layers agnostic of the upper layers. For example my data access layer never knows how it is presented or what busines rules are operated on it. My…
1
vote
4 answers

Law of Demeter violation proves useful. Am I missing something?

I have some code like this in my application. It writes out some XML:- public void doStuff( Business b, XMLElement x) { Foo f = b.getFoo(); // Code doing stuff with f // b is not mentioned again. } As I understand it, the Law of Dementer…
WW.
  • 23,793
  • 13
  • 94
  • 121
1
vote
1 answer

Law of Demeter - The pragmatic programmer

I have some questions considering the exercises in "the pragmatic programmer". It says: 1. public void showBalance(BankAccount acct) { Money amt = acct. getBalance() ; printToScreen(amt .printFormat()) ; } The variable acct is passed in as a…
Absolem
  • 139
  • 1
  • 5
1
vote
2 answers

Follow Law of Demeter when using collections?

Within Ruby on Rails (or any other language with a collection...) is it necessary to break Law of Demeter violations up when querying something simple like a count? class Survey has_one :kingdom def first_survey? # Should this be broken…
Gavin Miller
  • 43,168
  • 21
  • 122
  • 188
1
vote
1 answer

Domain Data Structures that hold Domain objects?

I have a position, and some entities that use position as it's identifier (geography, biome, and so on). If I want to get acess to them, I would need to retrieve each one by it's position, which would cause repeated code. On the other hand, I could…
1
vote
1 answer

Do Navigation Properties in the Entity Framework Break The Law Of Demeter?

I believe this is a clear yes/no question I am asking, and regardless of implementation either it breaks the law or it does not. So my question is, do navigation properties created in the Entity Framework Model break The Law Of Demeter? I think they…
atconway
  • 20,624
  • 30
  • 159
  • 229