The single responsibility principle


”A class should have one, and only one, reason to change.” – Robert C Martin

If I had to choose one design principle that has been most useful to practice over time it surely is the single responsibility principle (SRP).

What is this principle about?

The idea is that a module (or class) should only have one reason to change, i.e. it should only have one responsibility. As stated by Robert C Martin it is a simple principle, yet one of the hardest to get right. This means that a module should not be responsible for both business logic and database interaction or displaying things on the screen. Each of these areas should be handled by a separate module.

Even if this principle is often refereed to in a object oriented design context it is fully applicable to non-object oriented contexts as well, such as designing a module written in C.

Regardless of your programming paradigm separation of concerns in to modules with single responsibilities will make your code easier to read, understand and maintain.