57

I am not sure if both terms can be used interchangeably. Maybe there is some academic distinction in computer science which is not relevant for day-to-day programming? Or can I use both term interchangeably without being wrong? Maybe it depends on the context in which I use both terms?

Edit: On reason why I find both terms possibly interchangeable is a Wikipedia entry about Abstraction layer. There you can find David Wheelers quote 'All problems in computer science can be solved by another level of indirection.'

3
  • 8
    ...except the problem of too many levels of indirection! Commented Sep 30, 2011 at 21:44
  • @MasonWheeler: You could add another, less indirect level of indirection…
    – Jon Purdy
    Commented Oct 1, 2011 at 1:07
  • C++ allows you to add a layer of abstraction without a layer of indirection, so they can't mean the same. Commented Oct 1, 2011 at 18:23

8 Answers 8

52
+50

Abstraction deals with simplification, indirection deals with location.

  • Abstraction is a mechanism that "hides" complicated details of a object in terms of simpler, easier to manipulate terms. In programming, a good example is the difference in details between machine code and the various tools for creating applications that are ultimately based on machine code. Consider creating a Windows Form application with the Visual Studio IDE. The IDE lets you think of the application in terms of easy-to-manipulate items in a What-You-See-Is-What-You-Get manner. The position of a screen widget is abstracted out to a visual location in a frame which you can change by dragging the widget around. Internally, the IDE manipulates the widget using another layer of abstraction such as a high level language (such as C#). C# itself is not manipulated using machine code, it is manipulated using a "Common Runtime Environment" which itself is an abstraction of a computer and operating system.

  • Indirection refers to making the location of an item transparent. If you know a web resource's URI, you can access the resource without knowing its precise location. You do not access the resource directly, instead you access through a channel that passes your request through a series of servers, applications and routers. Indirection may be considered to be a special type of abstraction where the location is abstracted.

1
  • 2
    +1 best answer so far ! Was about to write one but this one is close enough to what I had in mind.
    – Newtopian
    Commented Oct 10, 2011 at 2:06
29

Abstractions are implemented using indirection.

For example, virtual memory: The abstraction is a contiguous address space entirely at your disposal. This abstraction is implemented using indirection via a page table. Instead of directly accessing physical memory addresses, they are translated from a virtual address to a physical address.

To add a layer of abstraction, you have to add a layer of indirection. But adding indirection doesn't necessarily give you an abstraction. For example, having getters and setters on every single variable is a layer of indirection, but if all they do is get and set simple values there is no abstraction.

8
  • 3
    In fact, the last example is not very good. Plain getters and setters do add abstraction. The only one knowing, they are plain, is the class declaring them. If you decide to change the implementation, any code built against the abstraction is not affected.
    – back2dos
    Commented Oct 5, 2011 at 14:08
  • can you give any better example which is indirection but not abastraction ?
    – Mr Coder
    Commented Oct 6, 2011 at 11:12
  • 1
    definitely a wrong example
    – Morg.
    Commented Oct 6, 2011 at 11:43
  • 4
    Who said anything about classes? I also said nothing about changing the visibility of a variable. Nothing is stopping you from accessing it directly; it's a variable. But you can also do it with a level of indirection via the getter and setter. Since the semantics of using the getter and setter are the same as accessing the variable directly, there is no abstraction.
    – Austin
    Commented Oct 6, 2011 at 15:20
  • 2
    Except that the getter and setter don't necessarily 'just' access the variable. If you are using a getter and setter you are adding an abstraction as you could change them to use a different variable without the user being aware of it, therefore an abstraction. Commented Oct 7, 2011 at 3:30
11

First, lets try proper definitions for the terms:

  1. Layer of abstraction means:

     a) there is large number of positions which use abstraction
          (layer = all the positions together)
     b) each position is hiding some complex stuff, but allows invoking it 
          using only simple code. 
         (abstraction == one-to-one mapping between simple code and complex code)
    
  2. Level of indirection on the other hand means:

      a) you're counting levels
      b) indirection==there are several steps before you can reach or access the data
      c) level of indirection is just how many steps it takes to access the data
    

