Understanding more about MVVM

Alok Choudhary
4 min readJul 28, 2018

We all know about MVC and it’s shortcomings. I would love to explain MVVM that make developer’s life as well as testing quite easy

Creating a View Model

Before we take a look at View Model. let’s tale a look at something familiar. You would feel home if you’ve been using the Model-View-Controller pattern to build Swift applications. The moment the view controller has finished loading its view, it performs a network request. It delegates this task to the APIManager class, which is a good start to reduce the responsibilities of the view controller.

Now let’s see how above situation would look like in MVVM design pattern. I’m keeping the example as simple as possible.

After looking at the example above one might ask the most important question. “I just add a view model? Is that necessary for such a simple view controller?”. The question implied that one missed the more profound benefits of the pattern. One see the MVVM pattern as a slightly more sophisticated implementation of the MVC pattern, with a few additional bells and whistles.

Before we go further, it’s time to put brains to work. You’ve now seen both implementations. The MVVM implementation is simple and we could make the implementation a bit better. But that’s not the point. Can you see how this simple view controller is benefiting from the MVVM pattern? Take a minute and think about the possible advantages MVVM has in this simple implementation.

The Bigger picture

Most developers solve the problems they face without taking a look at the bigger picture. That’s fine and it’s something one only learn after they gain experience building software. For many developers, the implementations shown above look very similar and rest assured that a surprising number chooses the first implementation over the second one. Why is that? Make a guess.

The first implementation looks less complex and that may be true. It has a few flaws, though. Why is the view controller responsible for making a network request? It’s true that the API manager helps the view controller with this task, but the view controller still knows that the notes it fetches come from a backend. You could argue that these details are hidden by the API manager, but that’s not the point. The point is that the view controller controls a view and it shouldn’t know where the notes come from. It should only need to ask for content to display in its view. It shouldn’t even know about the API manager let alone how it does its job.

The view controller of the MVC implementation also manages state, an array of Note instances. This is typical for the MVC pattern. This isn't bad or wrong, but the MVVM pattern hides this detail in the view model.

What’s is the takeaway

If we take a close look at the second implementation in which we use a very simple view model, we discover that the view controller is only responsible for controlling its view. It configures the view it manages and responds to user interaction. These tasks are the only responsibilities a view controller should have.

The view model may appear as just another object in the object graph. But it’s more than that. It hands the view controller an array of notes. The view controller has no idea where the notes come from and it shouldn’t be concerned with these details. They could be fetched from a backend, but they might as well come from a local cache managed by Core Data or Realm.

This also makes unit testing so much easier. Unit testing the view model is trivial and the testability of the view controller automatically improves.

Stepping Up the Game

I strongly believe robust, testable code is built in a way that approaches the problem from a different perspective. We could stick with the MVC implementation, but I hope this post has convinced you that it isn’t the best solution. Using MVVM may look complex, but it really isn’t. It simply requires you to think differently and bigger.

If you’re new to Swift development, then it’s fine to embrace the MVC pattern. I believe that’s a good place to start because it keeps everything a bit simpler. However, I hope you don’t stop there. I hope you have the ambition to evolve and grow as a developer and focusing on your foundation as a developer should be your top priority. Always.

Further reading about MVVM

https://docs.microsoft.com/en-us/xamarin/xamarin-forms/enterprise-application-patterns/mvvm

Originally published at https://alokchoudhary.com .

--

--