The WHY Series: Why should your controllers be thin? (ADDENDUM)
Ok – You totally got shortchanged.
In the second article of The Why Series, Why Should Your Controllers Be Thin?, we took a kind of proof-by-contradiction approach by controllers should be thin because fat controllers are a code smell for a different problem.
We grazed oh-so-lightly across the possibility that fat controllers are fat because they have business logic in them and that business logic should be in the business layer.
Did ya notice that all we did was shovel the should from controllers should be thin, over to business logic should be in the business layer
LAME.
Let’s peel this onion a few more layers so you actually get your money’s worth.
The Real Reason Controllers Should Be Thin
One of our primary jobs as a developer is to keep bugs out. And one of the best ways to do that is to write reusable code.
If you write a function the calculates interest on a loan, you or one of your teammates don’t want to have to rewrite that same function every time the calculation is needed. The calculation could end up different, which would be a bug.
Write the calculation function once in a library for easy reuse by you and anyone else that wants to save time and to build your system upon the stable ground of battle-tested code.
Ok Joe, thanks for the Software Engineering 101 lesson, what does this have to do with thin controllers?
This is what was left out!
Controllers should be thin because controller code can’t be reused. So if you’ve got fat ass controllers reeking the putrid stench of business logic, you’re shortchanging yourself and your teammates by keeping that code in a unusable location.
Why can't controller code be reused?
Well, if you use static methods you probably could reuse it, but static methods don’t always make sense, and that’d be sloppy as fuck because your teammates would have a hard time finding it anyway.
The same goes for instantiating a controller at some random point in your app just to use a public method off of it.
No.
What kind of logic would a thin controller have?
If you’ve theoretically moved the business logic into the appropriate layer, what would be left in your controllers?
To keep this article from going on too long, here is how we are going to answer that: If you haven’t heard of The Making Loops Newsletter, that’s the weekly developer essays I send out early to folks who have subscribed. If you subscribe using the form below, you’ll get the answer to the above question.
You’re welcome to unsubscribe right away if you want, this newsletter is mostly made up of .NET devs trying to up their game in career and life. We try to answer those questions that tutorials tend to leave out like why you do certain things, rather than just copying code.