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.
This is pretty much how I work too, but it's easy to say it out loud. It's a skill that experience gives you, and I think most people have to first walk the path to understand this mindset
I think I started about 13 - 15 years ago after a university degree, as a java backend dev for a back in the day pretty major german gaming platform. Stayed there for 5-6 years, until the company entered a meltdown and restructuring.
At that time, about 7 years ago, I kinda had the choice of either being a build server and CI sysadmin - but without any kind of control of how builds should be - or a technical leadership position to rebuild the infrastructure entirely at what grew my current place of employment. I figured that making shitty decisions of other people work on jenkins work wasn't in the best of my futures.
But then again, dealing with two to four dozen entirely different dev-teams does broaden horizons. Good teams make projects work, almost regardless of technology. Bad teams can drive all technology into the ground. Nothing has meaning beyond a certain point.
While I am very close to where you are, my motto is, every outside function is a way to document code. Does using an outside function or method add to readibility ? If not, you're doing something wrong. Outside functions are not used because they are bug free, they are just there because the name deciphers something that is happening. Otherwise, I'll just copypaste something that's used somewhere else. In this way the function can be edited to needs. Using same function in the long run does not bring benefits to business. It's all about readability and understandability.
And then there come the colleagues who just don’t want it being readable and go refactor. Suddenly everything is generic, is a strategy, abstraction layer, factory, or any sort of pattern even though it was only 2 slightly alike classes doing similar but different things.
And then it’s really hard to understand and debug, but it’s still „my code“ so i’m responsible to fix bugs in this weirdness.
Just create a project of files organized alphabetically, like Websters Dictionary, with a function and/or data structure for every word in the English language, with the main function in your "m" module. That way you have a function or structure for every possible use case. Programming languages are too small, they need more features.
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.