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

Where should my object construction code be while respecting the Law of Demeter?

I've been watching Google's clean code talks by Misko Hevery. These talks say: ask for dependencies in the constructor, so other programmers can see exactly what is needed up front, to instantiate an instance of a given object (law of demeter).…
Greg K
  • 10,770
  • 10
  • 45
  • 62
2
votes
2 answers

Is this a violation of the Law of Demeter?

Is this a violation of the Law of Demeter? private void MoveEmptyCells() { IEnumerable cells = this.internalGrid.GetAllEmptyCells(); foreach(Cell cell in cells) { cell.RowIndex += this.moveDistance; // violation…
bitbonk
  • 48,890
  • 37
  • 186
  • 278
2
votes
1 answer

Law of Demeter can easily be bypassed?

Is it always possible to work around the Law of Demeter simply by creating more methods? Some people mention that this is not valid (http://wiki.c2.com/?LawOfDemeterIsHardToUnderstand), but it seems like it should be valid, since the Law of Demeter…
Victor
  • 743
  • 1
  • 5
  • 16
2
votes
1 answer

Retrieve current user in Symfony app while respecting LoD

I'm having some issues understanding how the Law of Demeter should be applied in some cases with Symfony's DI system. I have some factory that requires to access current logged in user in the application. To do that I need to require…
2
votes
1 answer

Why PMD give me law of demeter violation in this get function?

I am using IntelliJ PMD plugin, and it gives me LOD violation, on the if(keys[i].equals(key)). Keys is an object in the same class within the function. public Object get(Object key) { int n,i; for(i=0,n=0;i=…
onurtore
  • 195
  • 1
  • 11
2
votes
3 answers

Does Law of Demeter also account for standard classes?

Assuming the following code: requiredIssue.get().isDone() where requiredIssue is an Optional and it has been verified that requiredIssue.isPresent(). Does this code break the Law of Demeter? Technically there is a tight coupling here between my…
AdHominem
  • 1,204
  • 3
  • 13
  • 32
2
votes
2 answers

Law of Demeter in Java

I have been building a RTS to improve my Java skills. I have been reading a lot about the Law of Demeter because I want to keep my code clean but I'm still quite confused! At the moment I have some code like this in the view to show how many of a…
Tom F
  • 443
  • 1
  • 4
  • 16
2
votes
3 answers

Dependency Injection with Guice and "Law of Demeter"

A trivial example from the "Dependency Injection with Guice" break the "Law of Demeter." At least as PMD mean it. public class BillingModule extends AbstractModule { @Override protected void configure() { …
Andriy Kuba
  • 8,093
  • 2
  • 29
  • 46
2
votes
1 answer

Law of Demeter and DAO pattern

Here's a method in my Spring/Hibernate website's code that exemplifies my codebase: public class UserVoteServiceImpl implements UserVoteService { @Autowired UserRepository userRepository; public static int getUserScore(long userId) { …
Brandon Yarbrough
  • 37,021
  • 23
  • 116
  • 145
2
votes
1 answer

"Bridging" (Connecting) methods between distant classes

Lets suppose I have Planet and Star classes and GameLogic class in the following way //Planet.java package Game; class Planet //invisible outside of package { public String getType() {return "Im a Planet";} } . //Star.java package Game; class…
jagnelo
  • 47
  • 1
  • 7
2
votes
1 answer

DI and Composite Components - Design

I'm designing a new component to a system, trying to follow the various guidelines on DI so we get payback in terms of isolation, mocking etc. So I have the following components (illustrated as an abstraction): Fetcher - supports IFetcher,…
Duncan
  • 10,218
  • 14
  • 64
  • 96
2
votes
0 answers

Doesn't it hurt Demeter's law when using services/factories in model?

class ForumThread { /** * @return bool */ public function findBadLanguage ($inWhat) { return (bool)rand(0,1); } /** * @return */ public function add ($threadName) { if…
John Smith
  • 6,129
  • 12
  • 68
  • 123
2
votes
1 answer

PHP MVC DI practical exaple of Law of Demeter with Router, Controller and Model

Struggling with MVC in PHP. My concerns grew even bigger after watching this: https://www.youtube.com/watch?v=RlfLCWKxHJ0 According to LoD my Router class should only know the Request Uri to load proper Controller class. However my Conroller should…
2
votes
1 answer

What is the best way for a mesh class structure in Delphi?

I want to create a triangluar mesh structure in Delphi XE5. The main TMyMesh class has generic TObjectLists to hold the list of vertices, faces, etc. Let's say I have to calculate somthing for every face of the mesh. I could let take the TMyMesh…
user3384674
  • 759
  • 8
  • 28
2
votes
1 answer

Why is this code thought to violate Law of Demeter?

Here's a method from my Android app: public ViewHolderBase buildView(PlayerResult playerResult) { View result = inflater.inflate( R.layout.player_result, null); this.helper = new ViewHelper(result); ViewHolderBase holder…
Konrad Morawski
  • 8,307
  • 7
  • 53
  • 91