Fat models and skinny controllers? Well… not exactly!

Make sure you follow us on Facebook, Twitter, Website and of course YouTube.

I got a question from one of the most active users on Devlob, Umer Abbas.

I have once mentioned in one of my videos that you should keep controllers skinny and models fat.

That is true only to some extend.

Let me elaborate more on this.

You controllers should be as skinny as possible.

I make sure that my controllers have max 4–5 lines of code. NO MORE THAN THAT! (except if their is a very very special occasion)

To make that possible, you can use various techniques.

1. Repositories

By using repositories you can separate the logic of your controller, to a different class.

By doing this, you can create multiple functions to that class and as a result, your controller will be small enough and very readable.

This technique also helps to avoid private methods in controllers, which personally I don’t like at all. I want my controllers to have just the 7 resource actions, no more than that!

It is not a mistake to have private functions in your controllers, but I personally find it difficult to maintain. At the end of the day, we are using repositories and you can keep your private methods there, at least most of them.

Of course, if you do all that, it means that your repository will be a big ass class with too much code…

So… is that good? You simply take the code from your controller and pass it to a repository? Does it really work like that?

Well, no… You should refactor your repository from there.

You have multiple solutions here!

You can use static methods in your models to keep your queries short.

Some people don’t like this… but User::byUsername(‘devlob’) is so readable!

Does it make the model fat? Yes it does! Is this what we want? Kinda! We will see how to tackle models a bit later.

2. Query classes

Since we are talking about queries, you can use Query classes to further reduce your query code to possibly 1 line.

Consider doing that, only for your long queries. Don’t overdo it!

3. Avoid switch statements

Whenever you have a switch, you can move to polymorphism (at least most of the times).

This will further reduce the code in your repository.

By doing all that and separate your logic in so many different classes, then even your repository will be short enough. So, you don’t really have to worry!

4. Events and listeners

Another great technique to reduce code in your controllers are events and listeners. We already have a video on that and we know how it works. If you want to refresh your memory take a look at this video here.

Events and listeners will decouple the logic so much, that you will fall in love with them.

5. Forget about if/else

This is not a big deal, but I see so many people not doing this and I get mad! You don’t want to see me mad! (wink) (wink)

Suppose you have this kind of code:

return true
return false

Then change it to this

return true

return false

6. Design patterns

There are so many design patterns out there, that can reduce your code even more. You should always consider design patterns whenever are needed.

I would love to go through all of them (impossible to say the truth), but they depend a lot of your situation.

I love using design patterns such as the State Pattern, but I find the Decorator Pattern difficult to read in some cases (as mentioned above).

So, design patterns really depend on your situation, whether you need them or not, whether they make your code more readable or not.

Sometimes, they might add more complexity to your code, so choose your design patterns wisely and use them whenever required!

What about models?

Regarding models, I tend to use traits, even if some people don’t like them, they will make your models thinner, which is always a better case.

You might consider decorators as well… But you will end up overdoing it and make the code messier. I don’t really tend to use them that much to be honest.

You can do so much more to keep your controllers thin AND your model THIN as well, but the above techniques are enough!

We have already seen some of the above techniques on Devlob, but I will make sure to create more and more courses to make all that clear.

So, make sure you follow us on Facebook, Twitter, Website and of course YouTube.

And because I am addicted to social media so much, follow me on Facebook and Instagram! I tend to post programming related posts that have no room in Medium.

I hope this article will help you to maintain big ass projects! :)



Making dreams come true...one course at a time!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store