Use thin controllers.

Some guidelines for the MVC technique.

MVC stands for Model-View-Controller, a popular programming pattern used in web applications. The Models represent the business objects, which contain business logic and database access. The Views handle the presentation of data to the user, the user interface. Finally, the Controllers route traffic from the business objects to the user interface.

As a good practice, you want thin controllers. Having too much logic in a controller is like having too many methods in a class. Each class should have one job. To reduce a controller’s responsibilities, one should move logic to other classes.

Controllers should not format output; instead, the Views should handle this. You should move data validations, user authorization/authentication, and other functions to helper classes. Models can manage business logic, data storage, and complex queries.

Controllers can call other controllers, which is very helpful in reducing the responsibilities of one controller. Similarly, a controller can route data from multiple models.

What’s left are thin controllers that are easily understood and also very simple to maintain.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: