Mastering MVVM

Domenico Cafarelli
5 min readMay 12, 2023

--

Best Practices for SwiftUI Programming

Photo by JJ Ying on Unsplash

Introduction to Design Patterns (A little bit of history)

Design patterns have been around in the software development world for a few decades now. The term was first coined in 1987 by Christopher Alexander, who was an architect and urban planner. In software engineering, design patterns were first introduced in the book “Design Patterns: Elements of Reusable Object-Oriented Software” by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides in 1994. Since then, design patterns have become a popular way of solving common problems in software development.

What a design pattern is

A design pattern is a general, reusable solution to a commonly occurring problem in software design. It is not a complete solution, but rather a template that can be applied to different situations to solve similar problems. Design patterns help developers to solve common problems in a way that is both effective and efficient.

What a design pattern is for

Design patterns are intended to help developers create code that is more modular, easier to maintain, and more adaptable to changing requirements. They also help to standardize software design by providing a shared vocabulary and set of best practices. By using design patterns, developers can create more robust and reliable software that is easier to test and debug.

The most used design pattern for SwiftUI

In SwiftUI, the most commonly used design pattern is MVVM (Model-View-ViewModel). MVVM is a design pattern that separates the presentation logic (the view) from the data and business logic (the model) using an intermediary object called the view model. The view model acts as a bridge between the view and the model, providing the view with the data it needs and handling user input.

The advantages of MVVM

MVVM has several advantages when used with SwiftUI. First and foremost, it helps to keep the code organized and easy to maintain. By separating the presentation logic from the data and business logic, MVVM helps to keep the code modular and makes it easier to modify and extend. It also enables better testability of the code, as the view model can be tested independently of the view.

Another advantage of MVVM is that it supports the declarative and reactive programming style of SwiftUI. SwiftUI views are declarative and describe what they should look like based on their state. MVVM facilitates this approach by providing the view model, which manages the state of the view and communicates with the model. The reactive nature of SwiftUI makes it easy to update the view in response to changes in the state.

MVVM also supports separation of concerns, which is a key principle in software design. By separating the presentation logic from the data and business logic, MVVM makes it easier to change one part of the code without affecting the others. This separation also enables better collaboration between developers, as each developer can work on a different part of the code without interfering with the others.

The disadvantages of MVVM

While MVVM has several advantages, it also has some disadvantages. One of the main disadvantages is that it can introduce additional complexity into the code. The addition of the view model layer can make the code more difficult to understand, especially for developers who are not familiar with the pattern. It can also increase the amount of code that needs to be written and maintained.

Another disadvantage of MVVM is that it can be overkill for small projects or simple views. For these cases, a simpler design pattern such as MVC (Model-View-Controller) may be more appropriate. Additionally, implementing MVVM correctly requires some additional work, such as defining the bindings between the view and view model and ensuring that the view model is properly updated when the model changes.

How to use the MVVM with SwiftUI

  1. Model Layer: The Model layer represents the data that the application is handling. In SwiftUI, this could be represented by a simple struct or class that contains the relevant data. For example, in a To-Do list app, the Model layer might include a struct that represents a single To-Do item, with properties such as title, description, and completion status.
  2. View Layer: The View layer represents the user interface and is responsible for displaying the data to the user. In SwiftUI, this is represented by the View struct, which contains the SwiftUI code for creating the UI. However, in the MVVM pattern, the View should not contain any logic or state — it should simply display the data that is provided by the ViewModel.
  3. ViewModel Layer: The ViewModel layer is the intermediary between the Model and View layers. It contains the business logic and state of the application, and provides the data that the View needs to display. In SwiftUI, the ViewModel is typically represented by an ObservableObject or a @StateObject, which can be passed down to the View using the @ObservedObject property wrapper. The ViewModel should contain the logic for fetching data from the Model layer, as well as any transformations or calculations that are needed before the data can be displayed. It should also expose properties or methods that the View can use to interact with the data.
  4. Binding: In the MVVM pattern, the View and ViewModel communicate using a binding mechanism. This allows the ViewModel to update the data in response to user input, and vice versa. In SwiftUI, bindings are typically created using the $ prefix, which creates a two-way binding between a property and a state variable. For example, if the ViewModel has a @Published property called “selectedItem,” the View can create a binding to this property using the $selectedItem syntax.

By following these steps, you can implement the MVVM pattern in your SwiftUI code. Remember to keep the layers separate and to use bindings to facilitate communication between the View and ViewModel. With a little practice, you’ll find that the MVVM pattern can simplify your code and make it easier to maintain over time.

Conclusion

In conclusion, design patterns are an important part of software development, and MVVM is a popular pattern used in SwiftUI programming. Despite its advantages, MVVM can also introduce additional complexity into the code and may be overkill for simpler projects or views. However, when implemented correctly, MVVM can help keep code organized, modular, and easy to maintain, as well as support the declarative and reactive programming style of SwiftUI. It is ultimately up to the developer to decide whether MVVM or another design pattern is most appropriate for their project based on the specific requirements and complexity of the code.

--

--