bugger.
DEBUGGING METHOD / PRACTICAL GUIDE

How to Debug Code: A Repeatable 6-Step Method

To debug code, define what should happen, reproduce what actually happens, and find the smallest explanation that accounts for the difference. Test one hypothesis at a time. Finish by proving that the fix handles the original failure and preserves the surrounding behavior.

How do you debug code? The short version

Debugging is the process of finding the smallest explanation that accounts for the difference between what the code does and what it should do. Work through six steps in order, and do not skip straight to editing.

  • Define the expected behavior as a checkable statement, not "it is broken".
  • Reproduce the failure with the same input, setup and event order.
  • Reduce the example until removing anything more makes the bug disappear.
  • Predict an observation before you edit, so you can be proven wrong.
  • Repair the shared assumption at the right boundary, not the sample input.
  • Keep the original failure as a regression test and check nearby valid cases.

1. Define the expected behavior

Turn “it is broken” into a checkable statement. Record the exact input, expected output, actual output, and any relevant environment or prior operations. If two people disagree about the expected output, settle the contract first; changing code cannot resolve an ambiguous requirement.

For example, an age rule may require people aged 18 and older to qualify. “An 18-year-old is rejected” is a more useful report than “the eligibility function is wrong.” It points directly to a boundary that you can test.

2. Make the failure repeatable

Use the same input and setup until you can trigger the same result. For intermittent failures, record the order of events and keep a timeline. A cache, connection pool, or queue may carry state between operations, so preserve that history in the reproduction.

If the failure disappears, treat that as evidence. Compare what changed: a fresh process, a different timezone, a new connection, or a different packet boundary. Do not assume an intermittent failure has fixed itself.

3. Reduce the example without losing the bug

Remove unrelated records, configuration, and code paths one at a time. Run the reproduction after each reduction. Stop when removing another part makes the failure disappear. You now have a smaller explanation to search for and a better regression case to keep.

For the age example, the surrounding interface is unnecessary. The rule can be checked directly with a boundary pair:

Python example
# Contract checks for an existing eligible(age) function:
assert eligible(17) is False
assert eligible(18) is True
assert eligible(19) is True

4. Predict an observation before editing

State a hypothesis in terms you can disprove: “The comparison excludes the exact minimum age.” Inspect the condition or add a focused diagnostic. If the condition already includes 18, the hypothesis is wrong; follow where the age value comes from instead.

Change one relevant thing at a time. Multiple speculative edits can make a test pass while leaving you unable to explain which change mattered. Keep the failing reproduction running as you investigate.

5. Repair the cause at the right boundary

Fix the shared assumption that produced the invalid behavior. Avoid special-casing the sample input, suppressing an exception, or weakening a test just to get a green result. Re-read the callers and their contracts before changing a shared function.

A good explanation connects the input, the incorrect decision, and the correction. If you cannot say why the original failed and why the new behavior follows the contract, keep investigating.

6. Keep the evidence as a regression test

Run the original reproduction, nearby valid cases, and the relevant existing tests. Include boundaries such as empty input, zero, repeated operations, and maximum values when the contract allows them. Remove temporary diagnostics and review the final diff for accidental changes.

Bugger makes this process concrete: read a bug report, inspect the small Python or Rust program, run a reproduction, and submit a correction. After acceptance, read the explanation and compare your reasoning with the failure mechanism.

  • Can you reproduce the original failure?
  • Does the fix explain the root cause?
  • Does the regression test fail before the fix and pass after it?
  • Do the surrounding behaviors still work?

Frequently asked questions

What is the fastest way to debug an unfamiliar bug?
Write down the exact failing input and expected output, reduce the input until removing anything makes the bug vanish, state one falsifiable hypothesis about what broke, and verify your fix with a regression test.
Why should you write a failing test before fixing a bug?
A test that fails for the reported reason proves your reproduction matches the real defect and guarantees the bug stays fixed as the codebase evolves.

Further reading

Start with primary reports and documentation. Community discussions are discovery leads, not evidence on their own.

PUT THE METHOD TO WORK

Practice on a focused challenge.

Explore all challenges
BUILD A REGULAR PRACTICE

Start free. Keep following the evidence.

Explore the current challenge collection, or see what is planned for future practice packs.