<p>You've been working on your code for 20 minutes. You run it, and... nothing works. Or worse — you get a confusing error message that makes no sense. Sound familiar? Welcome to the world of debugging! Every programmer, from beginners to the engineers at Google, spends a huge amount of their time debugging. The good news is that debugging is a learnable skill — and it's actually pretty fun once you get the hang of it.</p> <div class="callout"><p><strong>The word "bug" has been used in computing since 1947 when engineer Grace Hopper found an actual moth stuck in a computer relay causing errors. She taped it into her notebook — the original computer bug!:</strong></p></div> <h2>Step 1: Read the error message (really read it)</h2> <p>Error messages look scary, but they are actually trying to help you. Most error messages tell you three things: what went wrong, which line of code it happened on, and what type of error it is.</p> <pre class="post-code"><code># This code has a bug name = "Timi" print("Hello," + nme) # NameError: name 'nme' is not defined</code></pre> <p>See that? The error says NameError and points to the exact line. It's telling you that 'nme' doesn't exist. The fix? Change nme to name. Always read the full error message before doing anything else.</p> <h2>Step 2: Check the obvious things first</h2> <p>Most bugs in beginner code come from a small set of common mistakes. Before doing anything complex, check these first:</p> <ul><li><strong>Typos:</strong> A misspelled variable name or function name is the most common bug. Check your spelling carefully.</li><li><strong>Missing colon or bracket:</strong> In Python, an if statement or loop must end with a colon (:). A missing bracket in any language will break everything.</li><li><strong>Wrong indentation:</strong> Python uses indentation to understand your code's structure. One extra or missing space can cause an error.</li><li><strong>Using a variable before defining it:</strong> You can't use a variable if you haven't created it yet.</li></ul> <h2>Step 3: Use print statements to track what's happening</h2> <p>If you can't figure out why something isn't working, add print() statements to see what your program is actually doing at each step. This is called print debugging and it's one of the most powerful tools you have.</p> <pre class="post-code"><code>total = 0 for number in [1, 2, 3, 4, 5]: total = total + number print("After adding", number, "total is:", total) print("Final total:", total)</code></pre> <p>By printing at each step, you can see exactly where your result goes wrong.</p> <h2>Step 4: Google it (seriously, everyone does this)</h2> <p>Copy the exact error message and paste it into Google. You'll almost certainly find someone who has had the exact same problem and a solution. Websites like Stack Overflow are full of answered questions. Even professional developers Google error messages multiple times a day — it's not cheating, it's smart.</p> <h2>Step 5: Take a break</h2> <p>This sounds counterintuitive, but it works. If you've been staring at a bug for a long time, walk away for 5 to 10 minutes. Get a drink, take a walk, look out a window. When you come back, you'll almost always spot the problem immediately. Your brain needed time to process.</p> <div class="callout"><p><strong>The most important thing about debugging:</strong> it's not a sign that you're bad at coding. It's a sign that you're doing coding. Every bug you fix makes you a better programmer.</p></div>
Found this useful? Share it!