3

I thought I understood what abstraction meant - refactoring code so that it applies to more general use cases. However I have recently learned that some types of abstraction may actually be indirection, and am questioning my understanding.

I'd like to see some a specific example (of code in Javascript - not a description) which illustrates the difference between the two concepts.

I'm specifying Javascript because I only really know JS well (Though Python is similar enough that I could probably follow along.)

drenl
  • 167
  • 4
  • Have a look at https://softwareengineering.stackexchange.com/questions/111756/what-is-the-difference-between-layer-of-abstraction-and-level-of-indirection/113310. It deals with the two terms generally, rather than specifically related to JavaScript, so isn't a duplicate, but it helps explain why your question is difficult to answer. There are a bunch of good answers there. And they differ significantly. This is because there is no strict definition of the terms. There is no right and wrong on this issue. – David Arno Oct 04 '19 at 07:49
  • Indirection is one way to achieve abstraction, e.g. by creating a wrapper over some lower-level logic. – JacquesB Oct 04 '19 at 09:42

2 Answers2

1

Strictly speaking, indirection is the capability to indirectly refer to something - via a name, or an address, or an index, or whatever (what's direct and what's indirect may be context-dependent). Some forms of indirection are built into the language; e.g., when you pass an object to a JavaScript function, and call a method on it, you are not actually calling the method directly, because that object could be anything, so there are several candidate implementations (and the method could even be inherited, so it may not be "on" the object at all). You are actually specifying a name (indirection), and the language provides a mechanism to examine the object and find the function (see this).

You can also use indirection "manually"; e.g., via an array index, or a dictionary key, etc.

The statement "some types of abstraction may actually be indirection" doesn't necessarily make sense under scrutiny. There are different kinds of abstraction, but in very general terms, an abstraction is a thing that you place between two interacting components, and make both of those components depend on that abstraction, so that they can vary independently. (Usually, you'll view one of those components as a client of the other, and therefore say that the object you introduced is an abstraction of that other component; but really, the abstraction captures the essential features of the interaction between them). In OOP, this is most often an interface (captures the "I need these behaviors" - "I provide these behaviors" relationship), but it doesn't have to be. In JavaScript, it can be an implicit thing that only exists in documentation (e.g., a parameter to some method must be an object that implements some set of properties and functions - that's an interface requirement, and that very requirement is the abstraction here, even though it's not explicitly expressed in code). The fact that an abstraction can itself provide means for indirection doesn't make it "not an abstraction".

Filip Milovanović
  • 8,428
  • 1
  • 13
  • 20
  • Could we say that abstraction is the abstract concept and indirection a mean to indirectly implement it ? – Christophe Oct 04 '19 at 07:43
  • You could say that. In practice, it's not too much of a stretch to conflate the two. The difference is more of a pedantic one - to reuse David Arno's AB&C labels: "abstraction" emphasizes that there is a more general representation C of some more concrete concept, "indirection" emphasizes that there's some of a label C that refers to something else (via some mapping mechanism - memory address maps to a memory location, index maps to an array element, etc.). BTW: It would help if you shared where you read about this distinction, maybe they meant something specific. – Filip Milovanović Oct 04 '19 at 11:10
  • @Christophe: Sorry about the last bit, confused you with the OP. – Filip Milovanović Oct 04 '19 at 12:32
1

In theory, the difference between the terms is easy to define:

An abstraction exists between two things (A and B) if there is a third thing (C) between them. A and B are abstracted from each other by C.

An indirection (or dereference) occurs if a thing, A is not directly referenced by another thing, B but is instead indirectly referenced through thing C.

Notice any similarity between them? When expressed in such loose terms, they are synonymous. So it's easy to define the difference: there is none. But folk like to have them mean different things and that's where the problems arise.

Indirection is a term that's been used since the dawn of CPU instruction sets (or at least since the Z80 CPU in the early 80's, which is where I first came across it) to refer to using registers to specify an address, rather than the absolute address itself. The term is also commonly used for offsets, such as an index into an array, where the address of the start of the array plus the index, give the absolute address of the element.

But both of these are abstractions too though. We are abstracting the address, rather than directly accessing it. But it's unusual to find people referring to these as abstractions.

Abstraction is a term commonly used in OOP to refer to interfaces and abstract types. Here we are putting an abstraction layer (in the form of an abstract type) between the caller and callee. But that also means we are indirectly referencing the callee; we have an indirection. Again, it's unusual to find people referring to this as indirection though (except in the case of inheritance, where that abstraction is often called indirection).

So in summary, abstraction and indirection are fundamentally the same thing. Folk like to use the terms as if they were different, but because they aren't really different, you'll come across many conflicting definitions of the difference.

So you have a choice: view them as the same thing, or pick a definition of the terms that seeks to differentiate them and stick with that. Either way, just be aware that other folk may have picked a different meaning of the terms to you and so confusion may occur when you try to discuss the terms.

One final point, as you refer to JavaScript. If within the JavaScript community, there exists a consensus on strict definitions of these terms, then when using JavaScript stick to those. I don't believe there is such a consensus, but that may be due to ignorance on my part. The fact you ask this question lends weight to there being a lack of consensus though.

David Arno
  • 38,972
  • 9
  • 88
  • 121
  • Isnt’t there an abstraction when you realize that B is a special case of a more general A, without necessarily having a third thing ? I would not agree that indirection is the same thing. There are uses of indirection that are not related to abstraction. Indirection is only a mean to implement abstraction. – Christophe Oct 04 '19 at 11:20
  • @Christophe, yes in general, but we are talking about the abstraction as it occurs in programming, and in that context there is always (or there's an expectation of) some third thing that calls, or derives from, or otherwise relies on that abstraction (the third thing may or may not be written by you). There's a slight confusion because the word "abstraction" is used in two different ways: (1) the process of generalization, and (2) the end-result of that process, the in-between-thing C that models the abstract idea. BTW, I replied to your comment on my own answer, but forgot to tag you. – Filip Milovanović Oct 04 '19 at 12:30