MVC: Minimum Viable Code?

Not really. In a way Minimum Viable Code is a pretty good motto to live by, as the less code to do task (correctly!) generally the better. In this case however we are talking about Model View & Controller. These three objects are linked intrinsically to one another. A coding trinity to help guide new and old coders a like in designing object-oriented applications.

One of the main attractions of the MVC design pattern is that it breaks down code into more digestible chunks for debugging purposes. All the information in each entity, model, viewer and controller -can- all be placed in one file, one place. The issue there being that even simple applications become quite long and unwieldly to debug and sift through. Splitting up the coding between three or more entities helps keep down bloat and makes applications easy on the eyes. What then, do each of these concepts do?

The Model: The model handles the data of the application. Need to get information on X, Y, or Z? Gotta have access to the model. Need to update information on X, Y, or Z? Gotta have access to the model.

The View: This entity is pretty self explanatory. The view is what handles how a user interacts, or ‘views’ if you will, with the overall program. The view is generally made up of web design code, such as HTML, CSS, and Javascript.

The Controller: The Controller is what makes the magic between The View and The Model happen. Without it, all you would have is a data set that has no interaction with what is displayed on the user’s screen. Through The Controller users can manipulate the Model. Through the Controller the Model can display data to the user in the View.

What would these look like in an application? I’m glad you asked, dear reader as I have made a few examples!

The in code comment really says it all. The Model is pretty basic in scope compared to the View and the Controller. At least at the low level!

This should still look pretty basic. In this case our view file is just an html document, but it could easily take in other languages as well using ‘<% -insert ruby code %>’ and ‘<%= -insert ruby code %>’ syntax. The difference between ‘<%’ and <%=’ as your starting bracket is that the former will only execute code, where as the latter will be execute and display the code.

There is some additional stuff happening in The Controller, so lets go over it. Like our Model, the Controller is a class as well. This one in particular inherits ‘Sinatra::Base’. This is so that our Controller can use all the methods and functions found in basic Sinatra web framework. That allows us to configure a path to our views folder so that we can reference that in our routes.

What is ‘get “/” do’ doing? It is simply retrieving a route and then displaying something. In this case we are displaying our erb file, ‘the_view’. this is the same view linked just a couple spaces up. If we were to launch this application as a web server it would be exceptionally dull. It would have only a homepage that displays the html in our view.

Hopefully this explains a bit about how the MVC design pattern works in the coding world. It is exceptionally good at breaking down robust and powerful applications into digestible chunks that are far easier to debug and work with. It is a true modern standard for good coding etiquette in many areas, and for good reason.

Hi, I'm studying Software Engineering in boot camp and Software Development and Security in college. I used to write poetry but now I just play video games.