Meaning of Leaky Abstraction?

23,474

Solution 1

Here's a meatspace example:

Automobiles have abstractions for drivers. In its purest form, there's a steering wheel, accelerator and brake. This abstraction hides a lot of detail about what's under the hood: engine, cams, timing belt, spark plugs, radiator, etc.

The neat thing about this abstraction is that we can replace parts of the implementation with improved parts without retraining the user. Let's say we replace the distributor cap with electronic ignition, and we replace the fixed cam with a variable cam. These changes improve performance but the user still steers with the wheel and uses the pedals to start and stop.

It's actually quite remarkable... a 16 year old or an 80 year old can operate this complicated piece of machinery without really knowing much about how it works inside!

But there are leaks. The transmission is a small leak. In an automatic transmission you can feel the car lose power for a moment as it switches gears, whereas in CVT you feel smooth torque all the way up.

There are bigger leaks, too. If you rev the engine too fast, you may do damage to it. If the engine block is too cold, the car may not start or it may have poor performance. And if you crank the radio, headlights, and AC all at the same time, you'll see your gas mileage go down.

Solution 2

It simply means that your abstraction exposes some of the implementation details, or that you need to be aware of the implementation details when using the abstraction. The term is attributed to Joel Spolsky, circa 2002. See the wikipedia article for more information.

A classic example are network libraries that allow you to treat remote files as local. The developer using this abstraction must be aware that network problems may cause this to fail in ways that local files do not. You then need to develop code to handle specifically errors outside the abstraction that the network library provides.

Solution 3

Wikipedia has a pretty good definition for this

A leaky abstraction refers to any implemented abstraction, intended to reduce (or hide) complexity, where the underlying details are not completely hidden

Or in other words for software it's when you can observe implementation details of a feature via limitations or side effects in the program.

A quick example would be C# / VB.Net closures and their inability to capture ref / out parameters. The reason they cannot be captured is due to an implementation detail of how the lifting process occurs. This is not to say though that there is a better way of doing this.

Solution 4

Here's an example familiar to .NET developers: ASP.NET's Page class attempts to hide the details of HTTP operations, particularly the management of form data, so that developers don't have to deal with posted values (because it automatically maps form values to server controls).

But if you wander beyond the most basic usage scenarios the Page abstraction begins to leak and it becomes hard to work with pages unless you understand the class' implementation details.

One common example is dynamically adding controls to a page - the value of dynamically-added controls won't be mapped for you unless you add them at just the right time: before the underlying engine maps the incoming form values to the appropriate controls. When you have to learn that, the abstraction has leaked.

Solution 5

Well, in a way it is a purely theoretical thing, though not unimportant.

We use abstractions to make things easier to comprehend. I may operate on a string class in some language to hide the fact that I'm dealing with an ordered set of characters that are individual items. I deal with an ordered set of characters to hide the fact that I'm dealing with numbers. I deal with numbers to hide the fact that I'm dealing with 1s and 0s.

A leaky abstraction is one that doesn't hide the details its meant to hide. If call string.Length on a 5-character string in Java or .NET I could get any answer from 5 to 10, because of implementation details where what those languages call characters are really UTF-16 data-points which can represent either 1 or .5 of a character. The abstraction has leaked. Not leaking it though means that finding the length would either require more storage space (to store the real length) or change from being O(1) to O(n) (to work out what the real length is). If I care about the real answer (often you don't really) you need to work on the knowledge of what is really going on.

More debatable cases happen with cases like where a method or property lets you get in at the inner workings, whether they are abstraction leaks, or well-defined ways to move to a lower level of abstraction, can sometimes be a matter people disagree on.

Share:
23,474

Related videos on Youtube

Geonne
Author by

Geonne

Updated on September 12, 2021

Comments

  • Geonne
    Geonne over 2 years

    What does the term "Leaky Abstraction" mean? (Please explain with examples. I often have a hard time grokking a mere theory.)

    • Daniel Pryden
      Daniel Pryden over 13 years
      You might want to read Joel Spolsky's original article The Law of Leaky Abstractions which as far as I know is the origin of the term.
    • David Thornley
      David Thornley over 13 years
      Most of the answers of the proposed dupe are about fluent interfaces.
    • John Reynolds
      John Reynolds over 9 years
      When you google your way to this question 4 years later, it's difficult to guess which post used to be the 2nd highest voted one.
  • JUST MY correct OPINION
    JUST MY correct OPINION over 13 years
    <pimp>I like the Erlang approach to this better in that it doesn't try to hide the difference between a function call and sending a message to a process to the point that the two use very different syntax. And a remote process message send is very visibly different from a local process send, albeit using the same general syntax.</pimp>
  • Wowbagger and his liquid lunch
    Wowbagger and his liquid lunch almost 13 years
    Well, this is the only response that actually gives a good example (reading comprehension, folks), so it gets my +1.
  • tvanfosson
    tvanfosson over 12 years
    @mehaase I don't see how it matters whether your abstraction is leaky by design or by neglect. I've expanded the answer with an example and more info from the referenced article so that it can stand on its own. Further, I don't think that "leaky abstraction" necessarily needs to be a pejorative. To me it merely describes a situation where you, as a developer, need to be more careful when working with the abstraction. The design may be good, bad, or indifferent independent of the "leakiness."
  • Davy8
    Davy8 over 12 years
    And you work with 1's and 0's to hide the fact that you're working with electronics and physics (very late comment, I know)
  • Sebastian Patten
    Sebastian Patten almost 11 years
    Thanks for the example. No one else seemed to be able to provide a simple explanation.
  • chad
    chad almost 11 years
    This is a great answer, particularly since it demonstrates the user point of view, which is what the software version is all about.
  • brumScouse
    brumScouse almost 6 years
    What does meatspace mean? Lay person explanation?
  • brumScouse
    brumScouse almost 6 years
    Webforms had bo bottom in its bucket. Whats worse was that the thinly veiled abstractions amounted to working with Http like you were working in a glove box.
  • Wowbagger and his liquid lunch
    Wowbagger and his liquid lunch almost 6 years
    @brumScouse "meatspace" means the physical, offline world. It is used to contrast against the online, cyberspace world. I'll edit my answer to include a link to the definition.
  • alaboudi
    alaboudi almost 4 years
    I like how this post points out that "there are still leaks". It's all about minimizing them.

Related