Why I Love Model-View-ViewModel
The first programming language I picked up was PHP and, since it was my first experience programming, I immediately loved it. At that time, I had no idea what object-oriented programming was. The result was a lot of spaghetti code that did something.
Maintaining spaghetti code is a nightmare. It may be fine for a one-off script when you are short on time, but for software development you need structure. Later that year, I discovered object-oriented programming, which was still in its infancy for PHP at that time. That was another revelation and I was immediately hooked.
My first experience with programming was great, but I also got badly stung by not having a foundation to build on. There is nothing wrong with procedural programming, but I very much enjoy the structure and capabilities object-oriented programming provides. After discovering object-oriented programming, I became fascinated by patterns and best practices that could make my projects more structured, less coupled, and easier to maintain.
But MVC Didn't Cut It
Even though the Model-View-Controller pattern is a widespread pattern for application development, it isn't perfect. Apple's implementation of the MVC pattern falls short and is often too limiting.
It was after the introduction of Swift that I heard about the Model-View-ViewModel pattern. I was reluctant to learn yet another pattern that promised sanity in software development. I had heard that before. But at some point, I gave it a try in a small project and I am glad I did.
What Makes Model-View-ViewModel Great
The first thing you notice when learning the Model-View-ViewModel pattern is that it works very well with the Cocoa SDK. I consider it an improved version of the Model-View-Controller pattern. If you have been using the Model-View-Controller pattern to develop iOS or macOS applications, picking up MVVM is going to be different ... in a good way.
The first benefit of MVVM you experience is how much code you can remove from your view controllers. Have your view controllers become the dumps of your projects? Your view controllers are in charge of managing a view. That's it. But your view controllers do much, much more than that. Don't they? Managing the view has become one of the many tasks of your view controllers.
The Model-View-ViewModel pattern resolves this issue by outsourcing common tasks to the view model. View models are objects that sit between the model layer and the controller layer. Your view controllers no longer communicate directly with the model layer of your application. View models feed your view controllers with data and provide an interface to interact with the model layer.
Writing unit tests for view controllers is a pain because of their close relation to the view layer of your application. By moving data manipulation to the view model, testing your application becomes that much easier. Testing view models is easy. Really. Because a view model doesn't have a reference to the object it is owned by, it is easy to write unit tests for a view model.
Another, and often overlooked, benefit of MVVM is improved testability of your view controllers. Your view controllers no longer depend on the model layer, which makes them easier to test.
Separation of Concerns
In a project built with the MVC pattern, you are often faced with the question which code goes where. Code that doesn’t fit or belong in the model or view layer is often put in the controller layer. This inevitably leads to fat controllers that are difficult to test and manage.
By adding view models to the mix, MVVM presents a better separation of concerns. The view model translates the data of the model layer into something the view layer can use. The view controller is no longer responsible for this task.
The responsibilities of your view controllers are reduced to controlling the interaction between the view layer and the model layer, glueing both layers together.
The view model provides a transparent interface to the view controller, which it uses to populate the view layer and interact with the model layer. This results in a transparent communication between the four layers of your application.
A Change of Mindset
Swift has accelerated the adoption of the Model-View-ViewModel pattern. I use MVVM in every new project I start. It has changed the way I write and think about code. Believe it or not, that is probably the most important benefit of adopting MVVM.
Stop using your view controllers as dumps and build robust, scalable applications that are easy to test and maintain. Learn more about the MVVM pattern in Mastering Model-View-ViewModel With Swift. You won't regret it. I promise.