I always start my projects with modularity in mind, but then the spec changes and I have to add conditions to account for super weird edge cases.
Or the spec changes and a function is meant to do something slightly different, but it is already referenced everywhere throughout the program so it is not feasible to re-factor. Instead, I end up having to do some really hacky workarounds that would make this sub cry.
How do I combat this? Is the answer to just make everything even more modular until the program can resist all possible changes in spec? I feel like I would end up with hundreds of hyper-specific functions that way, which is hard to keep track off.
Hm. I've kind of grown into a kind of effective lazy over the years, with ideas how to progress from lazy.
By now, if there is a requirement, I tend to write the most simple and straightforward code possible. Like don't bother with anything. Is there a requirement for different database engines? Pfsh, just hardcode some ORM to postgres during startup. No one asked for XML or YAML on an endpoint? Pfsh, hardcoded JSON decoding it is. And then just add the most boring straightline code to make whatever they want work.
And if you need to add something new, embrace copy-pasting code (for now). Just take whatever is doing 60% of what's doing what you need now, copy it, adjust it, stop bothering. Yes this violates DRY and that's good, keep reading.
However, there are two important off-ramps and de-tours to look at there.
One is the three-times rule, or the same-bug rule, or the same-change rule. If you have the exact same code in three places, try to extract some abstraction. If you have to introduce the same new feature, or fix the same bug in three or maybe two places, try to introduce an abstraction so you have to fix it once.
Code isn't duplicated because it's the same code. Code is duplicated because it changes together and because it gets fixed together.
And additionally, if you have to change a function, a class, or something, consider the open-closed principle. A class or a function should be open for extension, but closed for modification. So if a method, function, or class has to change behavior, it's good to think if you could introduce some callback, some function parameter, some interface to call into. Something like that.
This is good imo, because if you have to change a detail about a function once, you will have to change that detail more often. So it is good to add a way to add functionality by injection, without changing existing code. This avoids breaking old stuff, because you add stuff, and if the added stuff is broken, just turn it off.
This way, I've found that the systems I touch or stand up start entirely naive and stupid, but they change and mold to accept the changes in requirement the business needs. Instead of putting up an idea of these abstractions and then the requirements move entirely left field and shit becomes weird. Or you know, requirements don't change and we have some entirely dumb code running for years.
16
u/ImprovementTough261 Nov 25 '22 edited Nov 25 '22
This is something I struggle a lot with.
I always start my projects with modularity in mind, but then the spec changes and I have to add conditions to account for super weird edge cases.
Or the spec changes and a function is meant to do something slightly different, but it is already referenced everywhere throughout the program so it is not feasible to re-factor. Instead, I end up having to do some really hacky workarounds that would make this sub cry.
How do I combat this? Is the answer to just make everything even more modular until the program can resist all possible changes in spec? I feel like I would end up with hundreds of hyper-specific functions that way, which is hard to keep track off.