Avoiding mutability as a robust development pattern
The previous recipe introduced the concept of immutable data structures. In this recipe, we are going to discuss a design pattern that avoids persistent database mutability in your code until the very end. In terms of pseudocode, most applications in a long script work as follows:
Do computation 1 Write computation 1 to disk or database Do computation 2 Write computation 2 to disk or database …. Do computation n Write computation n to disk or database
Here, we are going to present an alternative paradigm and discuss why it is generally better from a resilience point of view:
Do computation 1 Write computation 1 to temporary storage Do computation 2 Write computation 2 to temporary storage ... Do computation n Write computation n to temporary storage Take all temporary data and write it to definitive disk and database
First, we will show the code for both approaches, and then discuss why, for complex and sophisticated...