Beyond the Single-Responsibility Principle

The most famous of the SOLID principles and how it can change your mind set.

Nikita Lazarev-Zubov
Level Up Coding
Published in
7 min readJan 12, 2021

--

When it comes to discussing the SOLID principles the first one that usually comes to mind is the Single-Responsibility Principle, or just SRP, the first letter of the acronym. It’s become so obvious, that no one really ponder it anymore, its importance is just taken for granted nowadays.

Here’s its original expression by Robert C. Martin (a.k.a Uncle Bob:)

A class should have only one reason to change.

Photo by call me hangry 🇫🇷 on Unsplash

In my early programming days I was so impressed by this simple idea, that it became my working mind set. Now it means for me much more than just “write small, testable classes”. I believe it can be applied to literally any aspect of the software development. I’ll try to show you.

Code

Variables

Let’s start with something small and simple. This is an adapted example from the Steve McConnell’s Code Complete:

(All code snippets are given in Swift.)

import Foundation

// Qadratic equation: a * x^2 + b * x + c + 0
let a = 1.0
let b = -8.0
let c = 12.0

var temp = sqrt(pow(b, 2.0) - 4.0 * a * c) // Discriminant.
var root1 = (-b + temp) / (2.0…

--

--

Swift and general programming topics, Agile software development, soft skills