chiefletterAll stories

Tools & practice / CHIEFLETTER JOURNAL

Debugging Code Without Random Guessing

A calm debugging routine for reproducing a problem, narrowing its cause, and keeping the fix from creating a second one.

Code and terminal on a dark computer screen
Photography via Unsplash

Reproduce the failure first

A bug report that says “it sometimes breaks” is a starting point, not yet a debugging plan. Find the smallest sequence of actions that makes the failure happen. Record the inputs, environment, and exact result. If the issue is intermittent, look for conditions that vary: timing, network response, user permissions, or a particular data shape. Reproduction turns a vague frustration into something you can test.

Before changing code, confirm you can still make the bug appear. That sounds obvious, but a developer can spend an hour “fixing” a failure that was caused by stale data or a different build. Save the reproducing steps. Once the change is made, those steps become your first regression check.

Make one hypothesis at a time

A useful hypothesis predicts an observation: “This record disappears because the filter treats an empty string as false.” A vague guess such as “the database is weird” gives you nowhere to look. State what you think is happening, add the smallest diagnostic check, and see whether the evidence supports it. If it does not, remove the check and update the hypothesis.

Avoid changing three files and adding a retry before you know which part failed. Multiple simultaneous changes make cause and effect harder to see. If you are tired or under pressure, write down each experiment. The notes prevent the same failed idea from being repeated and let a teammate understand what has already been ruled out.

Shrink the problem

Reduce the failing case by removing inputs, steps, or code until the smallest version that still fails remains. If a page crashes with one thousand records, try ten. If a function fails only after a series of events, isolate the last event. A small case is easier to inspect, and it can reveal which variable or interaction matters.

Use logs at boundaries where values enter or leave a component. Inspect the value’s shape, not just its presence. An object that exists but has a missing field can be the real source of a later error. Remove temporary logging after the investigation or replace it with a deliberate diagnostic that is useful in production.

For a wrong checkout total, record the input items, expected value, actual value, and the first point they differ. A small test separates rounding from a missing item.

Use the tools you already have

A debugger lets you pause execution, inspect variables, and step through code. Learn the basics in the environment you use most. Browser developer tools can expose failed requests, console errors, and layout problems; a command-line debugger can show how state changes across calls. You do not need every tool. The goal is to observe the program at the point where your mental model and its behavior diverge.

Read error messages from the bottom line and follow the stack to the first frame you own. A stack trace can include framework code, but the useful clue may be the first place your function called it with an unexpected value. Search the exact error when you have enough context to judge whether an online answer fits your case.

Fix the cause and check the nearby ground

Once you understand the cause, make the smallest change that addresses it. Add a test if the failure represents behavior that should remain stable. Then rerun the original reproduction and one nearby case. A fix for “empty list” should not break a list with one item. If the code is shared, ask someone to review the explanation as well as the diff.

Leave a note in the issue or commit message: what failed, why it failed, and how you checked the fix. That record helps future maintainers and helps you recognize the pattern next time. Debugging skill is cumulative; each clear investigation adds a little structure to the next unfamiliar problem.

When a fix appears to work, repeat the original sequence after a fresh page load and test one nearby edge case. Record the exact input and result in a small automated check when possible. If the error disappears only after a restart, investigate state or caching before calling the issue resolved.

A short working note

Try this with your work

A calm debugging routine for reproducing a problem, narrowing its cause, and keeping the fix from creating a second one.

0 of 30 of 3 steps
Your note stays in this browser.

Problem note

Capture a useful technical observation.

Video resources

For a visual walkthrough related to this topic, see this YouTube learning resource. It opens a topic search so you can choose a current explanation that fits your level.

Frequently asked questions

What is the first step in debugging?

Reproduce the problem consistently and write down the inputs and steps that cause it. Without a reproduction, it is difficult to know whether a change fixed the right thing.

Is print debugging bad?

No. Logging or printing values is a useful way to inspect behavior. Use it deliberately, remove temporary output, and avoid exposing private data in logs.

How long should I spend debugging before asking for help?

Ask when you can describe the expected and actual behavior, show a small reproduction, and summarize what you tried. There is no benefit to hiding a blocker for hours.

How do I avoid fixing symptoms instead of causes?

Write a hypothesis that explains the failure, test it, and trace the value or state back to where it first becomes incorrect. Then check a nearby case after the fix.