Yet again, Skinny controller – Fat model?

I do not want to set any basis on this topic, but I have heard this since I started with Ruby on Rails. Why this blog post? Because I found that sometimes people (me included) just get on the surface layer. To put this simple, I would draw an easy simile:

Skinny controllers should be as skinny, and fat models should be as fat, as you can bear for a catwalk model.

Ok, this may not hold true for all guys but I think the idea is well depicted.

Secondly, I would like to note a couple of tips to make our models as fat as you can bear and your controllers as skinny as you can bear. I once was given a very important tip on something that anyone with a formal training on computer science should know, but that we mostly often forget. Honestly I am one of these guys who just get high using Vectors, Hashes and LinkedLists as soon as possible. Think through and strictly make sure that there is not a more efficient way (though less readable) of accomplish our purposes. Sometimes you may want to sacrifice efficiency and increase complexity by choosing a very clear solution but for me that is probably not the way to go in a web application. The old saying goes like this:

Smart data structures and dumb code works a lot better than the other way around.

That itself is your clue that the logic for those relationships, including validation should be in the model or database layer. An skinny controller is the consequence of well-designed data. The more effort you put into designing your data, the less code you have to write overall.

Controllers are best at parsing inputs, calling the appropriate models, and then formatting the outputs. Then you should let the model do validations (Rails makes this easy as pie). Make this as skinny as possible, so if you chose your data properly you should not be writing clumsy and messy code.

I think I do not have to mention avoiding atrocities like this:

And keep it like this.

Leave a Reply

Your email address will not be published. Required fields are marked *