|
Programming

How to debug code without panicking

The red text is not a verdict on your intelligence. It is a location and a reason. Read it slowly, shrink the problem, and change one thing at a time.

How to debug code without panicking

It is 1:40am and the page is white. Not blank in a design way. Blank in the way that means something crashed and the browser has given up. There is a red error in the console, forty lines long, and half of it is a file you have never opened. Your chest tightens. You start changing things at random.

That moment is where most beginners lose a night. Not because the bug is hard, but because the panic is louder than the error message.

The error is trying to help you

An error message has three useful parts: what went wrong, where it happened, and the chain of calls that led there. Beginners read the first line, see words like undefined or unexpected token, and stop. Read the whole thing, slowly, out loud if you can.

Start at the bottom, not the top. The last line usually names the actual error. Then move up to the first line that mentions a file you wrote. Ignore the lines from libraries and frameworks for now. Your bug is almost always in your file, even when the error appears in someone else's.

If it says Cannot read properties of undefined (reading 'name'), the question is simple: which object is undefined, and why? That is a much smaller question than "why is my app broken".

Make the bug happen on purpose

A bug you can reproduce is a bug you can fix. A bug that appears randomly is a bug you will chase for a week. Before you change anything, find the smallest set of steps that makes it happen every time.

Click this button, with this input, on this page. If it only happens with a name that has an apostrophe, that tells you the problem is in how the query is built. If it only happens after you edit a record twice, that tells you something is not refreshing.

Then shrink the code. Comment out half the function. If the bug disappears, it is in the half you removed. If it stays, it is in the other half. Repeat. This is binary search applied to code, and it works on almost everything.

Before fancy debuggers, use print statements. console.log in JavaScript, print() in Python, var_dump() in PHP. Print the value before the line that crashes, and print its type. Half the time the value is not what you assumed.

Print the input, the output, and the thing in between. If a function receives the right data and returns the wrong result, the bug is inside the function. If it receives the wrong data, the bug is upstream, and you have been staring at the wrong file.

Logs are the same idea for the server. When the browser shows a generic 500 error, the real message is in the server log, not on the screen. Find the log file, tail it, and trigger the error again. The answer is usually sitting there in plain text.

Explain it to someone who does not care

There is a reason rubber duck debugging works. Explaining the code line by line forces you to notice the assumption you skipped. I have solved more bugs by saying "and here the variable is definitely not null" out loud, then stopping, than by any tool.

If no one is around, type the explanation in a notes file. "This function should take the user id, query the database, and return the row. It receives the id, but the query uses email instead." Writing it makes the mismatch impossible to ignore.

This is also why a second pair of eyes is worth more than another hour of staring. A friend does not need to know the framework. They just need to ask "why" at the right moment.

Search well, not desperately

Copy the error text, remove your specific variable names and file paths, and search that. Add the framework and version. If you paste the exact error with your name in it, you will find nothing.

Stack Overflow is still useful, but check the date and the votes. An answer from 2013 about jQuery might be the top result and completely wrong for your setup. MDN is better for JavaScript and browser APIs. The official docs are better for everything else.

When you find an answer, do not paste it and move on. Read why it works. If you cannot explain the fix to yourself, you have borrowed a solution and you will be back with the same bug in a different file.

The bugs that hide in plain sight

Some bugs are not logic errors. They are environment errors. Check these before you rewrite a function:

  • The server was not restarted after you changed the config.
  • The browser cached the old JavaScript.
  • You edited the wrong file, or the right file in the wrong folder.
  • Your database has two rows with the same id because you seeded it twice.

I once spent four hours on a "broken" login. The password was correct. The issue was that I had two .env files and the app was reading the one I forgot about. Four hours. The fix took ten seconds once I printed the config value.

So before you rewrite the function, check the boring things. Restart the server. Hard refresh the page. Confirm which file is actually loaded. Read the log timestamp to be sure it is today's error.

When to walk away

After ninety minutes on the same bug, your brain stops generating new ideas and starts repeating the same three. Close the laptop. Sleep. The solution often arrives in the shower or on the bus, because your brain keeps working without the panic.

This is not laziness. It is method. The best debuggers I know are not the ones who stay longest. They are the ones who stay calm, take notes, and know when to stop.

What to do this week

Take one error you have been avoiding. Read the full message from the bottom. Write down the file and line. Print the value on the line before it. Change one thing. If that does not work, comment out half the code and find which half keeps the bug. Do not touch anything else until you know.

For the wider picture, read how to read documentation without getting lost and why your code works on your laptop but not on the server. If you want to learn this with guided projects, the Software development for absolute beginners course walks through real errors instead of perfect code.

Dave Partner

Dave teaches digital skills at Braintem and has helped thousands of Nigerians move from watching tutorials to charging for real work. He writes about the parts nobody puts in the course trailer.