Building at a Single Level of Abstraction

Learn how to tackle different levels of abstractions in code.

Handling abstractions

As we’re building the quiz, we’ll continue to build single-purpose functions that are easy to compose. One of the things that makes code easy or hard to read is the number of abstractions a programmer has to deal with at once. In programming, abstraction is just a way to simplify a complex idea or action. It turns out that we can handle many different abstractions if those abstractions are:

  • Well named

  • Well organized

  • Close together

This concept is the single level of abstraction principle introduced by Bob Martin.

Choosing a random question

The single level of abstraction principle says that each line of a function or method should be at the same level of abstraction. It’s a tough principle to articulate, but you’ll generally know it when you see it. A good example of that principle is ourselect_question function. We’ll open up /lib/mastery/core/quiz.ex and key this in:

Get hands-on with 1400+ tech skills courses.