Development Basic

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

Pronunciation

IPA: /bʌɡ/

Sounds like: “buhg” - one short syllable, the ‘u’ sounds like the ‘u’ in “cup”

Common 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 and her team at Harvard. They found an actual moth trapped in the Mark II computer that was causing malfunctions.

Hopper taped the moth to 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, but this incident popularized it in computing.

Types of Bugs

By severity

LevelNameExample
🔴 CriticalBlockerApp won’t start, data loss
🟠 MajorMajorMain function doesn’t work
🟡 MinorMinorVisual error, typo
🟢 TrivialTrivialAesthetic, 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);
  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

YearBugImpact
1962Mariner 1Rocket destroyed due to missing hyphen ($18.5M)
1996Ariane 5Rocket exploded due to integer overflow ($370M)
1999Y2KWorldwide panic over 2-digit dates
2014HeartbleedOpenSSL vulnerability affected millions of sites
2021Log4ShellCritical 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).