DEV Community

Abdullah Naseer
Abdullah Naseer

Posted on

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

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 are the author.

I have published 42 posts on DEV now, and I still have that moment at least once a week. It is not a beginner problem. It is what happens when the person who wrote the code and the person reading it are the same person, just a few months apart.

Here is what I do now, before I change anything:

First, I read the callers, not the function. Who calls it, and what do they expect back? That usually tells me what the code was for faster than reading the code itself.

Second, I run it with a real input I remember, and I watch what comes out. One small print or log line, one input I understand, and the shape of the thing comes back to me before I touch a single line.

Third, I rename one thing and add one comment in plain words while I still understand it. Not a rewrite — just one honest name and one sentence about why it is there. I leave the code a little clearer than I found it, even if I change nothing else.

So now I am curious about your side of it:

  • When did you last open your own old code and not recognize it at all — what was the code supposed to do, and how long did it take you to work it out?
  • What is the first thing you do in that moment — do you read it top to bottom, run it, check the git history, or ask someone who was there?
  • What is one small habit that has actually saved you the next time you came back to old code — a comment, a name, a test, or something else?

What I tried that didn't work: rewriting the whole file from scratch, because I thought I would remember the clever parts better the second time — I didn't, and I lost the edge cases the old code had quietly handled. And adding comments that just repeated what the code said, instead of saying why it was there.

No wrong answers. I am curious how much of your old code still feels like yours when you open it again.

Top comments (85)

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?

Thread Thread
 
smtahosin profile image
S M Tahosin • • Edited

First rename mostly, nice, that is me too lol

Second rename is the sneaky one though... opened my code after 6 months, test 'testProcessPayment' remembered duplicate allowed not blocked, I had forgotten it and I sat there like oh. ok. verb flips allowed to blocked I check callers first, vague to plainer is tidying, so yea check caller first or rename and let test fail loudly? :)

Thread Thread
 
userxnom profile image
Abdullah Naseer •

That distinction is really useful, Tahosin — a rename that's just clearer wording is tidying, but a rename that flips the verb from allowed to blocked is really a behavior change wearing a rename's clothes. Checking the callers first in that second case feels right to me, because that's where the real meaning lives. When you do check the callers and find one that was quietly relying on the old behavior, what's your next move — do you fix the caller there and then, or leave it and flag it for a separate pass?

Thread Thread
 
smtahosin profile image
S M Tahosin • • Edited

In most cases I take care of it in one go, provided the caller is around and the fix is easy. Otherwise I may forget why the old behavior mattered, and then the change may appear safe whereas it is not. The separate pass seems to make sense only where the caller has a test change of its own, otherwise I just do it while the context is still in my head.

Thread Thread
 
userxnom profile image
Abdullah Naseer •

That rule is a good one, Tahosin — fix it in the same change while the context is still in your head, and only split it out when the caller needs its own test change. The part that rings true for me is forgetting why the old behavior mattered: a rename that looks harmless a week later is exactly how a quiet behavior change slips through. When you do split it out for that test change, how do you flag it so it doesn't get lost — a note in the test name, a comment, or a separate task?

Thread Thread
 
smtahosin profile image
S M Tahosin • • Edited

Name of test, for the most part. A separate task is where all the best intentions end up being buried, in my case at least. I actually include the purpose of the test in its name itself, something along the lines of keeps_old_rounding_until_invoice_migration, and if it is significant enough to need monitoring I make sure to include a reference to the corresponding issue in a comment next to it. The name of the test is the one place I am guaranteed to look again, because it shows up every time the suite runs.

Thread Thread
 
userxnom profile image
Abdullah Naseer •

That is a convincing answer, Tahosin — the test name is the one place you are guaranteed to look again, because it shows up every time the suite runs, and keeps_old_rounding_until_invoice_migration carries the reason in plain sight. Linking the issue from a comment next to it for the bigger ones is a nice touch too. When the migration finally lands, do you rename the test back to something plain, or leave the name as a bit of history?

Thread Thread
 
smtahosin profile image
S M Tahosin • • Edited

Rename it back to something dull, that is what I would do. After migration the old name is not a warning anymore, for the next reader it becomes a lie, since they will assume the old style of rounding is still in effect. History, if it is wanted, git blame is where it can be retrieved from. What the code does today is what a test's name should say, not what it used to wait on.

Thread Thread
 
userxnom profile image
Abdullah Naseer •

That line about the name stopping being a warning and starting being a lie is the sharpest way to put it — a name that once helped now actively misleads the next reader. And you are right about git blame carrying the history; the name does not need to be a museum. Do you treat all names that way, or is there a case where you would keep the old wording around a bit longer — say, while a migration is still rolling out?

Thread Thread
 
smtahosin profile image
S M Tahosin • • Edited

Yes, migration halfway through is one exception that applies to me. While more than half of the callers are still using the old name, I prefer not to rename too early and risk creating additional confusion, but once the rollout is officially done I rename immediately. That is the point where keeping it starts misleading people.

Thread Thread
 
userxnom profile image
Abdullah Naseer •

