3 ms·
Chesterton's Fence is always worth keeping in mind when maintaining code too. Sometimes you'll come across some crazy nested conditional that seems to be check
by Lazare 10y ago
Chesterton's Fence is always worth keeping in mind when maintaining code too.
Sometimes you'll come across some crazy nested conditional that seems to be checking for cases that can't even happen. It's tempting to say "this is nuts, I'll just rewrite this whole overgrown method into two lines!". Sometimes that's even a good idea. But some programmer before you thought that crazy code was worth writing, and you might want to check `git blame` and see if you can figure out why.
Example: A while back I ran across a pretty rare race condition in some pretty gnarly distributed code. But there was an obvious fix, so I opened up the file where the fix should be made and found...
...someone else already had made that fix. Except it was in a conditional block which boiled down to "if (false) {...}". The fix would never run! No matter, it would be the work of 10 seconds for me to fix the conditional and push this code to production.
But first, I checked the logs. One of my colleagues had written this code (so that the fix would run) about two years ago. And then 20 minutes later he had committed a hotfix to stop the code running. Reading between the lines, the "obvious fix" clearly broke something less obvious and even more important somewhere else in the gnarly distributed code. That "broken" conditional wasn't a bug, it was a fence, and it had been erected for a good reason. And the race condition I found was really rare, and the whole gnarly distributed code is slated to be refactored next year, so...I left it.
(Now, the lack of comments explaining all that in the code base, and the fact the broken code was still in the code base just locked behind an always false conditional...those were issues. Then again, stuff like that happens in real code bases, even if they shouldn't. If you expect live to be completely tidy and well behaved, reality is going to be really confusing sometimes.)