November 27, 2014
S.T.O.L.I.D. OO Design PrinciplesJust time while we wait for folk to arrive for today's TDD training to capture my thoughts about a discussion that's going on in the room as I write.
The question is "are SOLID principles sufficient as an approach to OO design?" The answer, of course, is no.
If we are to build from the assertion that OO design is about managing dependencies (which I happen to subscribe to), then SOLID principles omit a very key part of the picture.
From conducting various experiments using simplified models of dependency networks, I learned that coupling and cohesion at the class level is a major factor in what we might call the "ripple effect" - where a small change to one class "ripples" out to impact other classes. Code where coupling between classes is high tends to result in wider ripples, and therefore a higher cost of change. Code where more dependencies are encapsulated inside classes tends to result in smaller ripples and a lower cost of change. It's simple physics.
SOLID doesn't address encapsulation, which, in my opinion, is a glaring omission.
A design principle that does is Tell, Don't Ask. Neatly summarised in that one pithy expression is the notion of classes that know stuff being given responsibility for doing the work that requires that knowledge. In other words, put the behaviour where the data is (put the methods where the fields are). This tends to lead to classes that are more cohesive, and reduces the need for coupling between classes - sharing of knowledge.
Jokingly, I propose SOLID becomes S.T.O.L.I.D., the dictionary definition of which is: "calm, dependable, and showing little emotion or animation".
So, it now goes:
Tell, Don't Ask
To mem, this seems like a more complete set of principles, addressing as it does 3 of my 4 principles of dependency management:
1. Minimise dependencies
2. Localise Dependencies
3. Stabilise Dependencies
4. Abstract Dependencies
Minimising dependencies is best achieved by writing less code, so I add two final design principles that a trump all others:
Simple Design (KISS)
Don't Repeat Yourself
But I couldn't think of a workable acronym that included those...
Posted 3 years, 6 months ago on November 27, 2014