Chapter 2 - Grokking Simplicity
Organizing code by "rate of change"
In order from "Changes Frequently" at the top to "Seldom Changes" at the bottom where each thing is implemented in terms of the things below it
| Pizza Kitchen Layers | Inventory Layers | Main Layers |
|---|---|---|
| This week's menu -Recipe for weekly special | **This week's shopping trip -Deciding where to buy ingredients | Business Rules |
| **Pizza making - Structure of recipe | ** Ingredients lists - Ingredients list operations | Domain Rules |
| **JavaScript - Objects - Arrays | **JavaScript - Objects - Numbers | Tech Stack |
Stratified design neatly separates business domain, and technology concerns
Part 2: First-class abstractions
Image of a robot saying "There is only one way for the pizza to be made" and a caption saying "One robot, one timeline"
MAKING A CHEESE PIZZA - sequential process
Starting at the top, at each step, you always know what the next step will be. Every step in a timeline is an action.
- Order comes in
- Prep: make dough
- Usage: roll out dough
- Prep: make sauce
- Usage: spread sauce
- Prep: grate cheese
- Usage: spread cheese
- Put in oven
- Wait 10 minutes
- Serve (At this point you're done making the pizza. The robot can go back and wait for the next order)
Timelines visualize distributed systems
- There are three robots working in parallel, so three different timelines
- Operations on different timelines can interleave; you don't know what order they happen
MAKING A CHEESE PIZZA
Order Comes In:
Robot 1: Make dough
Robot 2: Grate cheese
Robot 3: Make sauce -> roll out dough -> spread sauce -> spread cheese -> put in oven -> wait 10 minutes -> serve
Compare this to the original sequential process we looked at previously
Multiple Timelines can execute in different orderings
Here are 3 examples: "Dough Takes Longer", "Cheese Takes Longer", and "All 6 Possible Orderings"
Dough Takes Longer (there are three columns to illustrate what each robot does. I’ve called the robots 1, 2, and 3, for ease of description)
Robot 1: Make Dough (dough not ready until after the other robot tried to roll it out)
Robot 2: Order Comes in -> Grate Cheese
Robot 3: Make sauce -> roll out dough -> spread sauce -> spread cheese -> put in oven -> wait 10 minutes -> serve
Cheese Takes Longer
Robot 1: Make dough
Robot 2: Order comes in -> grate cheese (cheese not ready until after Robot 3 tries to spread it)
Robot 3: Make sauce -> roll out dough -> spread sauce -> spread cheese -> put in oven -> wait 10 minutes -> serve
All 6 Possible Orderings *only works when "make sauce" is last
- Make dough -> grate cheese -> make sauce*
- Make dough -> make sauce -> grate cheese
- Make sauce -> make dough -> grate cheese
- Make sauce -> grate cheese -> make dough
- Grate cheese -> make dough -> make sauce*
- Grate cheese -> make sauce -> make dough
Cutting the timeline: Making the robots wait for each other
The original three-robots were set up without coordination. Any of them could finish first. Let's look at the three-robot set up WITH coordination.
THREE-ROBOT SET UP WITH COORDINATION
Order Comes In: After the preparation steps: "make dough", "grate cheese" and "make sauce" have been completed, the second robot can finish making the pizza
Robot 1: Make dough
Robot 2: Grate Cheese
- Have all preparation steps been completed?
If yes: Roll out dough -> Spread sauce -> Spread cheese -> Put in oven -> Wait 10 minutes -> Serve
Robot 3: Make sauce
Toni says "I can have each robot wait for the others to finish prepping, and then one of them can finish the assembly. That way, it doesn't matter what order the preparation steps are done in."
Positive lessons learned about timelines
The cut (or the line after the preparation steps in the previous table) means nothing below that line happens before anything above the line. The assembly functions do not care what order the preparation actions happened because of the cut.