Both of these things can mean the same thing, as long as you use:

  a) step = going from simple code to complex code. 
8

Layer of Abstraction and Level of Indirection are distinct concepts. Abstraction is the aggregation and meaningful naming of a number of elements such as pieces of data or program instructions, for instance the concept of a file or method call, whereas indirection is the decoupling of entities to facilitate the postponement of the realisation of their relationship, for example, the use of JNDI to separate the identification of a resource within a program from the actual resource which may eventually be provided by an application container.

Frequently the concepts go hand in hand and which one applies to a particular construct depends on what exercise or discussion is in progress. For instance, the abstract nature of an interface is important when learning or documenting an API; its property of indirection is important when adding extensibility to or creating tests for an application.

A layer of abstraction is the aggregation of abstractions and giving them a conceptual integrity and consistency of usage. CreateProcess is the win32 API name for a bunch of code that builds and executes a process. The "name" is significant to this context because if we called the function something like DoAllocMemThenMakeEnvThenFindEntryPoint... it really would not be very abstract. A layer such as the Win32 API provides a barrier across which a programmer may be advised not to venture. It removes complexity from the caller's viewpoint at the cost of reduced power (flexibility, performance etc). This trade-off is highlighted by frequent discussion of leaky abstractions: we may still need to make direct SQL calls when using Hibernate or make Win32 calls when using .NET.

Regarding indirection, most non-trivial programs operate with some form of user coded indirection, witness COBOL's INPUT-OUTPUT section from before the ark. When accessing a resource such as a database we may see the embedding of a JDBC connection string in the code as Level 0 indirection, a JNDI connection (which delegates the choice of resource to an application container) as Level 1 and some Spring construct that maps the application JNDI identifier to one of many container resources as Level 2. Multiple levels allow a number of parties external to the relationship (in this case a relationship between executing code and a database) to manipulate that relationship. This applies equally to internal program components such as interfaces and events.

We see that, no matter what their other qualities, abstraction reduces complexity whereas indirection increases it. Abstraction reduces power whereas indirection increases it. Indirection can be used to restore the power of abstraction by allowing default behaviour to be overridden by custom callbacks.

7

My understanding is that abstraction mostly refers to functions and indirection mostly refers to data. In other words, the level of abstraction is how deep your stack trace gets, and the level of indirection is how many pointers you have to dereference. At least that's how I use the terms.

1
  • with virtual functions being modelled as pointers to functions?
    – Caleth
    Commented Sep 8, 2017 at 8:56
2

A pointer to a pointer to a pointer to a pointer to a pointer to a pointer to int has six indirection levels, but no layers of abstraction whatsoever.

1

A good example of abstraction is calling a single method to store an item in a database. The method abstracts the details of connecting and calling the database. Where an example of indirection is using a struct to access interrupts. You are still accessing the interrupt when you set values in the struct. it is just indirectly through the struct by giving struct member names to specific points in memory.

So abstraction hides details of implementation where indirection simply provides an "indirect" interface through which to access something.

Abstraction is when you do not need to understand what is being hidden, where with indirection you do.

-1

Adding a level (or layer) of abstraction and adding a level of indirection are the simply two ways of saying the same thing. When you solve a problem, you usually try a direct solution. Sometimes that isn't possible, so you try an indirect solution. This requires introducing some abstraction to simplify the problem - as it is because the problem is complex that it can't be solved directly. Having solved the problem by the indirect approach, there is no reason not to consider solving the problem again, but more generally; This will involve introducing another, higher level of abstraction. And this new, more general solution is even more indirect than the original indirect solution - ie so that another level of indirection has been introduced.

2
  • 3
    this doesn't seem to add anything substantial over points made and explained in prior 7 answers
    – gnat
    Commented Sep 8, 2017 at 4:37
  • Thanks, Gnat. I take your point, and its a pity this interesting question attracts a lot of spam!
    – ChrisC
    Commented Sep 8, 2017 at 19:25

Not the answer you're looking for? Browse other questions tagged or ask your own question.