Prefer to change the code rather than write a workaround
I can't count how many times
I've heard programmers talking about writing some new piece of code
to work around the behavior of some other code which they don't want to change.
You are a programmer - you are allowed to change code!
In fact, it's your job!
Just change the code to do what you want!
Fix the bug! Change the behavior! Add the feature!
- Log message that's noisy and useless?
Delete the logging code!
- API that doesn't expose what you need?
Add a new parameter!
- Library that does something horrible that all your software depends on?
Turn off the horrible behavior and remove your dependency on it!
- Open source software that's doing bad stuff?
Patch it locally and send the patch upstream!
I'm talking about situations where you know how to solve your problem,
but you've chosen to implement that solution in some additional layer of code on top,
rather than changing the original problematic code.
As
has
been
expressed
many
times
before,
code is a liability;
the more code you have, the worse off you are.
So just change the original problematic code instead!
There are some basic technical excuses one might give for why you aren't able to change the code.
For example:
- But I'm not allowed to touch this code!
- But I don't know how to change it!
- But I don't know where the source code is!
- But I can't build this software!
- But I can't run the tests!
- But I can't use a modified version of this software!
Some people work in an environment where these excuses are valid,
and they might find it hard to believe,
but there are many environments where these excuses don't apply.
Try to move from the former to the latter:
don't use software where these things are an issue,
make software open-source rather than closed-source,
and quit your job in favor of work that has good developer tools.
There are also some social/political excuses.
For example:
- But changing the code means we have to talk to the people who own this code!
So what?
They'll probably be happy that you're looking at their code and trying to improve it!
If they aren't happy, then maybe what you're trying to do is evil anyway,
and you should be thankful that they're stopping you.
If what you're doing is good, but they're still not happy,
then fork and upstream later.
- But changing things will impact other users!
If what you are trying to do is good for them,
they will be happy,
and you should embrace the costs and benefits of coordinating with them.
If what you are trying to do is bad for them,
then that's a sign that you shouldn't be doing it.
If you choose to do so anyway,
you can surely figure out a way that they don't have to use your changed version.
- But we haven't budgeted time to do it the right way!
Then you never will budget time to do it the right way.
Do it the right way now or you will suffer forever.
- But this is supposed to be a quick experiment!
You can perfectly well do a quick experiment by modifying the original code;
indeed, it should be quicker to do it that way.
Fear not!
Just change the code!