Piri Piri

The MVC Pattern as an Analogy

21 February, 2017

For my second lightning talk at the Brisbane Ruby Meetup, I talked about Ruby on Rails’ MVC pattern and how, as a beginner, I struggled to understand the concept. Thanks to Nigel, Tom, 2RK, and the BrisRuby team for letting me talk!

The first explanations of the MVC pattern went way over my head - they were too abstract for someone like me, with no coding background. I wanted to break the concept down to something simple and relatable - something that would make sense to a newcomer to coding. My example is oversimplified, but I think it’s important to have a very basic understanding and then be able to build on that knowledge.

So, if you are really new to Ruby, the first thing to know is that MVC is a pattern used to arrange code. “Pattern” in this particular case means using folders/file structures to compartmentalise things - a bit further on, I’ll explain some advantages to doing this.

If you are a beginner, you might be wondering by now, what MVC is, so let’s talk about that.

One of the first explanations I heard of MVC was something like:

Models: where all of your data is stored (for the application).

Views: what the user interacts with (html page, app view, etc).

Controllers: gets the data from the models and interprets it to the views.

Next, let's look at why you would use MVC.

When I was learning Rails, one of my first questions was, "Couldn’t you have all the code in one massive file?" the answer was, yes, it's possible. To the computer it doesn't really matter, but if you ever need to go back to fix something, or have someone else work on your code, it's going to be a big headache.

The MVC model provides separation of concerns. What this means is that by separating the different parts (or concerns), you can:

• tinker with each part and not break something else at the same time.
• keep it compartmentalised and organised for yourself and team members.
• easier to keep smaller chunks of logic in your head.

Got that? If not, don't worry.

"Enough with your technical mumbo jumbo!"

After looking at a few analogies for the MVC pattern, I found a lot of them broke down on deeper inspection or got very hard to follow, very quickly. This is the analogy I came up with in the end.
MVC as a barista.

Model = Everything you need to make a coffee. Milk, beans, etc.

View = Finished product/cup of coffee.

Controller = Barista - takes the order, puts the ingredients together, and gives you your coffee.

So that's my best analogy. What's yours? Contribute at the BrisRuby Github.

If you have any questions, or suggestions, feel free to drop me a line @lillypiri !