DEV Community

What do you do when you open your own old code and can't tell what it does?

Abdullah Naseer on December 02, 2026

You open a file you wrote yourself, you stare at a function you are sure you wrote, and for a few seconds you have no idea what it does — and you a...
Collapse
 
smtahosin profile image
S M Tahosin •

I go for the tests before the function body now. If there's a test that pins the weird edge case, that test usually remembers the bug I was scared of when I wrote the code, even when I've forgotten it completely. If there isn't one, I write a small characterization test with a real input first, just to freeze current behavior before I touch anything. The habit that saved me most is naming the invariant in that test, not the steps, so future me knows what must never break. Do you ever find the test names tell you more than the code does?

Collapse
 
ameenusman profile image
Ameen Usman •

@smtahosin Yes — good test names often outlive my memory of the code.

Especially when the name states the invariant (“rejects expired token”, “does not double-charge on retry”) instead of narrating steps. That’s exactly the breadcrumb I wish past-me always left. Characterization test first with a real input is how I unstick myself too: freeze behavior, then rename/split with less fear.

Collapse
 
smtahosin profile image
S M Tahosin •

The double-charge one is exactly the kind of name that saves you later. I read that and I already know why the retry code looks paranoid, without opening it. The step narrating names, stuff like testProcessPayment, tell me nothing a month later. Do you rename the vague ones when you touch the file, or only add the new characterization test alongside them?

Thread Thread
 
userxnom profile image
Abdullah Naseer •

I do both, but in a specific order — I rename the vague one while I am in the file, because that is when I actually understand what it does, and the rename is the comment that never goes stale. Turning testProcessPayment into something like rejectsDoubleChargeOnRetry tells the next reader the invariant before they even open the body. Then the characterization test goes alongside it, so the rename has a guard behind it. The rename alone feels risky without the test, and the test alone leaves the next reader guessing. Which one do you reach for first when you open a file like that — the rename or the test?

Thread Thread
 
smtahosin profile image
S M Tahosin •

Appreciate that. Rename first for me too, mostly because it forces the question early. If I can't write the new name in one try, I probably don't understand the code well enough to pin it down with a test yet. Learned that on a retry handler where testProcessPayment turned out to be asserting the duplicate was allowed, not blocked, and the rename made that obvious before I touched any logic. Do you ever find the first name you pick is wrong once the test is written, so you end up renaming it again?

Thread Thread
 
userxnom profile image
Abdullah Naseer •

That retry-handler story is a perfect example, Tahosin — testProcessPayment asserting the duplicate was allowed, not blocked, is exactly the kind of backwards test a good name exposes before you touch any logic. And yes, the first name is wrong for me too, often: the name I pick before the test describes what I hope the code does, and the test forces me to describe what it actually does, so a second rename is normal, not a failure. When that second rename happens for you, do you treat it as a sign the test found something real, or just as tidying up the name?

Collapse
 
ameenusman profile image
Ameen Usman •

This hits often—usually 3–6 months later on something I shipped in a hurry.

First thing I do: find a caller and one real input, then run it. Reading the function top-to-bottom alone still leaves me guessing. Git blame helps only after I know what “broken” looks like.

Small habit that saves me next time: rename one unclear name and add one plain-English comment while the context is still loaded—exactly your step 3. If I leave without that, I’m teaching Future Me the same puzzle again.

Collapse
 
userxnom profile image
Abdullah Naseer •

That 3–6 month window is so real, Ameen — long enough to forget the context, recent enough to be sure it was you who wrote it. I love the git blame point: blame tells you when, but it can't tell you what "broken" looks like until you've already got a failing input in hand. And "teaching Future Me the same puzzle again" is the perfect way to put it — that rename-and-comment habit is basically leaving a note for the person you'll be in six months. When you do run it with that one real input, do you usually find the answer in the output itself, or does watching it run just remind you what the function was supposed to do in the first place?

Collapse
 
ameenusman profile image
Ameen Usman •

Mostly the second — watching it run reminds me what it was supposed to do.

The real input reloads the shape in my head (happy path, weird branch, what “done” looked like). Sometimes the output itself is the clue — especially when it’s wrong or emptier than I expected — but usually the run is how I remember the contract, and only then does the output make sense.

Thread Thread
 
userxnom profile image
Abdullah Naseer •

That order is exactly right, Ameen — the run reloads the contract first, and only then does the output make sense. I like the detail about a wrong or emptier-than-expected output being the clue: it tells you the shape you remembered was the shape you wanted, not the shape the code actually has. When the run and the output disagree with your memory like that, do you trust the run and rewrite your mental model from it, or do you dig into the code to find which one is lying?

