Why you should use MVC
If you are any bit familiar with web development or software architecture you may have come across the phrase “MVC” whether watching a video or reading an article, most explaining how to use it or maybe tips on how to use it effectively. In my time in web development, I have seen and read these articles as well but always wondered why? I believe it is a great idea to use it on your website or web app, but what actually are the benefits of using it? In my research I have reinforced my knowledge of MVC, I have also been shown the reasons to use it, and there is a good amount.
In this post, I’m going to go over a brief description of what MVC is and how it works for folks who aren’t quite sure or are a little rusty. Then I will jump right into all benefits MVC has to offer.
What is MVC?
I think Wikipedia sums it up nicely “Model–view–controller is a software architectural pattern commonly used for developing user interfaces that divide the related program logic into three interconnected elements” I think the important thing to take away from this is that its an “architectural pattern”, a methodology, or an idea for how to set up your website efficiently. The MVC architecture is separated into three main components Model, View, and Controller and we will also talk about each component separately.
A quick note, all the code snippets I show here to demonstrate MVC will be in Ruby on Rails.
Model
This is considered the base level of the architecture. This will represent the data being transferred between the view and the controller. Think of them as the template for how each data object is stored, like what attributes it has.
An important aspect of the model is for relational databases, it defines how the data models are related to each other.
View
The view is associated with the user interface. This is a visual representation of the MVC but only displays things from the controller and the model. The view should not contain any logic but simply render what the controller sends to it.
Controller
This is the request handler, considered the brain of MVC. It is the link between the user and the database. The controller transmits data requests from the user to the model and then delivers back data that is then rendered to the user.
Benefits
Scalability
The separation of components allows for organized code right from the get-go, which will become even more important as the app grows. This allows developers to find any bit or section of code efficiently and get new functionality up and running quickly.
Modifiable
This benefit goes hand in hand with the previous. Because of the separation in MVC, any modification or functionality added is contained in its component. Basically, any addition you make to the code that doesn’t work won’t make the whole architecture come crashing down.
Faster Development process
When building an app from scratch speed can be key, whether trying to meet a deadline or just trying to push to market as soon as possible. MVC caters to speed because it can allow two, or even three, different developers to work on different components simultaneously.
Allows for Test Driven Development
MVC can simplify your testing immensely. With multiple components structurally and logically sound in the app testing can be a breeze. It can also make debugging the app so much easier as well.
Multiple views
Again, because of that separation in this architecture, adding multiple view components can be a breeze. A great example of this is if you were trying to expand your app to mobile, you can do this efficiently with a minimal amount of repeated code.
In summary
From all these benefits you can see that they all boil down to the importance of separation of concerns. Allowing each of the components to function separately allows for huge returns. It can get your app up and running quickly, can be scaled and modified astronomically with less fear of crashes, and is overall easier to manage. A good, but cheesy, metaphor is the three branches of government. All three of them work independently of each other but put together they form the entire government of the U.S. I hope this post was helpful and informative as I learned a great deal from writing it.