Abstraction inversion

From Wikipedia, the free encyclopedia

In computer programming, abstraction inversion is an anti-pattern arising when the interface to a construct does not expose the functions needed by its users, although they are used within the construct. The result is that the users re-implement the required functions in terms of the interface, which in its turn uses the internal implementation of the same functions.

The term "abstraction inversion" is sometimes misunderstood as referring to complex (or concrete) constructs with simple (or abstract) interfaces, which are normal and desirable. It is also sometimes misused as an insult against a certain architecture or design.

Possible ill-effects are:

  • The user of such a re-implemented function may seriously underestimate its running-costs.
  • The user of the construct is forced to obscure his implementation with complex mechanical details.
  • Many users attempt to solve the same problem, increasing the risk of error.

[edit] Abstraction inversion in practice

Ways to avoid this anti-pattern include:

For designers of lower-level software:
  • If your system offers formally equivalent functions, choose carefully which to implement in terms of the other.
  • Do not force unnecessarily weak constructs on your users.
For implementers of higher-level software:
  • Choose your infrastructure carefully.

[edit] Examples

Alleged examples from professional programming circles include:

  • In Ada, choice of the rendezvous construct as a synchronisation primitive forced programmers to implement simpler constructs such as semaphores on the more complex basis.
  • In Applesoft BASIC, integer arithmetic was implemented on top of floating-point arithmetic, and there were no bitwise operators and no support for blitting of raster graphics (even though the language supported vector graphics on the Apple II's raster hardware). This caused games and other programs written in BASIC to run more slowly.
  • A body of opinion holds the microkernel design to be an abstraction inversion (see the links). It is interesting that microkernels are also alleged to commit the design error of oversimplifying the components so as to overcomplicate their relationships.
  • Creating an object to represent a function is cumbersome in object-oriented languages such as Java and C++, in which functions are not first-class objects. In Java, a function has to be implemented by a method of an interface, sometimes in a class with no other justification for its existence. An example from Java is the method Runnable.run(), required by the method Thread.run() . In C++ it is possible to make an object 'callable' by overloading the () operator, but it is still often necessary to implement a new class, such as the Functors in the STL.

Examples that are common outside professional programming circles include:

  • Using spreadsheet lookup functions to replicate the functionality of a database
  • Using variant data types as loop counters in Microsoft Visual Basic where an integer type is also available.

[edit] External links

In other languages