Grade 9 Computer Science · Abstraction

Leaky abstractions

Why every abstraction breaks eventually, and what one does then.

The same call, a thousand times longer

Two calls to the same function look completely identical. One comes back at once, the other takes a thousand times longer, and neither has a bug. Whoever responds by rewriting everything is nearly always wrong — but so is whoever tries to learn everything underneath.

An abstraction promises there is no need to know what is beneath. Sometimes the promise breaks: the implementation beneath shows through the layer and affects whoever uses it, although it ought to have been invisible. This happens to every abstraction, and understanding where and when is the difference between someone who can use tools and someone who can fix them.

The leakHow it looks
PerformanceAlways correct, sometimes slow
FailureA message from a world you do not know
Edge casesBehaviour not in the promise

What one does when the abstraction leaks

The first reaction of many is to give up the abstraction and go down to write everything themselves. That is nearly always an over-reaction: an abstraction that leaks in one place still saves in the other ninety-nine. The approach that works is to learn only the layer beneath, and only as far as is needed to understand this particular leak. Whoever knows what the structure does underneath can guess why it is slow here, without rewriting it. That is why it is worth knowing what lies beneath even when it is not needed — not to use it, but to understand when something shows through.

Example 1 — a performance leak

  1. Two calls look wholly identical.
  2. One is immediate and the other very slow.
  3. The promise mentioned no such difference.
  4. The implementation beneath shows through it.

Example 2 — a message from another world

  1. The code calls a simple function.
  2. The message speaks of a deep layer.
  3. The user does not know the terms.
  4. The failure leaked through the abstraction.
A scenario: The report that suddenly slowed — at each step a course of action is chosen and its consequence shown; 5 possible situations.