DevelopmentBasic

Bug

An error or defect in code that causes unexpected or incorrect behavior in software. One of the most iconic terms in programming.

Pronunciation

/bʌɡ/
"buhg"

What is it

A bug is an error, defect, or flaw in code that causes software to behave unexpectedly or incorrectly.

It can manifest as:

  • A button that doesn’t work
  • An incorrect calculation
  • An application crash
  • Data that gets lost or corrupted
  • Strange behaviors under certain conditions

Common pronunciation mistakes

  • ❌ “boog” (not like “book”)
  • ❌ “boug” (not a long ‘u’ sound)

Origin of the Term

The Moth Legend (1947)

The term was popularized by Grace Hopper, who worked with the Harvard Mark II computer team. In 1947, operators traced a malfunction to an actual moth trapped in a relay.

The moth was taped into the logbook with the note:

“First actual case of bug being found”

This historic log is preserved at the Smithsonian National Museum of American History.

Note: The term “bug” was already used in engineering before this (Thomas Edison used it in the 19th century); the anecdote, which Hopper retold many times, popularized it in computing.

Types of Bugs

By severity

Level Name Example
🔴 Critical / Blocker Critical / Blocker App won’t start, data loss
🟠 Major Major Main function doesn’t work
🟡 Minor Minor Visual error, typo
🟢 Trivial Trivial Aesthetic, doesn’t affect functionality

By technical type

┌─────────────────────────────────────────────────────┐
│              COMMON BUG TYPES                        │
├─────────────────────────────────────────────────────┤
│ Logic       │ Code does something different than    │
│             │ expected                               │
├─────────────────────────────────────────────────────┤
│ Syntax      │ Writing error that prevents           │
│             │ compile/execution                      │
├─────────────────────────────────────────────────────┤
│ Runtime     │ Error that occurs during execution    │
├─────────────────────────────────────────────────────┤
│ Concurrency │ Problems with multiple threads/procs  │
├─────────────────────────────────────────────────────┤
│ Memory Leak │ Program doesn't release memory        │
└─────────────────────────────────────────────────────┘

Code Examples

Logic bug (the most common)

// ❌ BUG: Off-by-one error
function getLastElement(array) {
  return array[array.length];  // 🐛 Should be length - 1
}

// ✅ CORRECT
function getLastElement(array) {
  return array[array.length - 1];
}

Comparison bug

// ❌ BUG: Incorrect comparison
function isAdult(age) {
  if (age = 18) {  // 🐛 Assignment instead of comparison
    return true;
  }
  return false;
}

// ✅ CORRECT
function isAdult(age) {
  return age >= 18;  // Use >= for comparison
}

Null/undefined bug

// ❌ BUG: Not checking for null
function getUserName(user) {
  return user.name.toUpperCase();  // 🐛 Crash if user is null
}

// ✅ CORRECT
function getUserName(user) {
  return user?.name?.toUpperCase() ?? 'Anonymous User';
}

Real-World Example: The Shopping Cart Bug

Imagine this situation in an online store:

The bug report

User reports: “When I add a product and then remove it, the total still shows the price of the removed product.”

Investigation

// ❌ CODE WITH BUG
function removeProduct(cart, productId) {
  const index = cart.items.findIndex(item => item.id === productId);
  cart.items.splice(index, 1);
  // 🐛 BUG: Forgot to recalculate the total
}

// ✅ FIXED CODE
function removeProduct(cart, productId) {
  const index = cart.items.findIndex(item => item.id === productId);
  if (index === -1) return; // If not found, splice(-1, 1) would remove the last item
  cart.items.splice(index, 1);
  cart.total = cart.items.reduce((sum, item) => sum + item.price, 0);
}

Lessons learned

  1. Unit tests would have detected the bug
  2. Code review could have prevented it
  3. Documentation of the complete flow helps

Famous Bugs in History

Year Bug Impact
1962 Mariner 1 Rocket and probe destroyed after launch due to a symbol omitted from the guidance software, popularized as “the missing hyphen” (US$18.5M at the time)
1996 Ariane 5 (flight 501) Rocket self-destructed after an overflow converting a 64-bit number to a 16-bit integer (losses of over US$370M)
1999-2000 Y2K Costly worldwide remediation of dates stored with 2 digits
2014 Heartbleed OpenSSL vulnerability affected millions of sites
2021 Log4Shell Critical vulnerability in Log4j

The Bug Lifecycle

┌─────────┐    ┌──────────┐    ┌────────────┐    ┌──────────┐
│   New   │ → │ Assigned │ → │In Progress │ → │ Resolved │
└─────────┘    └──────────┘    └────────────┘    └──────────┘
     ↓                                                ↓
┌─────────┐                                    ┌──────────┐
│ Closed  │ ← ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ← │ Verified │
└─────────┘                                    └──────────┘

How to report a bug correctly

A good bug report includes:

  1. Descriptive title: “Cart doesn’t update total when removing products”
  2. Steps to reproduce:
    • Add product X to cart
    • Remove product X
    • Observe the total
  3. Expected behavior: Total should be $0
  4. Actual behavior: Total still shows $100
  5. Environment: Chrome 120, Windows 11, production
  6. Evidence: Screenshot or video
  • Debug - The process of finding and fixing bugs
  • Testing - Process to detect bugs before production
  • Hotfix - Urgent fix for a bug in production
  • QA - Quality Assurance, the team that looks for bugs

Remember: Bugs are inevitable. What matters is having processes to detect them early (testing), fix them quickly (debugging), and learn from them (retrospectives).