Friday, November 15, 2002

Leaky Abstractions

Classy posts some thoughts on Joel’s Law of Leaky Abstractions. I think Joel’s basically right (that one should be careful about abstractions), but he frames his argument in a slightly misleading way.

Here’s how I look at it. An abstraction is an idea, like a specification. It can be good or bad for a particular purpose, but it can’t fail. If the implementation doesn’t conform to the abstraction it’s supposed to, then the implementation is wrong. If the implementation conforms perfectly but doesn’t have the desired effect, then you picked the wrong abstraction. Joel picks an easy target—optimistic abstractions that guarentee that everything is fast and nothing ever goes wrong—and then goes on to show that sometimes those are not the right abstractions to use. Well, duh.

He then claims that the C++ string abstraction leaks:

C++ string classes are supposed to let you pretend that strings are first-class data. They try to abstract away the fact that strings are hard and let you act as if they were as easy as integers.

What’s really going on here is that C++ does not define the abstraction that Joel wishes it did. The C++ implementation and specification do not conform to the string abstraction in his head. If you write your code assuming that strings are first-class you’ll run into trouble, but that would be your fault—not the abstraction’s.

Joel’s conclusion, “The Law of Leaky Abstractions is dragging us down,” is a tautology. It’s like saying “The bugs in our software are the reasons it doesn’t work right.”

Comments RSS · Twitter

Leave a Comment