Read Code Fast

Lesson 21 Fake success

6 min read · Python · Quiz

swallowed errors, fallback values, TODOs and logs instead of failures

What you'll learn

  • Recognise a catch or except that hides a failure
  • Spot stubs, TODOs and defaults that fake success
  • Ask whether the caller can tell a failure from an empty result

The scariest AI code never crashes. It seems to work: it returns a nice empty list, an "ok", a default. The failure is hidden.

The idea

AI hates an error. So it adds a try/catch that returns an empty value, a fallback number, a stub that says "ok", or a log line, and the crash goes away. But the problem is still there: you just can't see it. The user sees "no orders" when the server is down, or "paid" when nothing was charged.

The test is simple: after a failure, can the caller tell? If a failed call and a real empty result look the same, that's fake success. Search for catch, except, return [], ?? 0, TODO and pass, and read what happens next.

orders.py
1def load_orders(user_id):2    try:3        resp = requests.get(f"{API}/users/{user_id}/orders", timeout=5)4        return resp.json()5    except Exception:6        logger.warning("could not load orders")7        return []89def charge_card(order):10    # TODO: call the payment provider11    pass1213orders = load_orders(42)14if not orders:15    show_empty_state()
  1. Line 4 requests doesn't raise on a 404 or 500. Without resp.raise_for_status(), the error page is read as if it were the orders.
  2. Line 7 The error is logged and thrown away. The caller gets [] for a failure, the same as for "no orders".
  3. Line 10 A stub with pass returns None and does nothing. Any code that doesn't check the result will carry on as if it worked.
  4. Line 14 The screen can't tell "no orders" from "broken". The user is told the wrong thing.

Take away

  • After a failure, the caller must be able to tell.
  • An except that returns an empty value, a default or "ok" is fake success.
  • Search for except, return [], .get(x, 0), TODO and pass, and read what happens after them.

Quiz: test yourself

7 questions: a quick quiz, then a review drill. Pick an answer to see why it's right.