liveBook · Manning

Leaky abstraction

As explained in Practices of the Python Pro

A leaky abstraction is an abstraction whose underlying implementation details or concerns escape into code that should depend only on its interface.

A leaky abstraction fails to hide enough implementation detail. Although it promises a simple interface, using it still requires knowledge of what lies underneath, creating coupling to those details. section 10.2.3

Leaks commonly appear when:

  • Calling code handles package-specific errors, such as exceptions raised by requests.get. section 10.2.3
  • A high-level layer exposes low-level operations, reducing the freedom to replace or customize the implementation. section 10.2.3
  • Business logic formats user-facing messages or directly calls database operations. section 10.3

Bookmark command

In AddBookmarkCommand, db.add couples the command to a database, while return 'Bookmark added!' makes business logic responsible for presentation. Replacing the database with a JSON file or changing the user interface would require changes in this command. section 10.3

Figure 10.5. Abstractions occasionally leak the details they’re trying to hide.

Use the figure to visualize how an abstraction can expose the details it was meant to hide. section 10.2.3

How Practices of the Python Pro covers it

How deep it goes
Defines the pattern and traces it through HTTP errors, database operations, user messages, and application layers.
Examples
Uses requests.get error handling and a bookmark command that writes to a database and returns a user-facing message.
What it assumes
Assumes familiarity with abstraction, application layers, and the distinction between business logic and presentation.
What you can do after
Diagnose coupling by locating low-level operations, package-specific errors, and presentation concerns in high-level code.
How it connects
Connects abstraction to coupling, feature envy, persistence layers, business logic, presentation layers, and multitier architecture.

Also explained in

Compare how 3 books explain leaky abstraction →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Practices of the Python Pro. The section links point to the parts of the book it draws on.