When my daughters were young, we had a Nintendo GameCube. It turns out that one side effect of having a dad who makes video games for a living is that your house is fully equipped with every single video game console. My children did not realize until much later that not everyone’s house was like this.
Our favorite game was Animal Crossing , a game in which the three of us shared a small village filled with anthropomorphized animals. You could do all kinds of things in the village—dig for fossils, design new clothes, decorate your house, gather seashells, go fishing, make friends with the animals who lived in the town, or just kick back and listen to KK Slider play his guitar.
One of the things you sort of needed to do in Animal Crossing was pull weeds. Every night, a few weeds would pop up in your village, whether you’d played the game that day or not. Pulling weeds was easy—run over to the weed, push a button, and pop! The weed’s out of the ground. But you needed to keep up. The weeds kept growing whether you pulled them or not. The weeds even grew on days you didn’t play the game! If you stopped pulling weeds, the weeds took over.
They’re still making Animal Crossing games 20 years later. Tens of millions of people have played some iteration of the game, and every single one has had the same experience—you come back to the tidy little village you’ve worked so hard on after a few weeks away, and it’s completely overgrown with weeds. Twenty years later, I can still feel the pain.
Your project is like that village. You’ve got to pull the weeds—the little bits of annoyance that continually sprout in any codebase. Every day, whether you’re working on the project or not, whether you’re pulling weeds or not, more weeds are growing. If you don’t pull the weeds, they’ll choke the project.
So what are the weeds in this metaphor? They’re small problems that are easy to fix, but also easy to ignore. Think of the weeds in Animal Crossing —pulling them is as easy as pushing a button. Pulling a weed doesn’t have side effects. It doesn’t cause problems elsewhere. All that changes is that you have one less weed.
Here’s a weedy bit of code:
// @brief Remove duplicate integers from a vector
//
// @param values Integer vector to compress
template <class T>
void compressVector(
vector<T> & values,
bool (* is_equal)(const T &, const T &))
{
if (values.size() == 0)
return;
int iDst = 1;
for (int iSrc = 1, c = values.size(); iSrc < c; ++iSrc) {
// Check for unqiue values
if (!is_equal(values[iDst - 1], values[iSrc]))
{
values[iDst++] = values[iSrc];
}
}
values.resize(iDst);
}
There are a couple of obvious problems with the comments in this code. The header comment doesn’t match the function, to begin with. It looks like this started as a function that compressed duplicate values from a vector of integers, and whoever adapted it into a template forgot to update the comment. On top of that, the comment itself is too vague—we’re not removing all duplicate values, we’re removing adjacent duplicates. Unless the array is sorted, those aren’t the same thing. To complete the trifecta, there’s a spelling mistake in the second comment. With those problems fixed:
// @brief Compress sequences of equal values in a vector
//
// For any sequence of values in the vector considered equal, keep only
// the first value in that sequence, removing the duplicates.
//
// @param values Vector to compress
// @param is_equal Comparison function to use
template <class T>
void compressVector(
vector<T> & values,
bool (* is_equal)(const T &, const T &))
{
if (values.size() == 0)
return;
int iDst = 1;
for (int iSrc = 1, c = values.size(); iSrc < c; ++iSrc) {
// Check for unique values
if (!is_equal(values[iDst - 1], values[iSrc]))
{
values[iDst++] = values[iSrc];
}
}
values.resize(iDst);
}
Fixing these problems is pulling weeds. It’s easy to do. Repairing the comments won’t cause problems elsewhere. And I’ve made the code better—fixing the ambiguity of the comment is likely to save somebody a bug at some point.
I could do more, though. There are some naming and formatting issues sprinkled in, too. The i and c variables aren’t following standard conventions—this project uses index and count , not one-letter conventions. The is_equal argument ought to be isEqual to match the project’s function naming style. The curly braces aren’t consistent, and this project’s conventions frown on packing multiple arguments into the for statement. The conventions also expect blank lines after comments, which the second comment doesn’t have.
All easy to fix:
// @brief Compress sequences of equal values in a vector
//
// For any sequence of values in the vector considered equal, keep only
// the first value in that sequence, removing the duplicates.
//
// @param values Vector to compress
// @param isEqual Comparison function to use
template <class T>
void compressVector(
vector<T> & values,
bool (* isEqual)(const T &, const T &))
{
int count = values.size();
if (count == 0)
return;
// Copy values that aren't equal to their predecessor
int destIndex = 1;
for (int sourceIndex = 1; sourceIndex < count; ++sourceIndex)
{
if (!isEqual(values[destIndex - 1], values[sourceIndex]))
{
values[destIndex++] = values[sourceIndex];
}
}
values.resize(destIndex);
}
This round of changes was also safe, though not as safe as the comment changes made in the first round. It’s possible to introduce a bug with these sorts of changes—mistyping a sourceIndex when you meant destIndex , say. Unlikely, but possible.
Weed Identification
It’s safety that defines whether an issue you spot is a weed or not. If you can fix it safely, then it’s a weed that should be pulled. Fixing a spelling problem in a comment is absolutely safe. For a more substantive mistake in a comment, like the ambiguity we cleaned up in our first round of changes, fixing the problem is also safe…as long as you’re actually right about what the function does!
You can fix naming issues safely, too. Doing a search + replace over a section of your source code will work, and the compiler will probably catch any mistakes you make, at least for compiled languages like C++.
In the second round of changes, I moved some variables around when I renamed them. That was safe-ish, but less safe than the other changes. Still a weed, probably, but getting less weedy.
There’s a spectrum here, obviously! None of the changes I’ve made so far are functional—the compiler will generate the same code, more or less. We’ve improved its readability and consistency without affecting its functionality.
You can imagine more substantive changes that still don’t affect the code’s functionality—like changing the name of a class member, which requires matching changes in many source files, or writing a new expected-usage comment for a class. As long as the functionality of the code isn’t affected, it’s a weed. Changes that shouldn’t change functionality as long as you get the details right, like moving or renaming a variable, are probably still weeds, but a little more caution is called for.
When you are intentionally changing functionality, it’s not a weed anymore: it’s a bug, and different rules apply. You pull weeds automatically; fixing bugs isn’t as automatic, because doing so often introduces new problems. By definition, fixing a weed doesn’t introduce new problems .
Pulling weeds is easy, and a weed-free codebase is much more pleasant to work in...so why do most projects have so many weeds?
How Code Gets Weedy
Well, it’s easy to pull a weed, but it’s just as easy to ignore it. We all have more tasks than time. And the cost of pulling the weed is immediate, though small, while its benefits are diffused and delayed. It’s tempting to avert your eyes.
Furthermore, what looks like a weed to one coder may look like a flower to another. 1 You may be confused by a comment and suspect that it’s incorrect, but not be confident enough in your understanding of the code to change it. You could take a more thorough look at the code or ask someone who’s more familiar with it to double-check your suspicion, but (see previous paragraph) you’ve got a long to-do list, and fixing this random comment is not on it.
Or you may see some formatting issues in a chunk of code written by a new member of the team. You could fix them yourself, but it’s easy to reason that fixing them yourself won’t teach the new team member the right formatting. Better to let their next code review identify the mistake.
Even though weeds are easy to pull, it’s almost as easy to leave them be. The factors that make you reluctant to pull them—needing to focus on important issues, being unsure of whether the weed is actually a weed or not—they’re all real.
But weeds breed more weeds. You may have a crisply defined set of naming and formatting conventions, but if your project is full of weedy bits of nonconforming code, then no one will know what to trust. Do they trust the conventions, or the code? I know what happens in this case: they shrug and copy whichever they’re more comfortable with.
The comment that confused you? It’s going to confuse the next person who sees it, too. And you’d be surprised and how often the process of fixing a comment—verifying that the function does what you think, checking that the code around it is making the correct assumptions, talking about the new comment in a code review—turns up a “real” bug in the code.
Look, pulling weeds is by definition quick. This isn’t something that needs to be scheduled. If something is going to take appreciable time, it isn’t a weed.
Our focus at Sucker Punch on weeds works because we all agree what a weed is. We have strong and strict team conventions, per Rule 12 . A lot of weed pulling fixes something that doesn’t match the conventions. That strengthens the conventions themselves—not least because the change will get reviewed, and in the review the two of you will look at the noncompliant and compliant versions, pre- and post-weed pull, and agree that it was a weed that needed pulling. If as a code reviewer you think the reviewee is wasting time on unimportant issues, the problem is that you don’t agree on what’s important. Fix that .
In the end, it’s simple. If you know something is a weed, pull it. If you suspect something is a weed, it’s worth a small bit of effort to verify that and pull the weed. 2
This places a counterintuitive imperative on your best, most senior team members. They’re the ones who will be best at spotting weeds. The person who wrote your project conventions is best positioned to spot deviations from those conventions, after all. They’re also likely to be one of the more senior people on the team. Does it make sense for them to spend a little bit of time fixing little problems ?
Absolutely! Clearing the project of weeds makes everyone’s job easier. It makes the important stuff more visible.
1 Per the gardener’s maxim, “A weed is just a plant in the wrong place.” Which reminds me of the time I decided to surprise my wife by weeding her vegetable garden for her. The surprise in that case was that I pulled out all of the asparagus she’d recently planted. If my goal was to never be asked to weed anything ever again…mission accomplished.
2 Just be sure it’s not asparagus.