Critical Concept: Abstraction

Abstraction may well be the single most important concept in computer science, and certainly the most fundamental. Unless you want to write a lot of 1s and 0s with magnets.

Abstraction is probably the single most important concept in computer science. In fact, if anything, computer science as a field is simply a stack of abstractions.

An abstraction is defined as something that sits on top of something more complex and makes it more simple for the user. When you think about it, at some point the code you write executes zeroes and ones inside logic gates in a CPU. But there’s not exactly a JavaScript function for that. The way that works is that you instruct something that instructs something else that instructs something… and so on. Each level of this is more and more complex and computery.

The benefit to us is that we only really have to learn the level that we ourselves work on. As PHP programmers, for example, we don’t have to learn C, even though PHP was originally built over the top of it. Much of web development lies in the usage of good abstractions.

var xhr = new XMLHttpRequest();'POST', 'api/v1/user', true);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.onreadystatechange = function() {
if(xhr.readyState == XMLHttpRequest.DONE && xhr.status == 200) {

This is wordy and not particularly intuitive to use. Thankfully, jQuery does a nice job of cleaning up this syntax with its .ajax method.

url: 'api/v1/user',
method: 'POST',
data: newUser
}).done(function (response) {

$.ajax has a number of even higher abstractions, though, which hide the details of even that.

$.post('api/v1/user', newUser, function(response){

Another example would be database access. The “standard” way to connect a PHP application to a database is mysql_connect, and a bunch of related functions, like mysql_query. More recent versions of PHP have deprecated these functions, replacing them with mysqli_query, etc. MySQL Improved. For many people this straight replacement was good enough. But there’s a better solution.

PDO is a “database abstraction layer” for database access.

$query = 'SELECT, p.type, p.breed, p.age, AS owner
FROM p LEFT JOIN owner AS o ON = p.owner_id
$pets = $pdo->query($query);

Note there’s nothing in here that links directly to mysql. Could be mysql, could be postgres, or sqlite. Could change on the fly. This is an abstraction. The implementation details — the mysqlness — has been hidden by the abstraction layer. It’s set once in the connection and then irrelevant. This fact, this abstraction, would be enough of a reason to use PDO over mysqli on its own, though there are other benefis.

PDO is a step in the right direction, but not a big step. We can go a lot further with something like an ORM. An ORM will define objects and the relationships between them. The Pet object has definitions in it of how it is related to Owner. And vice versa. This means you can do things like this Laravel Eloquent example instead.

$pets = Pet::all()->with('Owner')->where('type', 'Dog')
$user = Owner::with('Pet')->find($id);

By storing the relationships in one place, in the object definition, we abstract out all the complexity of directly manipulating SQL strings.

Leaky Abstraction

Abstractions aren’t a perfect solution. More particularly the more complex they are, the more likely they are to have the underlying requirements and nastiness bleed into your stuff. This is termed “leaking”, or a “leaky abstraction”.

All non-trivial abstractions, to some degree, are leaky.
— Joel Spolsky

For example, the above Eloquent library gets “leaky” in that we still have to think about this as a database, a rows and joins. It’s still easily possible to make very inefficient queries if we don’t think in these contexts (or think it inefficiently). There are also times when the abstractions become insufficient. In this example, the ORM is great for getting data out, and even better for updating and saving it. But when it comes to reporting and aggregates, ORMs are typically not great. It’s not what they’re for and they often require dropping back into some sort of lower level abstraction or raw SQL.

These issues can be solved sometimes by higher abstractions, such as a data mapper ORM like Doctrine, which breaks the conceptual linkage between the database rows and the objects.

The conclusion here might well be that you shouldn’t use abstractions. This couldn’t be further from the truth. Abstractions are hugely powerful, and make our development both more rapid and more maintainable. Well defined abstractions typically have “escape hatches” that let you access needed parts of the lower levels for those rare occasions.

In the case of the Eloquent ORM used above, you can drop down to QueryBuilder, an abstraction on the actual sql string creating process, or further down to run raw sql queries on the database.

There is, therefore, no real justification to not use an abstraction like this. There are enormous benefits for the 95% case, and easy “outs” for the rare exceptions. A big part of our job as professional developers is seeking and building abstractions that are intended to make our lives easier and our code better, hiding all the icky hards. Despite the implication this article may have given, this includes your own code as well. Abstracting difficult or confusing bits out of your own applications is just as critical.




Senior Web Developer based in Bangkok, Thailand. Javascript, Web and Blockchain Developer.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Microservices Observability Design Patterns

A required class was missing while executing org.wso2.maven:wso2-esb-api-plugin:2.1.0:pom-gen:

Flutter: Login with Twitter Oauth V2.0

READ/DOWNLOAD$% Introduction to Programming with C

Gutenberg is Coming: Why the Big Fuss?

How I built a home weather station IoT application with Raspberry Pi and Google Cloud

[Code coverage & Quality]– The Duty of Developer as a Professional #4

Using Your Own Custom External Domain with AWS Cognito

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Matt Burgess

Matt Burgess

Senior Web Developer based in Bangkok, Thailand. Javascript, Web and Blockchain Developer.

More from Medium

Made with only the finest ingredients

The RESTful Services | An Evaluative Overview

Introduction to Functional Programming

Object.keys() || Object.values()