Collapse
 
carsonak profile image
Andrew Kihara • • Edited

For me, the process is the same as diving into any new codebase. The single most helpful thing is grasping the author's intent at the time of writing, and that author can be me sometimes. To do that, I usually check these in order: Documentation and inline comments; for high-level context and the "why", Naming; not just of the function and its parameters, but also the variables at the call sites, Custom types used in the function signature to see the expected data shapes and finally Tests; to see the intended inputs, outputs, and edge cases.

Usually, that’s enough to understand how a function or script is meant to be used. I only dive into reading the actual implementation line-by-line when I need to make edits and at that point, I'm mostly checking whether the code's actual behaviour still aligns with the author's original intent.

Collapse
 
userxnom profile image
Abdullah Naseer •

That ordered checklist is a great way to put it, Andrew — and I like that you start with the author's intent, even when the author is past-you. The call-site variable names are the one I would have missed: they show what the caller thought it was getting, which is often more honest than the function name itself. Of the four checks, which one has saved you most often when the others came up empty — and when you do have to dive into the body to make an edit, what is the first thing you look for there?

Collapse
 
carsonak profile image
Andrew Kihara •

I get that "did I write this?" experience mostly with "one-off" scripts or code that I find myself revisiting once every month or two. It's usually a vague memory of a code snippet, function or script that I used to solve a particular problem, never a concrete name or location. I have experienced this a couple of times to realise using good names and even short comments will save future me the hustle of understanding the full context in order to derive what I need.

Thread Thread
 
userxnom profile image
Abdullah Naseer •

That monthly-or-two revisit is exactly where one-offs bite hardest, Andrew — small enough to feel disposable when you write them, and then they quietly become load-bearing. I like that your fix is the same one that keeps coming up in this thread: good names plus a short comment, so future you does not have to rebuild the whole context from a vague memory of a snippet. Do you have any naming habit for those one-offs now — like a prefix, or a one-line note at the top saying what problem it solved — or do you still trust yourself to remember?

Collapse
 
fizanahmed profile image
fizan ahmed •

This happens to me all the time, usually about three to six months after I wrote something in a hurry. My first move is the same as yours: find the callers and run it with a real input before I touch anything. The one habit that has saved me most is writing the comment in plain words while the reason is still fresh in my head, even if the code itself looks obvious at the time. Future me is always grateful for that one sentence.

Collapse
 
userxnom profile image
Abdullah Naseer •

Three to six months is exactly the window where it bites, fizan — long enough to forget the hurry you were in, recent enough that the code still feels like it should be yours. I like that your first move matches mine: find the callers, run it with a real input, and only then touch anything. And that one plain-words sentence while the reason is still fresh is the cheapest insurance there is; future you really is the one who cashes it in. Do you have a rule for what earns that sentence — the weird condition, the magic number — or do you write it whenever something feels even slightly non-obvious?

Collapse
 
ameenusman profile image
Ameen Usman •

@fizanahmed Same timeline for me — three to six months is when “obvious” code becomes archaeology.

Callers + a real input first is the right order. And that one plain-English sentence while the reason is fresh is gold. I used to skip it because the code “looked clear”; future me always disagreed.

Collapse
 
kreout profile image
Andrei Ovcharenko •

I look for the commit that introduced the odd condition, then the issue or test attached to it. The function can tell me what it does; the commit sometimes tells me which tempting simplification would bring the bug back. That is the bit I want in the comment when I finally understand it again.

Collapse
 
userxnom profile image
Abdullah Naseer •

That is a sharper move than just reading the code, Andrei — the function tells you what it does, but the commit tells you why the odd condition is there at all. Tracing it back to the issue or test behind it is the difference between understanding the code and understanding the decision, and that tempting simplification you mention is exactly the trap: the code looks cleaner without it, right up until the bug comes back. Writing that bit into the comment once you understand it again is the part I want to steal. When you find the commit, does the issue usually make the reason obvious, or do you often have to piece it together from the test as well?

Collapse
 
bacist_dev profile image
Roman Voinitchi •

I think I can always tell what my old code does. But the main question here is the quality of that code — it can be terrible. So I get a little embarrassed and quickly close the old project.

Collapse
 
userxnom profile image
Abdullah Naseer •

That distinction is a good one, Roman — knowing what the code does and being proud of how it does it are two very different things. And honestly, closing the project quickly is such a relatable move. I have done the same when the code worked but the structure made me wince a little. Do you ever go back and tidy up one of those embarrassing spots once the sting fades, or do you prefer to leave it as a snapshot of how you coded back then?