I'm working through an older service and found a block of logic that clearly handles some unusual edge case, but there's no comment explaining why. The original author has left, and the relevant Git history became unclear during a migration. It feels like the kind of code that exists because someone once had a painful production incident.
In practice, would you leave it alone and add a note saying the reason is unknown, investigate old tickets, logs, and usage first, or put it behind a feature flag and gradually remove it? What's the safest way to understand and document behavior like this without accidentally deleting something important?
4 Answers
An AI coding assistant can help trace references, summarize the control flow, and suggest where to look, especially if you give it access to relevant logs or surrounding code. But treat its explanation as a hypothesis, not evidence. It can miss an external process that creates the edge case and confidently label important code as unnecessary.
Start with the code and its interfaces rather than spending hours on historical archaeology. Check whether the branch still executes in production, what inputs reach it, and whether it interacts with another service, device, or environment that is not obvious locally. If it is active, preserve it. If it appears unused, add temporary instrumentation or logging before deciding it is dead.
Treat it as intentional behavior until you have evidence otherwise. First understand what it does, then add a characterization test covering the current output and the suspected edge case. You can inspect history, tickets, and logs, but the test gives future changes a safety net. If you eventually prove the behavior is obsolete, remove or refactor it behind a flag and document what you checked and why it can go.
If the system is working, don’t clean it up just because the code looks strange. Add a useful comment describing what you currently know, what behavior must be preserved, and what would justify removing it. A comment that only says “mystery code” becomes folklore and doesn’t help the next person.

Exactly. A test makes the behavior visible to reviewers instead of leaving behind a vague comment like “don’t touch this.”