Reference
The bug zoo
A bug isn’t a disaster — it’s a clue. Almost every mistake belongs to a small family you can learn to spot on sight. Here are the ones the lab drills; each has a character who always makes it. The trick is the same every time: compare what it prints to the goal, and the line that makes them differ is the culprit. Open the debug lab →
- 🐞 Offby One short or one over
A loop bound is off by one — ..< stops before the number, ... includes it.
How to catch it: Check ..< vs ... and whether count should be count or count-1.
- 🐞 Equals Saying vs asking
= gives a value; == asks a question. An if needs ==.
How to catch it: '=' assigns; '==' compares — an if/while condition needs '=='.
- 🐞 Andor And vs or
&& is true only when both are true; || is true when either is.
How to catch it: Swap '&&' and '||' to match what you meant.
- 🐞 Range Reaches past the end
A loop or index goes up to count, but positions stop at count-1.
How to catch it: Use 0..<array.count (exclusive), not 0...array.count.
- 🐞 Zero Divide by zero
Dividing by 0 crashes — check the divisor first.
How to catch it: Fatal error: Division by zero — test the denominator before /.
- 🐞 Hush Wrong but it runs
No crash, no red — the logic just does the wrong thing; a test catches it.
How to catch it: Write a test with a known answer to reveal the wrong result.
- 🐞 Forever Never stops
The while test never becomes false, so the loop runs forever.
How to catch it: Make sure something inside the loop moves the test toward false.
Where this idea comes from
The moth in the logbook
Engineers called a hidden fault a "bug" long before computers. Thomas Edison used the word in letters in 1878 for the little faults that made a new machine misbehave.
One thing to hold: The Harvard Mark II computer’s logbook, 9 September 1947. The team found a moth stuck in Relay #70 of Panel F, taped it to the page and wrote: "First actual case of bug being found." The logbook is kept at the Smithsonian’s National Museum of American History.
Why it matters: The joke worked because the word was already old. A bug report does a job like that logbook’s: it writes down what you expected, what actually happened, and where you found the cause, so someone else can see the same thing.
What we don’t know: Grace Hopper told the story often and made it famous, but she said herself that she did not find the moth, and in her tellings it happened in 1945, while the logbook says 1947. Who wrote the line is not known, and people still argue about it.
Dates:
- 1878 Thomas Edison writes about "bugs" in his inventions.
- 1947 The Harvard Mark II team tapes a moth into the logbook.
- 1994 The logbook joins the Smithsonian’s collection.
Fixing a bug and saying why in one sentence — the “bug report” — is a real coder’s skill. Every program here is written in a teaching subset of the Swift language. Catch a bug →