The best thing I wrote last month was the code I deleted Bex Calder

programmingcraftdeleting

The best thing I wrote last month was the code I deleted

Someone wrote in to ask how you know when to throw code away, and the short answer is that you learn to enjoy it.

by Bex Calder 3 minute read

Someone wrote in with a good question, the kind I like because it has no clean answer. How do you know, they asked, when a piece of code should be deleted rather than fixed? They had a function that everyone on their team was afraid of, and they wanted a rule.

I do not have a rule. But I have a feeling I have learned to trust, and I can try to describe it.

The first sign is fear. Not yours, everybody's. When a piece of code has reached the point where no one will change it without a good reason and a deep breath, that fear is information. It usually means the code is holding more in its head than any one person can, and code that has to be understood all at once, or not at all, is code that has stopped helping you. Fear is the smell of something that wants to be smaller.

The second sign is the comment that apologises. You know the one. This is horrible but it works. Do not touch unless you understand X, Y and Z. Every apologetic comment is a small confession that the code and the idea behind it have drifted apart, and that someone patched over the gap with a sentence instead of a rewrite. I read those comments as invitations.

Deleting is not giving up on the code. It is giving up on defending it.

Here is the part people find hard, and it is the actual answer to the question. You cannot know whether code should be deleted until you have understood it well enough to rewrite it, and once you have understood it that well, you almost always can rewrite it smaller. The understanding is the expensive part. The deletion is cheap, and it feels, wrongly, like the risky part, so people avoid doing the cheap safe thing because it looks like the dangerous one.

So my honest method is this. When a piece of code frightens the team, I do not try to fix it in place. I sit down and force myself to explain, in plain words, what it is actually supposed to do, stripped of every clever thing it currently does to achieve that. Nine times out of ten the plain words are much shorter than the code. That gap, between what it does and what it needs to do, is the code you get to delete. Then I write the small version next to the old one, prove they agree, and remove the original without ceremony.

The person who inherits your code, and it is usually you in six months, does not want to admire your cleverness. They want to be able to change one thing without discovering they have changed four. Every line you delete is a line that can never break, never confuse, never need a comment that apologises for it. I have never once regretted making a working program smaller. I have regretted, many times, leaving something in because I was proud of it.

So to the person who wrote in: you do not need to be sure the frightening function should die before you start. You need to understand it well enough to say what it is for in one sentence. If that sentence is much shorter than the function, you have your answer, and it will be one of the more satisfying afternoons you spend all month. Ask me for a favourite language another time. This is the favourite thing.

More from the journal

All of the journal Every piece, by month

On the same subject

Discussion

House rules

  • Sign how you like, but be one person. You may use the name on your account, a name of your own choosing, or none at all. What you may not do is wear somebody else's: no pen name of ours, no member's name but your own, and nothing that reads as the studio or its staff.
  • Argue with the point, not the person. Robust disagreement is welcome; contempt is not.
  • Nothing unlawful, hateful or harassing, nothing that identifies a private individual, and no advertising.
  • Post only what is yours to post. That includes other people's writing and private correspondence.
  • We may hide a remark, remove it, or close an account to the discussions, and we may do so without notice.

What a chosen name is and is not

A chosen name hides you from the room, not from us. Every remark stays linked to the account that wrote it, and the studio can see that link whenever it needs to. Please treat it as a way to speak freely, not as a way to say something you would not put your name to.

Where responsibility sits

Remarks in this thread are written by members and published without prior review. They are the views of the people who wrote them and not those of eQuill Studio. We do not verify their accuracy, and we accept no liability for them or for any decision taken in reliance on them.

If something here breaks these rules or infringes your rights, report it and we will look at it promptly.

This thread belongs to this piece alone. Members choose how they are signed, and can change it from the box below. When the studio or the writer joins in, the page says so with a badge, so you always know who is talking.

OPENING THE THREAD…