In this article, I will be showing you four simple steps you can use, to convert a state machine diagram into working code. These steps will make writing state machines so easy, that you will want to have all your stories described in state machine diagrams.
Before we get into the solution, let's understand what a state machine is. A state machine is a system that takes in events and produces new states given the accumulated events that it has received.
This diagram is a very common state machine that we find in most applications.
Writing a pure view model makes it very easy to test and understand. But writing a pure view model reactively makes for better code.
In my last article, we converted an impure view model into a pure one. At the end of the article, we saw that the mutability of one of the properties in the view model property is not ideal. In this article, I will fix this issue by converting this pure view model into a reactive one.
Let’s take a look at the view model again.
The name property in the view model is mutable, and it needs to be since this needs to be updated when we update the view model with the
NameService.Response. However the issue with
name being mutable is that anyone that has access to our view model can change its value. Here is an example of a mistake someone might do. …
In my last article, I wrote about pure functions, what they are, and why they are awesome. This is the second part of a three-article series I am writing that goes into the concept of pure code.
In this article, I will be converting an impure
viewModel into a pure one. I will show you how it's difficult to write unit tests for impure
viewModels and how it becomes super easy once they are pure.
Below is a very common
viewModel setup you might find in many codebases.
ImpureNameViewModelTypeis? Read this article)
Right off the bat, we can see that this
viewModel is not Pure. It makes a request to the server, thus changing the state of the server you are consuming, meaning this
viewModelis not pure. Secondly, we should ask ourselves what the input of the
viewModel is. I guess you could say that the server response is the input, but this is not passed in. The
viewModelalso suffers from having multiple responsibilities. It makes a request and it handles the response. How would we unit test this? You can mock the request and have full control of the server response. To do this, I could point you to "request mocking libraries", but now we need to also provide a mock value, that will probably be in JSON format meaning probably want to add JSON files to the project. All this can get very ugly very quickly (I'm not going to show this example). …
Programmer: I write Pure functions now.
Mathematician: You mean functions?
Programmer: No, PUREEEE functions!
Mathematician: What the heck is a PUREEEE function?
Exactly, what the heck is a Pure function? And why is this mathematician confused?
Let me explain.
A very common answer you will get when asking developers “what is a Pure function?” is that a pure function only does one thing. But this is incorrect. A Pure function is described as the following;
In computer programming, a pure function is a function that has the following properties:
- Its return value is the same for the same arguments (no variation with local static variables, non-local variables, mutable reference arguments or input streams from I/O devices).
- Its evaluation has no side effects (no mutation of local static variables, non-local variables, mutable reference arguments or I/O streams). …
Model-View-viewmodel (MVVM) is one of the most used, and known patterns in mobile development. At its core, MVVM helps you separate your view logic from your business logic. The idea is that your views should not know where or how the data it is displaying came to be. In this article I want to show you how I write MVVM, and why I write it the way I do. I have seen many code bases that can be improved with what I am about to write. So let’s get into it.
When I am writing a pattern (or any code for that matter), the first thing I think about is; What do I want to accomplish with these patterns?. This is already known with MVVM. In essence what you want to accomplish with MVVM is the…