Can you in "plain English" explain why we need Single Responsibility Principle?
Especially that it moves the what I call "bloat" into other places. See below for more info. By bloat I mean in my specific case, I have multiple responsiblities in a Controller that exhibit themselves as functions/methods. By applying SRP in the way that I understand, breaks up the Controller into several Controllers, each with one method. The "bloat" is therefore moved from "number of methods" into "number of files".
The article in the wiki link above explains what SRP is, but it does not seem to tell me "why" we use it. I'd like a not-so-technical (or maybe even a technical) reason/explanation/sense as to why we need this principle.
Because in my current experience, implementing SRP, leads to smaller, more narrowly-aligned code, but it creates more files.
Example
I have a controller file that can have many various actions/methods inside of it. To keep all those actions in the controller, I have to bloat the number of dependencies I have to pass to the controller, because dependencies must cover all possible actions, even if any one particular action does not need all of the dependencies.
so I can break up the controller into 2 or more pieces to where I have one action per controller. This satisfies the SRP, but it bloats the number of files I have to create.
Example
/*
* Repository initialized with $quoteId
*/
class Repository
{
private $quoteId;
function __construct($quoteId)
{
$this->quoteId = $quoteId;
}
}
/*
* Controller initialized with $repository
*/
class Controller
{
private $repository;
function __construct($repository)
{
$this->repository = $repository;
}
function addForm()
{
//repository is initialized elsewhere with $quoteId
$this->repository->getFormDataFromQuote();
};
function viewForm()
{
$id = int_val($_GET['id']);
//does *not* use $quoteID
$this->repository->getViewDataFromId($id);
};
}
SRP
To abide SRP we can break up Controller into two, one for addForm, one for viewForm. We then can break up the Repository into two as well, one for each method in Controller. Thus, we started with 2 files, we will end up with 4 files.
Drawbacks
I interpret SRP here as "Break up the controller" (and, I presume any further supporting files as well, such as Repository here) into two in this case. Thus in the above example, there will be ControllerAddForm
and ControllerViewForm
, and if I am using a repository for those methods, which I am, I have to create RepositoryViewForm
, and RepositoryAddForm
to satisfy SRP as well, because different repository methods are required for the different actions of the Controller. Thus, I get 2x file bloat. Why again is SRP recommended despite moving the bloat into the number of files instead of into the number of methods per file.