Dictionary: “The process of considering something independently of its associations or attributes”

Thesaurus: "Detachment, remoteness"

Wikipedia: “It works by establishing a level of complexity on which a person interacts with the system, suppressing the more complex details below the current level"

I also found a quote that gives a description of abstraction far more eloquently than I could:

"The essence of abstractions is preserving information that is relevant in a given context, and forgetting information that is irrelevant in that context." (John V. Guttag)

So a quick example...

We have a cake service which handles the details of making a cake:

public class CakeService
    public void MakeCake()

We have a Baker class that utilises the CakeService to get cakes:

public class Baker
    public void Bake()
        CakeService cakeService = new CakeService();

Here the Baker class does a spot of baking and uses the CakeService. The baker has no concept of what is required to actually make the cake, the complexity is all abstracted away into the CakeService.

What’s the benefit?

The baker class is kept simple by not needing to know anything about actually making a cake. If it then needed to Flambé or make fish and chips, we’d keep all of that outside of the baker class, leaving it nice and simple.

A further point to note here is that if you see a class getting large, say 200+ lines then you should consider if there is too much going on and if some of that detail should be abstracted away into another class. Doing so may make for improved readability, testability and remove complexity.

Got a comment or correction (I’m not perfect) for this post? Please leave a comment below.
You've successfully subscribed to Gavin Johnson-Lynn!

My Pluralsight Courses: (Get a free Pluaralsight trial)

API Security with the OWASP API Security Top 10

OWASP Top 10: What's New

OWASP Top 10: API Security Playbook

Secure Coding with OWASP in ASP.Net Core 6

Secure Coding: Broken Access Control

Python Secure Coding Playbook