Subscribe to the goodies

Email Address

The Direction Of The Dependency

Erik–Friday, 26 February 2016

When projects grow they become hard to change. One aspect that is not often highlighted is dependency direction. I haven’t found much material on the topic, maybe the best ideas came from this talk by Sandi Metz “Less, the path to a better design”.

Some of the main points of Sandi’s talk

The purpose of design is to reduce the cost of change, anything else is not design.
Managing dependencies is at the heart of design.

Every class used in your application can be ranked along a scale of how likely it is to undergo a change relative to all other classes.
- Sandi Metz POODR, Chapter 3, pg 54

My suggestions to choosing navigability

The class diagram of the app can express navigability with the slim arrow (->). The navigability determines the dependency direction. When in doubt about a dependency direction, we can follow the class diagram.

If Post belong_to User, User owns Post, the navigability is Post -> User and you should consider favouring depending on User in Post, rather than the other way around;

Ask yourself: “Can Post exist without User?” (and vice-versa); Usermakes sense even without Post, but it’s unlikely that a Post can exist without a User, so the navigability should be Post -> User;

Avoid User <-> Post, if you do it you will be unable to use User without a Post and vice-versa;

Classes with many associations should not hold methods about them; Failing to do so will break the SRP;