Burn Fat of Your Fatty Controller

While working on MVC design pattern, we familiarize with Separation of Concerns design principle. We separate our program into distinct sections. Each section is a set of information that do a certain function. Today we will discuss about controlling the size of our controller.

Burn fat of your controller

GarazLab : Fastest growing WordPress WooCommerce marketplace for code script and themes. Buy Sell innovative web solutions simply & easily in lowest affordable price from our marketplace. Buy production ready scripts and themes with full after sale professional service warranty from our marketplace.

Why it becomes fatty? How to solve it? etc. Sometimes, we found our controllers become more than 3000/5000 lines. It’s then very hard to maintain. See the below controller of near about 3000 lines.

Split long controller into small units

One class should have only one responsibility; it might be ‘user’, ‘event’, ‘coupon’ etc. It is easy to maintain, readable, reusable, easy to debug, can easily do unit testing. To refactor a controller, group necessary functions into separate controllers or common class. In the below diagram, we group necessary functions into separate controller.

Group common work methods into separate controller

We should follow DRY principle. Let’s see what Wikipedia says about DRY:

Every piece of knowledge must have a single, unambiguous, authoritative representation within a system

If you need to use same function on each controller, please move that function into a common class; such as a component/service/library/helper etc. then you can use it on multiple areas. Always write code smartly.

Write code smartly

Writing too many nested loops make our code un-maintainable, unreadable. Also it is memory hungry.

Avoid nested loops

You should break down nested loop into separate functions or use different conditional blocks. Also you can use complex SQL queries in some cases.

We do copy-pasting from tutorials without understanding the full code. While following a tutorial, instead of writing the full code in controller, please move all the queries to model, keep the logic into the controller / services / helper / component and the presentation to view file.

Delegate work load to respective layer

The above diagram depicts instead of writing every business login in controller, you can delegate the proper code to proper place.

Laziness is one of the reason behind a fat controller. When we have a very tight deadline, we need to complete the tasks anyhow. It spoils the work. After the meeting the deadline, we can refactor our code.

Sometimes we comment out a huge block of code (near about 500 lines) though we follow version control. Here, you can see we comment out a huge block of code. But it is kept in version control, so, we can delete this code without any hesitation.

Unnecessary comment block

In version control, whenever we need we can get our old code from history. so, we don’t need to comment out our code fearing we might need it later.

While using asynchronous requests, we need to update information dynamically into our presentation layer. Sometimes, we pass HTML block in AJAX request from controller to view layer instead of JSON.

Don’t write HTML code block in controller

JSON is light weight and simple to parse. Moreover, as we pass HTML data, it becomes very difficult to modify the HTML code block which is written in controller. Hence also process JSON while requesting AJAX. Please write corresponding HTML block on presentation layer. You can pass multi-dimensional nested JSON object and iterate it on the presentation layer.

In a nutshell, we have discussed about some common pitfalls of developers during implementing a controller. These are basic guideline while creating a controller. Every developer should culture the above habits while coding. It’s all about practicing a good coding culture. Happy Coding!!! Cheers!!!

Leave a Reply