DEV Community

Cover image for SLAP: Single Level of Abstraction Principle

Posted on • Originally published at on

SLAP: Single Level of Abstraction Principle

Even thought this acronym is quite catchy, SLAP is the one principle that you don’t find many people talking about.

Single Level of Abstraction

In essence, the principle proposes that each block of code should not mix what the code does with how it does it. Another way of thinking about it is, whenever possible, the code should describe in steps the actions that will take and then each step can elaborate on that.

For example:

this class has one method that showcases both what it does and how it does it.

If we want to SLAP it we need to delegate the how of each step to its own method:

here the invoke method simply describes what will happen upon its invocation. A new task will be created, then saved and finally passed to any observers.

For knowing how each step gets implemented we need to drill down one level. For example, creating a new task requires us to normalize the provided description and then create the task. For knowing how the normalization gets implemented we yet again move one level deeper!


Keep hiding how something gets implemented in new methods until you can no longer avoid it!

Top comments (0)