codeguy / php-the-right-way

An easy-to-read, quick reference for PHP best practices, accepted coding standards, and links to authoritative tutorials around the Web

Home Page:https://www.phptherightway.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Single Responsibility Principle definition

cGuille opened this issue · comments

Hello!

I have read the SRP definition on the website. As a reminder, here is how it is currently described:

#### Single Responsibility Principle
The Single Responsibility Principle is about actors and high-level architecture. It states that “A class should have
only one reason to change.” This means that every class should _only_ have responsibility over a single part of the
functionality provided by the software. The largest benefit of this approach is that it enables improved code
_reusability_. By designing our class to do just one thing, we can use (or re-use) it in any other program without
changing it.

I remembered reading a blog post a few weeks ago about how the idea that SRP meant "one class should only do one thing" was a misunderstanding of the principle.

Instead, the original meaning of this principle would be that only one "actor" should be able to cause a change in a given module (here a class), implying that if the module tries to address multiple business needs at the same time, those different business needs might end up conflicting and will make later changes harder.

So I don't think the SRP is about code reusability, I think it is about maintainability and ease of following business changes.

Here are a few posts I found about it (I cannot remember the post I originally read about this misunderstanding):

What do you think about this? Should we change the description of this principle on PHP the right way?

I second this. SRP basically means that things which do not always change together (i.e. for different reasons) MUST NOT live together.