Understanding the Basics of MVVM in Swift-Ui.
Hi there It's been a whileeeeeeeee, and I am happy to get back to writing, And also I have been away building my skillset more than a designer (learning Swift UI now) and I am glad to share all I have learned so far in my journey in learning about IOS programming.
Okay, firstly what is MVVM?
I will give you the big words (so I can sound smart )
MVVM ( Model — View — ViewModel) is a software design pattern in computer software that facilitates the separation of the development of the graphical user interface from the business logic of an application.
Now in the simplest terms
MVVM is a way/pattern to build modern software by Separating your Data/Model from your Functions/Methods and also from your Views/Screens.
As it provides a cleaner, safer, and faster way to write and maintain your code.
Let's talk more about it.
When building mobile apps as a beginner it's easy to have all your functions, data, and everything in one view. Why not?? it's cool to have it in one place right??. Well not completely.
Here is an example of a code without MVVM architecture 👇
In the above image, we created a text and a button, whereby if the user clicks on the button it changes the color of the text.
Issues with the code above on a Large scale?
If you take a closer look at the code, it's not a bad practice but it's not safe as well. Why ??
- We created our variable in our current view
what happens when you want to use that data in another view??? - Our function is initialized in our current view
If we have to use the function in a different view, we have to repeat the code again and again which is not good practice.
Now here is where MVVM architecture makes it cleaner and safer when building 👇
Now Let’s break it down.
From the code above we have our Data separate from our functions and from our view and now it's easier to track and test our code.
And also it's easier to scale our code when need be.
Benefits of MVVM architecture.
- Separation of concerns: MVVM separates the view, the view model, and the model into distinct layers. This makes the code more modular and easier to maintain.
- Testability: MVVM makes it easier to write unit tests for the view model and the model. This is because the view model and the model are not dependent on the view.
- Reusability: MVVM view models can be reused in different views. This can save time and effort when developing new features.
- Scalability: MVVM is a scalable architecture that can be used to develop complex applications.
Here is a more detailed comparison of MVVM with other code architectures:
Conclusion:
Overall, MVVM is a good choice for developing complex and maintainable applications. It is especially well-suited for applications that have a lot of user interaction.
I am Marvis Ighedosa your favorite product designer and engineer.
Let's connect on Twitter and Linkedin —