Abstraction Layers in Programming: an Overview

This article explains abstraction layers, which you can use in many programming domains. Let’s get started.

What is an abstraction layer?

The abstraction layer creates a separation between two things. For programming, this is often splitting tasks into separate entities.

For example, an entity A might be assigned the task of fetching an image from a database and then processing the image when retrieved

Creating an abstraction layer will split this entity A into entities A and B, where:

  • Entity A fetches the resource.
  • Entity B—now the abstraction—depends on the image to return from the first operation to then perform its second operation.

The abstraction layer allows one party, or operation, to be entirely responsible for one task while a dependent waits for that party to return data for them to process.

Abstraction in code

APIs are abstraction layers. While APIs serve many purposes, the way they are used is this: a developer is trying to create an application involving tweets, so they tap into Twitter’s new APIv2 to get tweet data.

The developer is not responsible for creating a service that allows users to speak with one another via 140-character messages. The Twitter service is operated by Twitter, and all responsibility for collecting their data rests on their shoulders.

The developer just has to to get the data and use it for their purposes. Because the developer is not responsible for getting the data and providing the Twitter service at their level of operability, the Twitter API is abstracted away from the developer’s operation.

In our case, abstraction can also be called indirection. In a famous quote by David Wheeler,

“All problems in computer science can be solved by another level of indirection.”

Creating an abstraction is as simple as changing one function into two. This Python function opens a text document and counts the number of lines, the number of words, and the number of characters it has.

def processDoc(filename):
with open(filename,'r') as f
number_of_lines = 0
number_of_words = 0
number_of_characters = 0
for line in f:
line = line.strip("\n")
words = line.split()
number_of_lines += 1
number_of_words += len(words)
number_of_characters += len(line)
return number_of_lines, number_of_words, number_of_characters

To create an abstraction layer from this function, the “counting” portion of the function can get abstracted away from the “opening” portion of the function. One function is not responsible for both; two functions take responsibility for the process:

  • The openDoc function will open the document and return the document as a string that the computer can use.
  • The processDoc function takes as input, the string output from the openDoc function, and runs some counting scripts and returns their values.

To the original function, the step that processes the texts is the abstraction layer—it is one step removed from the original task. Using the appropriate nomenclature, it has been abstracted away from the original function.

def openDoc(filename):
with open(filename,'r') as f
text = f.readlines
return text
def processDoc(text):
number_of_lines = 0
number_of_words = 0
number_of_characters = 0
for line in text:
line = line.strip("\n")
words = line.split()
number_of_lines += 1
number_of_words += len(words)
number_of_characters += len(line)
return number_of_lines, number_of_words, number_of_characters
Sarah Jenkins

Sarah Jenkins

Senior Technology Editor & AI Specialist

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.

Share this article