Chapter 21. First, After, Later, Never

Let’s talk about the timing of tidying with respect to a behavior change in the system. Tidy first, then change the behavior? Change the behavior, then tidy? Or simply note messiness (in the sense that future behavior changes are going to be harder than they need to be), then come back later to tidy? Or, don’t tidy at all?

Later

Some folks think tidying later is pure fantasy, a unicorn, an honest politician. The mythological status of tidying later is used as justification for tidying too much now, whether that is before or after. I’m here to tell you that you really can tidy later. You may not like the prerequisite, though.

Is there enough time to do your work? I’m not asking whether you have plenty of time, because of course not. I’m not asking whether there’s more to do than you have time for, because of course there is. Ask yourself, “How would we work if we had enough time?” If the answer is wildly different from what you are actually doing, then no, there is not enough time to do your work.

But I invite you to examine this assumption that there isn’t enough time to do your work. I’ve worked with large, successful, long-lived, highly profitable businesses that still believed, in the face of all the evidence of being large, successful, long-lived, and highly profitable, that there just wasn’t and wouldn’t ever be enough time to do the work. Seems bizarre, like a bird questioning the laws of physics and suddenly falling out of the sky.

What would you do if you temporarily, provisionally believed that there was enough time to do your work? You might make a list of messes to tidy later (I call this my Fun List, because I have an odd notion of “fun”). Then later, rather than jumping feverishly to the next feature to implement, you might glance at your Fun List and think, “I have an hour. I don’t want to start something big. Why don’t I take a crack at item 4?” And then you might.

That’s tidying later. It can happen. Try it. Then it will happen.

Tidying makes future changes to the behavior of the system easier (through mechanisms we will explore in the next part of the book). If there is an area of the system that’s guaranteed to change (strong word, “guaranteed”), then tidying in that general area creates value if it simplifies those future changes.

Tidying later (that is, not tied to an immediate behavior change) creates value in a couple of other ways. One is by reducing the tax of messiness. You are migrating from an old API to a new API. You’ve changed the call sites that are immediately affected, but you have one hundred more call sites to migrate later. When you’ve migrated them all, you can remove the old API. However, until then, you have to mirror changes made to the new API in the old API.

Tidying all those call sites isn’t pointless mucking about. Once you migrate all of the changes, a certain class of changes becomes cheaper. There may not be a pressing need to reduce that cost, but taking the pebble out of your shoe lets you walk better.

Another reason to tidy later is as a learning tool. The code “knows” how it wants to be structured. If you’re listening and you move the code from its current structure toward its desired structure, you’re bound to learn something. Tidying is a great way to become aware of the detailed consequences of your design. Tidying illuminates the design as it could be.

Finally, tidying later just feels good. Software development is a human process. We are humans with human needs. Sometimes I just don’t have the energy to tackle a new feature, but I want to work. Picking an item off the Fun List and tidying it brings me joy. Don’t underestimate how much better you are as a programmer when you’re happy.

Summary

Tidy never when:

  • You’re never changing this code again.

  • There’s nothing to learn by improving the design.

Tidy later when:

  • You have a big batch of tidying to do without immediate payoff.

  • There’s eventual payoff for completing the tidying.

  • You can tidy in little batches.

Tidy after when:

  • Waiting until next time to tidy first will be more expensive.

  • You won’t feel a sense of completion if you don’t tidy after.

Tidy first when:

  • It will pay off immediately, either in improved comprehension or in cheaper behavior changes.

  • You know what to tidy and how.