Contents Lesson 13 of 21

9 min read · foundations

Debugging is a method, not a talent

By the end of this lesson you will have broken one of your unit 2 programs twice on purpose, found each fault by stating a belief and checking it, and written both down in notes/debugging.md.

You already read an error message in the last unit. That was one skill. This is the other one, and it is the difference between an hour and a week.

Most people, when something breaks, start changing things. They try one idea, then another, then a third, and if it starts working they do not know which change fixed it — which means they cannot fix it again. That is not debugging. That is shuffling.

You are not hunting a bug

You are hunting the first place where reality stopped matching what you believed.

Every program you write is a stack of beliefs. "The file loaded." "The number is a number." "The response has a close field." When something breaks, one of those beliefs is false, and everything after it is nonsense. Your job is not to spot the mistake by staring. It is to find the earliest false belief and check it.

The method

1. Make it happen on demand. A failure you cannot reproduce is one you cannot fix, because you will never know whether it is gone. Write down the exact steps. If it only happens sometimes, note what is different when it does.

2. Read the whole error, message first. Node prints the error message at the top, with the file and line number of the failing code right above it (for example changes.js:4), then the at lines with the most recent call first. Skip the lines inside libraries you did not write (node:internal). Read the message, then the first at line that names a file you recognise. The last line of the output is only the Node version.

3. Say out loud what you believe. "At this point, rows is an array of five objects." Full sentence, specific.

4. Check that belief. Print it. console.log("rows:", rows) on the line before the one that fails. Not a guess — a look.

5. Change exactly one thing.

Printing is not cheating

Beginners hesitate to add console.log as though it were an admission of failure. Professionals do it constantly. It is a window into a running program, and a running program is otherwise completely opaque.

Print the thing, not a message about the thing:

console.log("about to parse");        // tells you almost nothing
console.log("body:", body);           // tells you what actually arrived

Label your prints with the variable name. Three unlabelled numbers in a terminal are worse than none.

"It worked yesterday" is evidence

If it worked before and does not now, something changed, and the list of things that changed is short. Your code (check git diff and git log). Your data. Something outside — a service that is down, a key that expired, a network you are not on.

That is why the last unit had you commit. git diff answers "what did I change?" in one command, and that question is the fastest route to most failures you will hit this year.

The trap

The trap is the fix that works for a reason you never found out. You changed three things, it works, you move on. Two days later it breaks again and you have no idea why, because you never knew why it worked.

If a change fixes it, undo the change and confirm it breaks again. That takes ten seconds and turns a coincidence into knowledge.

Try it now

Take two of the programs you wrote in unit 2 and break each on purpose. In changes.js, misspell a variable name somewhere in the middle. Run it. Read the error and find the line number before looking at your code. Then fix it. Then do it again in functions.js with a different kind of break: make formatPercent return x * 100 (a number) where text with a % is expected, and run the file. Put each file back with git restore changes.js functions.js before you move on. Notice that the second failure does not announce itself nearly as loudly. Write both down in a new file, notes/debugging.md: make the folder first with mkdir notes.

The prompt

Do the first break yourself, then paste the error and the code into your chat assistant:

I broke a program of mine on purpose and I am practising a debugging
method. Below are the error it printed and the code. Do not fix it and
do not tell me which line is wrong. Tell me which line of the error to
read first, and what belief about my program that line shows to be
false. Then ask me one question that would help me check that belief.

[paste the error, then the code]

A good answer points you at the line naming your own file, puts the false belief as a sentence — "every name on line 4 is one this file defines" — and asks you a question instead of handing you the fix.

Check it yourself

  1. The misspelled name prints ReferenceError, your file's path and a line number, and that line holds the typo.
  2. You wrote the belief down as a full sentence before printing anything.
  3. The second break prints no error where you made it. A function that should give back text and now gives back a number shows up as a strange value further on, or as nothing visible at all.
  4. Undo each fix once and the failure comes back; redo it and it goes away.

Compare with ours

Our first break, and our notes

closse typed for closes in changes.js. Node printed this, with the path shortened:

...\eodhd-course\changes.js:4
  const change = (closse[i] / closes[i - 1] - 1) * 100;
                 ^

ReferenceError: closse is not defined
    at Object.<anonymous> (...\eodhd-course\changes.js:4:18)

And notes/debugging.md:

Break 1: closse for closes, changes.js
  Error: ReferenceError, changes.js line 4, column 18
  False belief: every name on line 4 is one this file defines.
  Confirmed: fixed it, broke it again, fixed it again.

Break 2: formatPercent gave back x * 100 instead of text
  Error: none. It printed the number 12.34 where the text 12.34% belonged.
  False belief: formatPercent gives back text.
  Check: console.log("type:", typeof formatPercent(0.1234)) printed number.

Checkpoint

Your programs should be back as they were — git diff shows nothing for them. Then commit the notes:

git status   # only the files you expect, and no .env*
git add -A
git commit -m "Two breaks on purpose: the false belief behind each, and the check that found it"

Then answer this lesson's questions.