Script Valley
Reading Other People's Code
Debugging Code You've Never SeenLesson 4.4

How to form and test a hypothesis when debugging unknown code

scientific debugging method, hypothesis formation, minimal reproducible example, binary search debugging, rubber duck debugging, documenting findings

Debugging Is Empirical, Not Random

Scientific debugging loop diagram

Random console.log placement is not debugging — it's hoping. Systematic debugging starts with forming a specific, falsifiable hypothesis, then designing a test that proves or disproves it.

The Scientific Debugging Method

// Bug report: "User profile sometimes shows wrong name"

// Step 1: Observe the symptom precisely
// - Happens on profile page load
// - Only for users who logged in via Google OAuth
// - Not reproducible for email/password users

// Step 2: Form a hypothesis
// Hypothesis: Google OAuth users have a 'displayName' field
// but the profile page reads 'name' — undefined for OAuth users

// Step 3: Test the prediction
console.log('user fields:', Object.keys(currentUser));
// Output: ['id', 'email', 'displayName', 'googleId']
// 'name' is missing — hypothesis confirmed

// Step 4: Fix and verify
// const name = user.name ?? user.displayName ?? 'Anonymous';

Binary Search for Bugs

If you can't isolate the bug, add a check at the halfway point of the execution path. If the state is correct there, the bug is in the second half. If wrong, it's in the first half. Repeat until you've narrowed it to one function. This is git bisect's core idea applied to code reading.

Up next

How to read logs and identify patterns in production errors

Sign in to track progress