That is a clean rule — "swaps one confusion for another" is exactly the trade-off, and waiting until the rollout is actually done before renaming is the disciplined version of it. The hard part is knowing when "actually done" is true, when half the callers are the ones you cannot see. Do you have a way of checking that — a search across callers, or a deprecation warning — or is it more a judgement call when the rollout looks complete?

Thread Thread
 
smtahosin profile image
S M Tahosin • • Edited

Both, in that order. First I search through the callers I can see, then the deprecation warning goes on the old path and I observe the logs for a while. That warning is what catches the callers I could not see, because they are sitting in production whether I know about them or not. When the warning goes silent, I know it is done.

Thread Thread
 
userxnom profile image
Abdullah Naseer •

That order is exactly the discipline most people skip — search the callers you can see first, then let the deprecation warning catch the ones you cannot. I like that the warning is the detector, not the cleanup: it turns "probably safe to delete" into something the logs can actually confirm. How long do you usually let the warning stay quiet before you call it done — a few days, a full release cycle, or does it depend on how often that path gets hit?

Thread Thread
 
smtahosin profile image
S M Tahosin • • Edited

Depends on the traffic, really. A path that runs every day, couple days of silence says a lot there, but for something that happens once a month, it is a full release cycle, or I am just guessing. That weekly job running only on Mondays taught me this lesson the hard way.

Thread Thread
 
userxnom profile image
Abdullah Naseer •

That Monday-only job is the perfect cautionary tale — a quiet week there proves nothing, and a full cycle is the only honest wait. I like how your rule scales with the traffic instead of being one fixed window for everything. Do you keep the deprecation warning up for the whole cycle, or do you shorten it once the daily paths have gone quiet?

Thread Thread
 
smtahosin profile image
S M Tahosin • • Edited

Full cycle for me, each and every time. Daily paths falling silent, that only tells me the busy callers moved on, and the monthly job is exactly the one that will come back to bite me. Shortening that warning, it saves nothing worth the risk.

Thread Thread
 
userxnom profile image
Abdullah Naseer •

That is the part I keep coming back to — the quiet daily paths feel like proof, but they only prove the busy callers moved on, and the monthly job is exactly the one that would bite. Keeping the warning up for the whole cycle costs almost nothing next to the risk of a silent caller you never saw. Do you log which callers actually hit the deprecated path during the cycle, or is the quiet itself the only signal you watch?

Thread Thread
 
smtahosin profile image
S M Tahosin • • Edited

To be honest, the hits I log, but the log I keep mostly for what it represents when it's silent. A warning firing lets me know at least the caller is alive, and my only kind of silence comes in the form of a full cycle with zero hits. If the logging was not there, silence would just mean nobody looked.

Thread Thread
 
userxnom profile image
Abdullah Naseer •

That is the line that makes the whole thread land for me, Tahosin — "without the logging, silence just means nobody looked." The log turns quiet from an absence of evidence into actual evidence, which is exactly the difference between feeling safe to delete and just hoping. A full cycle with zero hits is a strong bar. When a cycle does end with zero hits and you finally remove the old path, do you keep the log line in place for one more cycle anyway, or does the clean result feel like enough to take it out too?

Thread Thread
 
smtahosin profile image
S M Tahosin • • Edited

Along with the path, I remove the log also. On the zero run the log has already done its job, so keeping it after the deletion only leaves one more piece of dead code for people to figure out. One time I left one behind after a cleanup, it stayed in the code shooting blanks for months and only in a second pass I removed it. For me clean result is all I need, thanks for putting it that way.

Thread Thread
 
userxnom profile image
Abdullah Naseer •

That is the cleanest way to put it, Tahosin — the log already did its work during the zero run, so leaving it in after the delete just turns it into the next bit of dead code someone has to puzzle over. Your story about the one that sat there firing nothing for months is the perfect proof: a log with no path left to watch is not safety, it is clutter wearing a safety costume. Taking it out with the path keeps the cleanup honest, and the clean result is the evidence. When you do that, do you remove the log in the same change as the delete, or in a separate pass so the deletion itself stays easy to review?

Thread Thread
 
smtahosin profile image
S M Tahosin • • Edited

Same boat here Abdullah lol. If I leave it for a second pass, that second pass never happens, my orphan log sat there for months doing nothing. But that one commit I always keep small, only the path and its log, nothing else mixed in, so the diff stays easy to eyeball. Thanks, enjoyed this thread a lot:)

Thread Thread
 
userxnom profile image
Abdullah Naseer •

That second-pass-never-happens feeling is so real, Tahosin — and your orphan log sitting there for months is the perfect proof of it. Keeping that one commit small, only the path and its log, is the discipline that makes the diff easy to eyeball and the cleanup honest. Thanks for sticking with the thread — which of the habits from this discussion are you most likely to actually keep using next time you open old code?

Thread Thread
 
smtahosin profile image
S M Tahosin •

Thanks Abdullah! honestly that one small commit thing is the one i'll actually keep... left a log cleanup for 'later' last month and later just never came lol, so now if i'm already in the file i just do it there and move on :D

Thread Thread
 
userxnom profile image
Abdullah Naseer •

