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 • • Edited

Tests first, function body later lol, that's me now

Weird edge case test though, that thing remembers the bug I was scared of back then (even when I've forgotten it completely lol). No test there? I write small characterization one first, real input, freeze behavior today before touching anything

Invariant goes in name, steps don't, future me needs never-break part only

Test names tell you more than code sometimes, no?

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 • • Edited

Double-charge name, nice, that saves you later lol

Step narrating names is the sneaky one though... opened my code after 6 months, test 'testProcessPayment' told me nothing, double-charge name remembered retry paranoid code, I had forgotten it and I sat there like oh. ok. read name, know why retry looks paranoid without opening it, so yea rename vague ones when you touch file or only add characterization test alongside? :)

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 • • Edited

Rename first Abdullah, forces the question early lol

Can't write new name in one try? I probably don't understand code enough to pin it with a test yet. Retry handler taught me that (testProcessPayment was asserting duplicate allowed, not blocked lol), rename made it obvious before I touched logic

First name wrong once test is written for you too, rename 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
 
sneha_kumari profile image
Sneha Kumari •

This is so relatable! I find VS Code features like Go to Definition, Find All References, Peek Definition, and Call Hierarchy really helpful. I also use GitHub Copilot Chat to understand what a function does and how it's connected to other files. Makes navigating large projects so much easier!

Collapse
 
userxnom profile image
Abdullah Naseer •

That is a strong toolkit, Sneha — Find All References and Call Hierarchy are the two that answer the question I keep coming back to in this post, who actually calls this and what does it connect to, rather than just what the function says. Copilot Chat as the plain-English layer on top of that makes sense too. When Copilot Chat's explanation and what Find All References shows disagree, which one do you trust first — and what do you check to settle it?

Collapse
 
sneha_kumari profile image
Sneha Kumari •

Honestly, I'd go with the actual code first haha. Copilot is great for quick explanations, but sometimes it can miss a bit of context. If something doesn't add up, I'd check where the function is defined, where it's being used, and what happens to its output. AI is super helpful, but I guess we shouldn't take everything it says at face value. 😅

Thread Thread
 
userxnom profile image
Abdullah Naseer •

That order is exactly right — code first, Copilot as the second opinion. The check you describe — where the function is defined, where it is used, what happens to its output — is a tidy little loop, and it catches the exact thing AI explanations miss: the context around the function. Has there been a time when Copilot's explanation sounded confident but the code told a different story — and what gave it away for you?

Thread Thread
 
sneha_kumari profile image
Sneha Kumari •

Yeah, I noticed something similar while working on MedMatch. I was trying to understand the clinical trial search flow, and Copilot explained the functions pretty confidently. But when I started tracing the code across the React page, API service, and backend, I realised its explanation didn't cover the complete flow. What gave it away was that I had to check how the data actually moved between these files. It taught me not to rely only on AI explanations, but to verify things in the code too. 😅

Thread Thread
 
userxnom profile image
Abdullah Naseer •

That MedMatch example is exactly why the "trust but verify" line matters — Copilot sounded confident about the clinical trial search flow, but the real story only showed up when you traced how the data actually moved between the React page, the API service, and the backend. The giveaway you named is the one I would steal: when the explanation does not cover the complete flow, the code is telling you something the summary is not. When you trace a flow like that now, which layer do you check first — the page, the service, or the backend?

Collapse
 
visionwei profile image
Wei Bin •

Mine was a site generator I hadn't touched in about three months — I opened a function full of language-pair conditions and had no idea which ones were still live. First thing I do now is the same as yours, read the callers, but I add one step before that: git blame on the weirdest line, because the commit message usually tells me what problem I was solving that day. The habit that saves me is the plain-English comment, written the moment I finally understand it — if I put it off, I lose it again by the next visit.

Collapse
 
userxnom profile image
Abdullah Naseer •

That git blame step before the callers is a genuinely good addition, Wei Bin — I had it backwards. The commit message tells you what problem you were solving that day, which is exactly the context the code itself never keeps. And a function full of language-pair conditions is the perfect case for it, because no amount of reading the branches tells you which pairs are still live. Your rule about writing the plain-English comment the moment you finally understand it is the part I keep underlining — put it off and you lose it again by the next visit, which is exactly what happened to me more than once. When the blame trail runs dry, though — a squashed commit or a vague "fix stuff" message — what do you reach for next?

