CodeIgniter Forums
Controller File Size/Length - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12)
+--- Thread: Controller File Size/Length (/showthread.php?tid=71820)



Controller File Size/Length - Poetawd - 09-27-2018

Hello All !!!

Thanks for reading this !

I am a self-taught programmer... never had a teacher or anything like it... Just foruns, books and documentations...

Shy

I have a question regarding the size of a controller file.

Is it OK for a controller to have 2000~3000 lines ? Should I divide it into 2 controllers ?

My question is about best practices, but, it is also about server resources ! Will a "big" controller consume more memory than a "small" controller?

THANK YOU ALL FOR HELPING !


RE: Controller File Size/Length - ignitedcms - 09-27-2018

It sounds to me like bad design, but I guess it could be alright. I would separate the database logic into models. Other than that depending on what it is your controller does it may be alright.


RE: Controller File Size/Length - kaitenz - 09-30-2018

I can only say is: Skinny controllers, fat models


RE: Controller File Size/Length - Pertti - 10-01-2018

I have one controller here with 15K+ lines, other than navigating the source, haven't really noticed any resource issues.

Said that, there are few things to consider.

Purely from development point of view, it's much easier to put business logic in models, in case same functionality needs to be used from multiple endpoints. Learned it hard way, we had split apps for main application and registration page, and at some point some password rules changed, which were updated on one, but not other. If we had kept that logic in model, it would have been updated on both sides.

I'd say skinny controllers fat models is very reasonable way to organise your code.


RE: Controller File Size/Length - sofwan - 10-11-2018

2000 - 3000 lines is still Okay. I think even 10,000 lines of source code is still okay as long as it applies good algorithm and MVC rule and make it the code easy to read, such as pay attention about indentations.
In addition, especially for speed, the first part to execute in Controller is construct method, if not so many libraries or class load there, for example Models, let's say under 5 models not load there, it is still okay, i think.