Independent coding guide. Not affiliated with any editor, IDE or course listed here.

How do you debug code step by step?

Reproduce the bug reliably, read the full error message, narrow down where it happens, test one guess at a time, fix it, then add a check so it cannot quietly return.

What this guide is based on. The method below is our own, built from common practice. We have not yet reviewed published debugging research for this page, so it contains no study figures. Where a point rests on a source, it is linked.

Last updated November 2026

What is the first thing to do when code breaks?

Make the bug happen again on purpose. If you cannot reproduce a bug, you cannot tell whether a fix worked. Write down the exact input, the steps, what you expected and what happened instead. That short note often points at the cause before you open the editor.

Then read the whole error message, from the bottom of the traceback up in Python. It usually names the kind of error, the file and the line. Beginners often read only the first line or skip straight to searching online.

What is a reliable debugging method?

A reliable method is a loop: observe, guess, test one change, observe again. The steps below put that loop in order. They work in any language and with any editor.

  1. Reproduce. Find the smallest input that triggers the bug every time.
  2. Read the error. Note the error type, file and line number.
  3. Locate. Find the first point where a value is not what you expect. Use the debugger's breakpoints or print the value.
  4. Isolate. Cut the code down, or comment out parts, until only the failing piece remains.
  5. Guess. State one hypothesis in a sentence: "total is zero because the list is empty".
  6. Test one change. Change one thing, run again, compare. Undo changes that did not help.
  7. Fix and verify. Run the original reproduction and confirm it now works.
  8. Guard. Add a test or a check so the same bug cannot return unnoticed.

Which debugging tools should beginners learn?

Learn print statements first, then your editor's debugger. Print statements are quick and work everywhere. A debugger lets you pause on a line, inspect every variable and step forward one line at a time, which is faster once a bug spans several functions.

Debugging techniques and when to use them (our advice)
TechniqueBest forLimit
Reading the tracebackCrashes with an error messageShows where it failed, not always why
Print statementsChecking a value at one pointClutters code; remove them after
Breakpoints and steppingBugs that span several functionsNeeds editor setup
Cutting the code downBugs in large filesTakes time, but always works
Explaining it aloudLogic errors with no messageNeeds a patient listener, or a notebook
Version history diff"It worked yesterday"Needs regular commits

Browse code editors and IDEs by language and platform.

How does readable code make debugging easier?

Readable code makes wrong code look wrong. PEP 8 notes that code is read far more often than written [1], and debugging is reading under pressure. Clear names and short functions reduce the places a bug can hide. Consistent indentation matters literally in Python: mixing tabs and spaces is not allowed [1], and you can test for it with the snippet check on our home page.

Where should you look for help?

Start with the official documentation for the function or library involved. It was the most used learning resource among developers in the 2025 Stack Overflow survey, at 68%, ahead of online resources at 59% and Stack Overflow at 51% [2]. When you ask a person or a forum, include the smallest code that reproduces the bug, the full error and what you already tried.

Common mistakes

  • Changing several things at once. If it starts working, you will not know which change fixed it.
  • Fixing the symptom. Catching an error and ignoring it hides the bug instead of fixing it.
  • Trusting your assumptions. Check the value; do not assume it.
  • Debugging while tired. A short break often beats another hour of guessing.
  • Leaving debug prints behind. They clutter output and confuse the next reader.
  • Not committing working code. Without a known good version, "what changed?" has no answer.

Summary

  • Reproduce first; a bug you cannot trigger cannot be confirmed fixed.
  • Read the full error message before searching.
  • Find the first wrong value, then isolate the code around it.
  • Test one hypothesis and one change at a time.
  • Add a test or check so the bug stays fixed.

Related: clean code tips and code review best practices.

Sources

  1. Python Software Foundation, PEP 8: Style Guide for Python Code (2001-07, viewed 2026-11)
  2. Stack Overflow, 2025 Developer Survey press release (2025-07)
CodingTips.comis for sale by its owner, website included.For sale, website included
Buy now