So I was thinking the other day about software development, and I realize there is a general consensus among the development community that is very important to manage complexity, many people seem to talk about managing complexity but sometimes I feel its so easy to throw this term in the air like that to “manage complexity” but when then time comes most people forget about and for whatever reason, very complex design and source code gets produced. I am definitely not the only one experiencing this, every time you work with an existing code base and realize the painful process of introducing a change.
I know software needs to be designed in a way that is easy to understand and change, but sometimes we forget all that and jus want to get the job done.
Fortunately there is a very simple nugget of wisdom that we all can follow in order to “manage complexity” and that is don’t make the software more complex that it needs to be, and that is the to use encapsulation.
In my experience the most common form of complexity in software development comes from introducing too many dependencies, specially if some of those dependencies don’t make a lot of sense. However other kind of complexities can be seen from too many software hierarchies or the wrong hierarchy design.
As a general rule don’t introduce hierarchies in the form of class or object inheritance, only do that very very very high up in the hierarchy for very general objects that closely relate to the actual problem domain, and keep the hierarchies very flat, the flatter the better: for example use an interface and classes that implement that interface but no more, or use a base class and a class that derive from that, but no more.
This will make sure you object hierarchies remain clean and simple if you have hierarchies 3 or more levels deep, your are probably using a wrong design, I cannot stress the importance of this sentence: Don’t specially using inheritance do it using object composition.
Using object composition is a way of encapsulating a “special” behavior, which will make it easier to understand, test, and therefore change if needed later on.
I mention before dependencies and you might think that by using object composition will lead to more dependencies, in a way it will but dependencies are good if they designed in a why that higher level objects depend on simple lower level objects, and what I say lower/higher, I mean the importance of responsibility.