4 Simple Rules of simple design part 1
Reading time: 1 minHave the mindset of better design and not best design
At times I suffer from coding paralysis. I used to want the best design up front, so I would be in a state where I would continue thinking without doing any productive work. I am beginning to realise that it's not even about the best design. It's about a better design than the one I have. With this mindset I can look for the simplest action I can take, that will improve my design, and with tests I know I can always refactor later to improve the design further.
Strive for adaptable design
One constant in life and software development is change, our code should strive to do be adaptable to change. If it's not, the code becomes more difficult to maintain, eventually being abandoned.
When you have a decision to make, make a change based on what would be easier to change, what is causing pain right now. Change it. Objects too tightly coupled? Too difficult add a new feature? When you are experiences these moments, whatever you decide, base your decision on what would be easier to change.
The Four Rules of Design
- Tests Pass
All tests should pass. Test verify that the correctness of behaviour and are reliable enough that you're comfortable refactoring -
Expresses Intent
Always keep your names relavent, from classes, methods, fields. They shoudl represent the intent. The moment it comes for you to make a change, it should be relatively easy
to find the location to make that change. -
No Duplication (DRY)
Not just code duplication, but perhaps more importantly knowledge duplication. -
Fewest elements
The smallest amount of components as possible. That means minimazing the number of classes and methods. It'll make it easier when you need to add a features, refactor, running of tests etc
Following these rules won't give you the best design, but it will almost most definitely lead to better design.