(See also the discussion when I posted this on HN: https://news.ycombinator.com/item?id=25198597 ).
That's not what I intended, which is why I have this in there:
> Often you’ll jump into code to fix a bug, investigate an issue or answer a question.
> When you do so, improve it. This doesn’t mean you rewrite it, or upgrade all the libraries it depends on, or rename all the variables.
> You don’t need to transform it.
> But you should make it better. Just clean it up a bit. Doing so makes everyone’s lives just a bit better, helps the codebase in a sustainable way, and assists the business by making its supporting infrastructure more flexible.
Perhaps I need to make it super explicit that you should understand whatever changes you are making, discuss them with team members, have them code reviewed, and start with small steps (like documentation). I thought that was implied with the above, but maybe I wasn't clear enough.
> we were left with his crappy code
Seems like a horrible situation. I have so many questions!
If it was a true refactor (and not new functionality) why did you have to use the crappy code? Or did he do a rewrite in the process of adding new functionality? Who was reviewing the code? How did it get merged to the mainline?
It is a common refrain against simple mottos or ideologies in programming for people to pick the worst memory they have of a coder or experience as an example of why it cannot work.
As if the problem with a bad team member is that they heard the wrong motto, if only they hadn't started incorrectly following the boy scout rule their contributions would be exemplary.
I don't think you would find many people who would consider his actions a reasonable interpretation of it. I think this person would cause damage regardless of their motto or underlying intent.
Taking a leaf from the guidelines for person-to-person,discussion on this forum about arguing against the strongest version of your opponents argument: I think there is great value to lots of small improvements over a long period of time (no silver bullet etc). Certainly it should be tempered with the knowledge that the change is indeed an improvement. That can usually only be decided by the person who originally wrote it OR a consesus of the team involved.
I've worked on codebases where the whole team agrees a certain part of the codebase needs to be refactored, but we all also agree it is way too dangerous. Which is better? Working but hard to maintain spaghetti code, or clean, new, and broken code?
I sweeten the pot with management by pointing out that if you do this, it’s easier to ramp up new people. If you can ramp up new people, you can absorb new customers without making a fool of yourself in the process. We should want to be able to take on big new customers, right?
For your second point, I don't really see the applicability of this specific concept of the boy scout rule to the situation of "business critical code that is in such a bad state the whole team agrees it should be rewritten".
However, were I to try to apply the concept to your situation, the first question to ask is "why are people afraid to change the code?". There's usually 2 reasons for this. Firstly, the code and the problem are super complex and hard to understand, this just takes a lot of time, to develop the knowledge and intuition around the code. Secondly, the code lacks tests (unit / intergration / functional / manual spreadsheet of test cases) that assert the behaviour you expect. Therefore my (unasked for and probably already known to you) suggestions for "leaving it cleaner than you found it" would be to make a start on understanding the code better through a series of very very very small refactorings. Pick one tiny function that is a mess and clean it up. At the same time, ensure your changes are correct by asserting all expected behaviour of that function.
If you are now saying "duh of course" well thats the point. I do not think a single reasonable person would suggest that a full rewrite of a complex business critical system that is either poorly understood, poorly tested or both is a charitable interpretation of what the article is talking about.
At the end of the day it is super easy to shoot down any idea or concept in programming, because we have so much power to abuse and ruin anything day to day if we're not careful. It's the easiest thing in the world to take a machete to a codebase without thought. But the problem there is the person holding the machete, they can't be trusted with it. I cannot think of a single thing that wouldn't be misinterpreted or done badly by some of the terrible coders I've met during my career.
> You don’t need to transform it. But you should make it better. Just clean it up a bit.
> A warning about refactoring. Don’t refactor what you don’t understand. Don’t drive by refactor. Discuss your plan with someone more familiar with the code.
if your organization has lost its grip on the code base, no one understands it, and every time you try to touch it something odd breaks and you have to spend days tracking it down - you have a problem.
if you can talk to someone about what the code does, and there is sufficient testing in place so make it safe to change - you don't have a problem.
refactoring is the process of (re)taking ownership of the codebase. the result is in your head. you don't even need to get the result committed for it to have been useful.
Most of the production code I've touched I would never dare to do something like this until I had a very good understanding of the codebase as a whole.
Obviously any but the most trivial refactor requires some care. Is it worth it, when (in the context of OP) you're in there to do something else? Sometimes yes, sometimes no. One rule of thumb is whether you're just rearranging code or changing the fundamental way that it works. Refactors of the first type can generally be kept low-risk and are probably worthwhile. Rewrites of the second type involve more risk and are probably better left to be projects unto themselves.
I’ve resigned myself to the idea that there’s two diametrically opposed way to run a software project.
One is by rote memorization. The people on the team have been there a long time. Seniority is hard to achieve because everyone makes dumb mistakes for a few years, instead of six months, due to the code being rife with tribal knowledge. This group gets very, very cranky when you refactor, because you are moving things out of place. Things they spend a great deal of effort memorizing. You’re threatening their seniority, their job security, and their control.
Another way is to design for people getting in and out quickly. The knowledge is curated, the code tells a story you can follow. Important assumptions are encoded plainly, perhaps documented there or in a wiki. This team can size up or down more quickly without bloodshed. Which is great if there are other projects that need attention, not so good if the company is a one trick pony with money flow problems. Counterintuitively, your best people may choose to stay a little longer here because they don’t feel trapped.
Early in my career I had a lot of luck changing engineering culture for the better, and I mistakenly assumed these techniques would work anywhere. They work lots of places, but they don’t work on a team that is collectively smarter than you are. Like doctors, clever but misguided developers can make the worst patients. There was one team in particular in a vertical I really wanted to be in, where I should have quit four months in, when it was obvious we would never see eye to eye. How can you hate your own code but refuse to change how you write it? Sometimes the smartest developers are the dumbest people.
Good severance package when the layoffs happened, but I suspect to this day the manager thinks I was part of their decline, rather than the harbinger.
In a way we are both right. My energies would have been better spent elsewhere. I’ve thought a lot about how to detect this sort of dynamic at new prospects but the best I’ve come up with is maybe don’t take a job where you’re the first new hire in n years and everyone else has been there forever. It might be okay, but it might be hell on wheels.
It's probably that way for a reason.
Every time I tried to clean up code, I ran into the following:
Some arcane bug was exposed
The previous developers couldn't understand it
Something else broke
So unless you have a way to 100% test your change, keep the status quo.I find this assumption to be harmful. There’s really no probably about it; it’s just the way it is, might have been intentional, might have been completely unwitting. The solution should be to refactor if you have to but in a way that doesn’t change the underlying functionality.
If you think about it, there is an extreme case as a contractor where the code you need to change is so convoluted and impossible to understand (imagine a neural net without the training set) that you just can’t change it without refactoring. So I would argue refactoring is necessary sometimes.
Why were they using a quantity field for more than just numbers? Why was it only being used in some corner of the code in a totally different module?