I've been recently struggling with keeping the design of my systems small. My problem stems from my presumptions of what the system will need.
I predict what the system will need in the future, so when the future arrives, my system can be ready for that change. Sometimes though the future never arrives for the system, it's scrap or requirements change so drastically it no longer looks like the same system.
I was suggested to read up on the "4 Rules of Simple Design". This was originally expressed by Kent Beck in the late 1990s in his popular book ExtremeProgramming.
The four rules are as follows.
- Passes the tests
- Reveals intention
- No duplication
- Fewest elements
These rules are in priority order which don't fully agree with. Being a TDD practioner, I have 100% agreement with number 1.
It's number 2 and 3 that I have slight issues with, duplication is one of my not pet peeves in programming, I feel slightly off when I see duplicated code, so much so I feel compelled to change it. Hence why I would probably place it tied at number 2.
Revealing intention reminds about how your code should be a story, having clear names, right level of abstraction, not having confusing comments. Simply put, self documenting code.
This is perhaps the one I struggle at times with, because naturally when I'm writing my code I know my thought process at the time (future Ced might not though) but having clear intention revealing code is about having empathy for our fellow programmers and our future selves.
Which leads us to "Fewest elements", this is the one that's been on my mind a lot recently. What constitutes as the "fewest" element? What is the least amount of class? what is the least amount of methods?, these are the questions that I'm constantly battling when thinking about the right approach to design.
To solve this problem in the mean time, I like to think of what Bob Martin said related to how small functions should be and instead I apply it to overall design.
"The first rule of functions is that they should be small. The second rule of functions is that they should be smaller than that"
So my rule for myself is, whatever number of elements I think I should have? I probably should have fewer elements than that.