Reputation: 330
I just discovered the official Symfony best practices about controllers, where it is written:
As a rule of thumb, you should follow the 5-10-20 rule, where controllers should only define 5 variables or less, contain 10 actions or less and include 20 lines of code or less in each action.
I have a question about its meaning, as it could be understood in two different ways.
"controllers" designates the controller classes (ex class MyController{}
) & "actions" designates the controller functions (ex public function indexAction(){}
) => It makes no sense to me
"controllers" designates the controller functions (ex public function indexAction(){}
) & "actions" designates instructions in these functions (ex if(){//do something}
) => It makes sense to me
In case 1, that means something like:
// Approximate maximum number of lines : 200
class MyController {
// Max 20 lines of code
public function firstAction(){}
[...]
// Max 20 lines of code
public function tenthAction(){}
}
Also, I can't get in this case what means "5 variables or less". 5 global variables? 5 local variables for all of the 10 Actions?
In case 2, that means something like:
// No maximum number of lines
class MyController {
// Max 10 actions * 20 lines by actions = 200 lines of code, and 5 local variables
public function firstAction(){}
[...]
// Max 10 actions * 20 lines by actions = 200 lines of code, and 5 local variables
public function fiftiethAction(){}
}
The goal of my question is to be absolutely sure to understand the good meaning. But I also think that it would be useful to ask the Symfony team to rewrite this sentence without leaving any ambiguities? I specially think about non-English developers like I am, or Symfony beginners.
Considering that the case 2 is the good one, the word "action(s)" could be replaced with "instruction(s)" or "directive(s)", for example?
Upvotes: 0
Views: 1571
Reputation: 12375
A Controller is a class that has actions in it. An action is a method that runs for a specific view.
Meaning 1 you suggested is correct.
What they recommend is that you keep the Controllers as simple as you can. An action method with loads of lines of code isn't as clean as an action method with only one little task to perform.
class SomeController
{
private $variablesLikeThis;
private $keepToAroundFive;
public function someAction()
{
// no more than ten of these action methods
// don't make the code in here too long (20 lines or so)
}
}
Upvotes: 3