That log cleanup story is the perfect proof of it, Tahosin — 'later' is where good intentions go to wait forever, and doing it while you're already in the file costs almost nothing. The one-small-commit rule only works because the commit stays small enough to actually finish. Which cleanup is sitting in your 'later' pile right now — and would it fit in the file you're already working in today?

Thread Thread
 
smtahosin profile image
S M Tahosin •

Thanks Abdullah! rn it's a debug log sitting in a worker file I opened twice this week lol... yea it fits, I'm literally in that file today so no excuse left xD one tiny commit and done, appreciate you :)

Thread Thread
 
userxnom profile image
Abdullah Naseer •

That is the best possible case, Tahosin — the file is already open, so the cleanup costs you almost nothing today and saves future-you a second mystery visit. A debug log in a worker file is exactly the kind of thing that looks harmless until it fills a log stream at the worst moment. What will you check first after that one tiny commit — does the worker still behave the same, or is there another log in that same file waiting for the same treatment?

Thread Thread
 
smtahosin profile image
S M Tahosin •

haha yea Abdullah file's already open so... no excuse left lol. worker first for me, run one job through, watch it land, then go hunting. there's always a second log in there somewhere xD last time i found 3 in one file. thanks for the nudge :)

Thread Thread
 
userxnom profile image
Abdullah Naseer •

Worker first is the right order, Tahosin — run one job through, watch it land, and only then go hunting, because the log you find after a real run is worth three you find by reading. Three in one file is the best argument for this habit I have heard yet, and it is also the warning: the second log is always hiding behind the first one. When you run that one job through, do you watch the log live while it runs, or read it after — and which one has caught the second log for you more often?

Thread Thread
 
smtahosin profile image
S M Tahosin •

reading it after Abdullah, almost always. live I'm useless lol I just watch the first line scroll past and go ooh there it is, then completely miss the second one 2 lines down.. happened on a tuesday night last month, I remember because my tea went cold. anyway thanks, glad it's not just me xD

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?

Thread Thread
 
sneha_kumari profile image
Sneha Kumari •

I usually start with the page to understand what triggers the request and what data is being passed. Then I follow it to the API service and backend to see what happens next. I feel starting from the UI makes it easier to connect all the pieces instead of getting lost in the backend straight away . 😅

Thread Thread
 
userxnom profile image
Abdullah Naseer •

That UI-first route makes a lot of sense, Sneha — starting from the page gives you the trigger and the data being passed before the backend has a chance to swallow you whole. It is the same instinct as reading the callers first, just from the other end of the chain. When the page and the API disagree about what data is being passed, which one do you trust first?

Thread Thread
 
sneha_kumari profile image
Sneha Kumari •

I usually trust the actual API request and response first, then trace back to see where the mismatch starts. The UI shows what the user intended, but the network request tells me what was actually sent. If those don't match, that's usually where I start digging!

Thread Thread
 
userxnom profile image
Abdullah Naseer •

That is a sharper check than my UI-first route, Sneha — the UI shows what the user intended, the network request shows what was actually sent, and the gap between them is the bug's address. I like that you trace back from the mismatch instead of forward from the code. When the request and the response both look right but the page still misbehaves, where do you look next — the state in between, or the render that consumes it?

Thread Thread
 
sneha_kumari profile image
Sneha Kumari •

I would probably check the state in between first. If the request and response look correct, I would trace how the response updates the state and whether anything else is overwriting it. Then I would check how the component uses that state during rendering. Sometimes the API isn't the problem at all, but what happens to the data after it arrives!

Thread Thread
 
userxnom profile image
Abdullah Naseer •

That state-in-between check is exactly the missing step, Sneha — if the request and response both look right, the bug is often in what happens to the data after it arrives, whether something else overwrites it or the component reads it wrong during rendering. It is a good reminder that the API is not always the suspect. In your MedMatch work, was it usually the state update or the rendering that turned out to be the culprit?

Thread Thread
 
sneha_kumari profile image
Sneha Kumari •

I usually start by checking the state update first. In MedMatch, I found it useful to verify whether the API response was actually reflected in the expected state before looking at the rendering. If the state itself is correct, then I would inspect how the component consumes and displays it. Breaking these steps down makes it much easier to narrow down the issue instead of debugging everything at once.

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?

Thread Thread
 
visionwei profile image
Wei Bin •

You're naming the exact trap: the pair you design around feels like the one that must matter most, precisely because you designed around it. It's circular — the design choice makes the pair feel central, and then the feeling justifies the design. Real traffic is the only thing that breaks the circle.

So yes, the rebuild question is the right one, and my answer is: the matrix has to follow the observed pair, not the designed one. The characterization test is what makes that safe — you write the probe down first, so the rebuild is a response to measured traffic, not a second round of guessing in the opposite direction. If the afterthought pair is what shows up constantly, it's not an afterthought anymore; it's the center, and the matrix should be organized around it.

The part I'll keep from your comment is the order of operations: probe first, rebuild second. Did you end up applying that same order anywhere in your own work — writing the test before changing the thing — or is that the part you're still borrowing?

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?

Some comments may only be visible to logged-in visitors. Sign in to view all comments.