law-of-demeter

Coupling, Cohesion and the Law of Demeter

The Law of Demeter indicates that you should only speak to objects that you know about directly. That is, do not perform method chaining to talk to other objects. When you do so, you are establishing improper linkages with the intermediary objects, inappropriately coupling your code to other code. That's bad. The solution would be fo...

DI and Composite Components - Design

Hi 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, which fetches data from a particular data source. Returns IDataSource. Builder - supports I...

How to restructure this code hierarchy (relating to the Law of Demeter)

I've got a game engine where I'm splitting off the physics simulation from the game object functionality. So I've got a pure virtual class for a physical body class Body from which I'll be deriving various implementations of a physics simulation. My game object class then looks like class GameObject { public: // ... private: B...

How to solve the violations of the Law of Demeter?

Me and a colleague designed a system for our customer, and in our opinion we created a nice clean design. But I'm having problems with some coupling we've introduced. I could try to create an example design which includes the same problems as our design, but if you forgive me I'll create an extract of our design to support the question. ...

When is it ok to break "the law of demeter"?

When is it ok to break "the law of demeter"? Any examples?? ...

Law of demeter or return the whole vector

Which one is better: public: const vector<int> & GetPointsVector(); private: vector<int> PointsVector; Or: public: int GetCurrentPoint(); void MoveToFirstPoint(); void MoveToNextPoint(); bool IsAtLastPoint(); size_t GetNumberOfPoints(); private: vector<int> PointsVector; ...

Law of Demeter vs. REST

The Law of Demeter (really should be the suggestion of Demeter) says that you shouldn't "reach through" an object to get at their child objects. If you, as a client, need to perform some non-trivial operation, most of the time the domain model you're working with should support that operation. REST is in principle a dumb hierarchy of o...

Law Of Demeter on Factory Pattern and Dependency Injection

hello all I have a question regarding dependency injection. say i want to create a class call it, WebGetTask WebGetTask would need a dependency to HttpService bad code 1 Code: private HttpService httpService; ... List<WebGetTask> list = new ArrayList<WebGetTask>(); for(...) { list.add(new WebGetTask(httpService)); } ...

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. ...

Does Passive View breaks the Law of Demeter?

I'm trying to understand how to use Passive View correctly. It seems to me that every examples I look at on Passive View breaks the Law of Demeter : //In the presenter code myview.mytextfield.text = "whatever"; So what's a better implementation of Passive View? ...

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<int> state; public: foo(int value) : state(new int(value)) {} friend class baz; }; class bar { private: some_weak_pointer<int> state; public: ...

Law of Demeter and Class Constructors

The Law of Demeter does not prevent passing objects into class constructors. However, it does forbid getting that same object back later and calling a method on it to get a scalar value out. Instead, a proxy method is supposed to be created that returns the scalar value instead. My question is, why is it acceptable to pass an object into...

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. The purpose of the loop is hard to understand. In pseudocode, here's where I'm at the re...

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 would say this is bad. "Code Complete" says this is increasing coupling. This method s...

Granularization of models?

I'm developing a CMS largely based on Zend Framework components. Some of the database tables for this CMS are as followed: site | id | name | ------------- locale | languageCode | regionCode | ----------------------------- site_locale // link sites with locales | siteId | languageCode | regionCode | isActive | isDefault | ------------...

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 business rules are agnostic of how they're presented. But I must pray to Demeter for forgiv...

Exercise 26 of The Pragmatic Programmer

There is a code snippet presented in The Pragmatic Programmer on page 143 as: public class Colada { private Blender myBlender; private Vector myStuff; public Colada() { myBlender = new Blender(); myStuff = new Vector(); } private doSomething() { myBlender.addIngredients(myStuff.elements()); ...

Wrappers/law of demeter seems to be an anti-pattern...

I've been reading up on this "Law of Demeter" thing, and it (and pure "wrapper" classes in general) seem to generally be anti patterns. Consider an implementation class: class FluidSimulator { void reset() { /* ... */ } } Now consider two different implementations of another class: class ScreenSpaceEffects1 { private FluidSim...

How does this code break the Law of Demeter?

The following code breaks the Law of Demeter: public class Student extends Person { private Grades grades; public Student() { } /** Must never return null; throw an appropriately named exception, instead. */ private synchronized Grades getGrades() throws GradesException { if( this.grades == null ) { this.grades = c...

How to modify code so that it adheres to the Law of Demeter

public class BigPerformance { public decimal Value { get; set; } } public class Performance { public BigPerformance BigPerf { get; set; } } public class Category { public Performance Perf { get; set; } } If I call: Category cat = new Category(); cat.Perf.BigPerf.Value = 1.0; I assume this t...