Welcome Guest, Not a member yet? Register   Sign In
How many lines average do your controllers run ?
#1

[eluser]internut[/eluser]
Hey All,

After re-working thing and making things the correct (i hope) MVC way I'm finding my controllers between 60 - 110 lines of code.

This is with line spaces to make things easy to read.

I do have which I probably will remove validation in some controllers which would shrink them.

Just curious as to others line counts. Of course this varies depending upon what the controller is doing but just thought I'd ask. Just curious.

And I mean average... 50 - 100...... 200..... 300... not specifics.
#2

[eluser]TheFuzzy0ne[/eluser]
A very bizarre question... I can't give you an answer because it varies so much. I could have a single controller method, or I could have twenty. It really depends.
#3

[eluser]internut[/eluser]
Yeah guess its an odd question that has no answer... If you're keeping things individual and not putting 20 functions into a single controller which I just got away from I was at 1k lines of code at times.

Being able to read it and find quickly where I wanted to alter things was not easy.

So splitting things up I'm down to around that 60 - 110. Which makes things so much easier to read and know whats going on.

Maybe I'm wrong but I thought I read a post about what a controller should do and read a post about if a controller is 100+ lines of code it gets hectic to read into.
#4

[eluser]jedd[/eluser]
My question on this subject:

How do you guys reduce the size of your controller? The only way I could see, other than modifying the design such that you have more controllers doing less things, is to have methods in a sub-directory, relatively pathed in require() calls, and that require() call is pretty much the only thing in the function in the (main) controller file. I can see one prima facie benefit - code only gets read in when it's definitely going to be used.

I suppose it comes down to design approach, coding style and self-discipline as to whether not being able to see the contents of each method easily while surfing through your controller would be a hindrance or a benefit. With a decent IDE (I use Quanta and it *mostly* does this elegantly) you have a document-structure tree to help you navigate around - take you straight to method names f.e., and that might affect your decision.

OTOH, to answer the question, extrapolating from what I have here, I'd say an average of around 600 lines. But my code contains a heck of a lot of white space. My average would work out to be about 16KB - very rough estimate. I suspect this equates to maybe 2 or 4 reads (at a file system level).

Plus I'd expect it'd be impossible to measurably identify any performance delta between 600 lines and 100 lines of code, even with the above require() style approach to each method's guts, given the way PHP is optimised and considering the rest of the disk activity kicked off by a page hit.

Mind, I can only comment on jedd practice, not best practice. Wink
#5

[eluser]jdfwarrior[/eluser]
Using models should reduce the amount of code in the controller a good bit by itself. But just like others are saying, its hard to give an accurate number on that, or an average/estimate due to varying size of controllers, as a result of projects requiring different amounts of code and functionality. That and it depends on how well you separate functionality with the controllers. You could have one controller that handles ALOT or you can have controllers for each type of operation that would exist.
#6

[eluser]TheFuzzy0ne[/eluser]
If you have a lot of code in a single controller method, it might be worth seeing if there are any blocks of code you can export to a private function. It will make your controller method easier to follow, and you will essentially have alias to read, instead of having to look at the code itself.
Code:
if ( ! $this->session->userdata('logged_in'))
{
    $this->_show_login_page();
}
else
{
    $this->_refresh_userdata();
}

Let's say for example _refresh_userdata() happens to be 40 lines of code, and _show_login_page() is another 20, that's already 60 lines of code you've managed to get out of the main method. Also, without looking at the underlying code, it's quite clear what's happening in the code.

Hope this helps.
#7

[eluser]drewbee[/eluser]
Agreed with fuzzy on this one. I have some controllers that have 50 lines, others that have 1000-2000. Just depends on how many methods (pages) I have for each controller.
#8

[eluser]Evil Wizard[/eluser]
do you mean lines of code? or number of lines?

I believe in the 60/40 rule, 60% code and 40% comments, and in breaking code blocks down into manageable sizes utilising libraries and models when possible.
#9

[eluser]rogierb[/eluser]
That is impossible to answer. One of our project has 100+ controllers ranging from 100 to almost 10.000 lines.

Another more recent project has 7 lines of code per method per controller.

It all depends on the size of your project and you way of organizing things
#10

[eluser]internut[/eluser]
I did have a "users" controller which had everything you could possibly do to a users record in one controller.

It goes to be over 1,000 lines of code and was just bothering me.

So I split it up to usage like:

Controllers:

add_user.php
edit_user.php
delete_user.php
users_lists.php
users_groups.php
useres_group_exp.php

Having all them in on "users.php" controller just got out of hand. So breaking them up add using
modules for DB calls really broke things up and makes it easier down the road to fix or add things.

And I know there is no "correct" answer to number of lines so forgive me for the initial post wording.

Was just getting a feel for if some actually have controllers that are very large as I was finding it hard to manage. Well not hard just not as easy.




Theme © iAndrew 2016 - Forum software by © MyBB