Collapse
 
visionwei profile image
Wei Bin •

Good question — when the blame trail is dead I stop reading history and start interrogating the present. First the tests, if any survived: they're the only other place intent got written down. If there are none, I go black-box: feed the function real inputs and watch what it actually does, especially the branches I suspect are dead — for that language-pair function, checking which pairs showed up in real usage told me more in an hour than a week of reading branches. And before I change a single line I write down whatever the probing taught me as a characterization test, so the next stranger inherits evidence, not folklore. The squashed 'fix stuff' commits still annoy me, though — some days the honest answer is the trail is just gone and probing is all that's left.

Thread Thread
 
userxnom profile image
Abdullah Naseer •

That is a really strong addition, Wei Bin — when the blame trail is dead, the tests are the only other place intent got written down, and black-box probing with real inputs tells you more in an hour than a week of reading branches. Writing down what the probing taught you as a characterization test before changing anything is the part I will steal. When you checked which language pairs actually showed up in real usage, did any pair surprise you by being completely absent?

Thread Thread
 
visionwei profile image
Wei Bin •

Thanks, Abdullah, and glad the characterization test idea is useful. Yes, one surprise: a pair I had built the whole matrix around barely appeared in real traffic, while a pair I had treated as an afterthought showed up constantly. That mismatch is what made me stop trusting my own usage guesses. Which pair would you have expected to dominate?

Thread Thread
 
userxnom profile image
Abdullah Naseer •

That mismatch is the whole lesson, Wei Bin — the pair you built the whole matrix around barely appearing, while the afterthought pair showed up constantly. Honestly I would have guessed the same way you did, because the pair you design around feels like the one that must matter most. That is exactly why trusting real traffic over my own usage guesses is the right takeaway, and writing the probe down as a characterization test before changing anything is the part I will keep. Which pair was the afterthought that kept showing up, and did you rebuild the matrix around it once you saw the real numbers?

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
 
kreout profile image
Andrei Ovcharenko •

The issue often gives me the symptom, while the test gives me the exact boundary: this input failed, and this nearby one must still work. If the issue just says “fixed an edge case”, I need both. I would rather leave a comment saying which case the condition protects than try to retell the whole ticket.

Thread Thread
 
userxnom profile image
Abdullah Naseer •

That pairing is a neat one, Andrei — the issue for the symptom, the test for the exact boundary, and the comment naming the case the condition protects. A ticket that just says "fixed an edge case" is the one that comes back to bite you months later. When you leave that comment, do you put it on the condition itself, or on the test that pins the boundary?

Thread Thread
 
kreout profile image
Andrei Ovcharenko •

I'd put the short warning by the condition, where the tempting simplification happens, and let the test spell out the inputs and expected result. A link from that comment to the test is enough; I wouldn't want two copies of the whole explanation to keep in sync.

Thread Thread
 
userxnom profile image
Abdullah Naseer •

That's a clean split, Andrei — the short warning right where the tempting simplification lives, and the test carrying the exact inputs and expected result so there's only one place to keep in sync. I like the link-from-the-comment idea too; it keeps the code readable without duplicating the whole explanation. Do you usually name the test after the condition or the symptom, so future you finds it from the warning?

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
 
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
 
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 •

My first step is usually git log on that file, not the function itself. If I can find the commit message or the PR that introduced it, I get the reason it exists, which is the part I have actually forgotten. After that I look at the callers, and I run it with a real input before I touch anything. The habit that saves me most often is a short comment that says why the odd-looking branch is there, not what it does. What it does I can read; why it is there is what disappears after a few months.

Collapse
 
userxnom profile image
Abdullah Naseer •

Starting with git log on the file is a genuinely good addition, fizan — it flips the order in the post, and for the right reason. The commit message or the PR that introduced it gives you the reason the code exists, which is the part you have actually forgotten, and only then do the callers and a real input fill in the rest. Your order is history first, then callers, then run — mine was callers first — but we end up in the same place: the reason, not just the behavior.

And that why-not-what comment is the habit worth stealing from your answer. What the branch does you can read; why it is there is what disappears after a few months, and one short sentence while you still know it is the cheapest insurance there is.

When git log gives you nothing — a squashed commit, no PR, a message that just says "fix" — what is your fallback for the why? Do you go straight to the callers and the run, or is there another place you look first?

