Abstractions in Software Are Like Boxes

Nikhil Saraf
The Startup
Published in
4 min readJul 16, 2020
An Abstraction is like a neat and clean box — Photo by Brandable Box on Unsplash

Software projects start off small. As the code grows it becomes complex and eventually spirals out of control.

Good software developers focus on writing simple code. As the codebase grows they refactor the code to keep it simple. This makes things easy to reason about, debug, and grow.

One of the most important techniques to make software simple is an abstraction.

An abstraction is a simplification of a much larger concept into something that is easier to understand. An abstraction allows you to focus on the big picture by hiding the inner workings.

An Example From The Real World

Let’s say you live in a place with four walls, a floor, roof, plumbing, electrical piping, water tank, bell, kitchen, bed, lights, fans, desks, television, etc. This is what we call a “home”.

Let’s use this in a simple sentence:

I am going home.

Let’s say, instead of using the word “home” you use the full description from above. The sentence would now become:

I am going to the place where I live that has four walls, a floor, roof, plumbing, electrical piping, water tank, bell, kitchen, bed, lights, fans, desks, television, etc.

This would get cumbersome for you and your friends very quickly. It’s a separate issue that you may not be left with many friends after this! There’s also no reason to do this in reality since it is simpler to use the word “home”. Everyone already understands what a “home” is.

In the example above, “Home” is an abstraction for the concept of a place where you live.

You don’t need to describe the home in full detail every time you discuss it. The basic expectation from a “home” is that you can live inside it, which is a simple concept to understand.

How To Apply Abstractions in Software

Let’s say we have a function to get an address book (phone book):

sample code that gets an address book represented by a map

The function gives us a direct mapping from a person’s name to a number. This seems straightforward and harmless but it is not. It has made the software more complex and hard to maintain!

All developers who use this function need to be aware of its internal representation as a Map<String, String>. When they think about an address book they will always see it only as a Map, but an address book can be so much more!

What happens when a person gets a second phone number?

Using a Map<String, String> to represent an address book is like giving someone a box with the insides exposed. It does not look clean and they have to take extra care of it.

A messy box representing messy code — Photo by RoseBox رز باکس on Unsplash

In reality, it is much better to model this AddressBook (or phone book) as a class, so it looks like a well packaged box. When you hand it to someone you should package it neatly so it is easy to handle.

A cleanly packaged box is easy to handle — Photo by RoseBox رز باکس on Unsplash

Here is an example of how we can convert the above code into a clean abstraction of an AddressBook class:

sample code demonstrating the principle of Abstraction

This is a lot of code — 20 lines instead of 7 lines (excluding comments). Yet, this is a much better model for an address book. We have created a good abstraction.

Where is our Abstraction? — it’s the “AddressBookclass that we created, of course!

Exactly like our example above about a “Home”, an “AddressBook” is an Abstraction. Our codebase now understands the concept of an “AddressBook”, what it can do, and how we can create a new one. Our code can now use this new “word” that we created in it’s conversations.

We should avoid using a Map<String, String> everywhere and instead use an “AddressBook”.

To add many phone numbers for each person we can update the internal representation in the AddressBook class. We can have a new function on the AddressBook class called getFirst(…). We can add complex logic inside AddressBook to handle a daytime and nighttime numbers. The AddressBook can get data from a database or a file and yet the caller does not need worry about these inner workings. All that the callers care about is that it can perform a get(…) call to fetch data using an individual’s name.

Last Comments on Abstractions

Using abstractions reduces the cognitive overhead of developers. This allows them to focus on the big picture, enabling them to be more creative.

Abstractions need to be well-tested because developers depend on the functionality to be working as advertised.

Many programming languages give you the ability to create interfaces, classes, or structs. These can be confused with the concept of an abstraction. An abstraction is a general software engineering concept. Interfaces, classes, and structs are features of a given programming language that let you create abstractions. In a way you can say that an abstraction is an abstract concept materialized by interfaces, classes, and structs. There’s something meta for you!

The above examples of a home and address book are very simple. As your codebase gets bigger you will need to create larger abstractions to maintain the simplicity. Start from the smallest parts of your codebase to make it easy to create these larger abstractions.

--

--

Nikhil Saraf
The Startup

CTO @ stealth startup; ex-Engineer @ Stellar XLM (2017–2022); ex-VC