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:
- read in a file
- parse the JSON
- find all the numbers in the resulting object
- add 1 to each of those numbers
- serializes the object back into a JSON string
- writes a string to a file
- wire all these pieces together
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.