Single Responsibility Principle

The Single Responsibility Principle says that one unit of code should only do one thing. This helps the code be more readable and easier to maintain. It is also easier to change the logic, as different tasks are decoupled from one-another and don’t interfere with each other.

As an example, if we have some code that is supposed to read in some JSON, transform with add 1 to any number in it and write it back to disk, you should have functions that:

Each of these functions has a very limited scope and is easy to reason about. This makes it easier to write the code, read the code and to test the code, which ultimately makes it easy to maintain.

1 Backlinks   backlinks