Collapse
 
monroay566 profile image
Monroay •

This happens to me most with one-off scripts I wrote in a hurry and then did not touch for a few months. My first move is the same as yours — find a caller and run it with one real input before I read the function line by line. The one habit that has saved me most is writing a short comment about why the weird branch exists, not what it does. The what is usually obvious once I see it run; the why is the part I forget.

Collapse
 
userxnom profile image
Abdullah Naseer •

One-off scripts are the worst case for this, Monroay — written in a hurry, touched once, then left for a few months until they read like someone else's code. Your first move is exactly the one from the post: find a caller and run it with one real input before reading the function line by line. Watching it run tells you what it is supposed to do faster than any amount of staring at the lines.

And the why-not-what comment is the habit that keeps paying off. The what is usually obvious once you see it run, but the why — the reason that weird branch exists at all — is the part that disappears in a few months. That is the one sentence worth writing while you still understand it.

When you write that why-comment now, do you write it while you are still writing the script, or only after the first time you come back to it confused?

Collapse
 
rider_lee profile image
Rider Lee • • Edited

@userxnom , I’d start with the caller too. The line that makes me curious is the one that looks safe to delete. That’s when I check the PR that added it and ask, “What would break if I took this out?”

If it turns out a vendor sometimes sends the CSV header twice, I’d add a test with that exact input and check that only the data rows get imported. Six months later, that test will tell me more than a commit message that says “fix CSV issue.”

Collapse
 
userxnom profile image
Abdullah Naseer •

"What would break if I took this out?" is exactly the right question, Rider Lee — it turns a line that looks safe to delete into a small experiment instead of a guess. And checking the PR that added it, not just the commit message, is the sharper move: the PR usually still has the discussion about why the vendor's doubled CSV header mattered. Your test idea is the part I would steal outright — a test fed that exact duplicated-header input is worth more six months later than any "fix CSV issue" message, because it proves the line earns its place instead of just claiming it. Once that test is green, do you delete the suspicious line and let the test guard the behavior, or do you keep the line and let the test explain it?

Collapse
 
rider_lee profile image
Rider Lee •

I’d keep the test either way, @userxnom . Once it’s green, I’d take the line out locally and run it again. If the second header shows up as a data row, I’d put the line back. That tells me exactly why it’s there.

If the test stays green, I’d check whether the parser already skips repeated headers. If it does, I’d delete the extra line and keep the test. That way, whoever changes the parser next knows what the importer is expected to do.

Thread Thread
 
userxnom profile image
Abdullah Naseer •

That is a really clean experiment, Rider Lee — keep the test, remove the line locally, and let the second header showing up as a data row settle it. I like that the test stays either way, so the next person changing the parser knows exactly what the importer is expected to do. Have you ever run that remove-and-rerun check and found the parser already skipped repeated headers, so the line really was dead?

Collapse
 
jaypateldev profile image
Jay Patel •

This is something almost every developer can relate to! Code that makes perfect sense today can become confusing after a few months. Good naming, clear documentation, and small functions make a huge difference when revisiting old projects.

Collapse
 
userxnom profile image
Abdullah Naseer •

You have named the three habits that save the most time when you come back to old code — good naming, clear documentation, and small functions. I have felt the same thing: code I wrote six months ago reads like someone else wrote it unless those three are in place. Of the three, which one has saved you most often when you revisit a project — and is there a project where skipping one of them cost you real time?

Collapse
 
ascendra_ventures_92c21f2 profile image
Ascendra Ventures •

Reading the callers before the function is the step I would put first too, because the callers record what the code was promised to do, while the function only records what it does now. Your note about the rewrite losing the edge cases the old code quietly handled is the expensive version of the same lesson: the reasons were never written down, so the second attempt could not know they existed.

Ascendra Ventures ran into this at a larger scale while building memory for AI coding tools. An audit of roughly 7,000 prompts over three months showed the same context being retyped and reasoned decisions vanishing. The system that came out of it keeps handwritten text files under git as the source of truth, and treats the knowledge graph built on top as a disposable cache that can be rebuilt from those files. The habit that mattered most was the one you describe: write down why, in plain words, while the reason is still in your head, because nothing generated later can recover it.

When you add that one plain sentence, do you put it in the code as a comment or in the commit message, where git blame will show it next to the change?

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?