2

Today, I have learned that it is considered good practice to have fat models and skinny controllers. Up to now, I have had it the other way around, so everything that I believed I understood about MVC has now been proven to be wrong.

Most articles indicate fat models/skinny controllers approach is better, and the main reason I see is that they claim that controllers are not re-usable. This is true for standard installations of CodeIgniter, but when using a plugin like Wiredesignz' HMVC plugin, this becomes a non-issue. So, in short, the question is:

What is the best approach to use for Code Igniter specifically?

  1. Fat models/Skinny controllers
  2. Fat controllers/Skinny models with HMVC plugin
  3. Fat controllers/Skinny models WITHOUT HMVC plugin

I am not considering fat controllers/skinny models WITHOUT HMVC plugin as an option any more, but included it for completeness.

Your thoughts, please? Is an HMVC plugin thus an evil thing?

Vitalii
  • 103
  • 1
  • 6
Kobus Myburgh
  • 1,114
  • 1
  • 17
  • 46
  • 1
    The current interpretation of HMVC is more of a side effect of the Rails-like design pattern (it's not MVC what CI implements, it's Rails). As for skinny controller, in HMVC you would basically use the same implementation as in simple Web MVC. Only difference would be that t controller **might** end up dealing with management of subrequests. But even that part is possible to extract outside MVC triad ... thought that would mean implementing HMVC without that plugin. – tereško Jan 19 '13 at 22:50

2 Answers2

6

You are kind of right in that splitting into modules does negate some of the reasons for working SC/FM. But it is still good practice for several reasons:

1) Re-usability of code As you said models are supposed to be reusable, the more code you can make reusable the better. Yes I know modules do this too, but the point of modules is more about code being isolated and portable, you still may want to extend those controllers at a later stage and working in a SC/FM way will help when that time comes.

2) Code readability When reading your code most people will start from your routes config and then go to the appropriate controller. A skinny controller acts as a director and should be concise and readable. Just making calls to more complex and verbose code. Less code is easier to follow.

3) When in Rome Not to be overlooked, the fact that everyone* who uses MVC frameworks works this way means that you should work this way too, yes its nice to be different, but if you have 5 people in the same team working together who all want to be different it's just a pain in the arse.

Should you rewrite your code?

Probably not immediately, I'm sure you have better things to do. If you start trying to think in this mindset from this point forward, at some point you will look back at you old code and change it (probably when you're in there changing something anyway), but for now if it aint broke...

Further more, working in a modular way is not as straightforward as it appears, making truely reusable modular code is not easy and actually requires quite a lot of overhead compared to doing quick and dirty code for a single site. May be better to start practicing this, on fresh code until you have a good understanding of what's required to convert your existing code to something truely modular - this would be the time to sort out your fat controllers.

When writing new code for a particular site, I always bear in mind that I may want to reuse it on another site later, but if I tried to make everything re-usable with every option under the sun I would never get anything done. Far better to go and tweak it when the time comes.

Opinion on HMVC I'll be honest, I dont really know what the HMVC bit is about, but modules are the best thing since sliced bread, since I stumbled across HMVC wiredesignz I now do much less work for a lot more website - and much more importantly I spend a lot less time doing the same thing... What you want a gallery on your site. Heres the gallery manager module, Cms you say, yes there is a cms module, products, yes theres a products module.... you get the idea. I now have lots of variations on one framework rather than hundreds of different sites.

Summary

You may be dealing with simple modules at the moment which means that code re usability and clarity is not a major issues within those modules. But start coding with good practice today. And when you are working on some mammoth CMS or products module you will be glad you did.

Check this out for a bit of a guide on what should go where How should a model be structured in MVC?

Community
  • 1
  • 1
SwiftD
  • 5,769
  • 6
  • 43
  • 67
  • Thank you - very complete and well-explained answer. With this, I can most certainly get on the right track :-) Thanks! – Kobus Myburgh Jan 07 '13 at 14:58
0

Well i dunno about HMVC, but i have been working with codeigniter for quite time, i can advise you about fat model and skinny controller is a good practice when it comes to :

  1. Having back-end and front-end accessing the same models
  2. Creating your own API to interface with other application so you re-use the same fat models

However I have took a look for HMVC documentation and it seems it's really depends on how you are going to distribute your logic and how this modular component will share he same data resources

It depends on the project you are working on

Ahmed Gaber
  • 707
  • 1
  • 10
  • 27
  • Thanks, Ahmed, I appreciate your comment. I am trying to ascertain whether it is feasible for me to convert all my current projects to the FM/SC way. But since this is a lot of work, I am not convinced yet :-) – Kobus Myburgh Jan 07 '13 at 13:01