2 Functional thinking in action

 

Chapter 2 from Grokking Simplicity by Eric Normand

In this chapter

  • See examples of functional thinking applied to real problems.
  • Understand why stratified design can help organize your software.
  • Learn how actions can be visualized in timelines.
  • See how timelines help you discover and resolve problems having to do with timing.

This chapter will give a broad overview of the two big ideas we will address in this book. Your main goal should be to get a taste of thinking with FP. Don’t worry about understanding everything right away. Remember that each of these ideas will be addressed in several chapters later on. This will be a whirlwind tour of functional thinking in action.

Welcome to Toni’s Pizza

Welcome to Toni’s Pizza. The year is 2118. It turns out people still like pizza in the future. But all of the pizza is made by robots. And the robots are programmed in JavaScript. Go figure.

Toni has applied a lot of functional thinking to the code that runs her restaurants. We’re going to take a brief tour through some of her systems, including the kitchen and her inventory, and see how she has applied the two levels of functional thinking.

Just so you don’t have to flip back, here are the two levels again. Here’s how Toni uses them.

Part 1: Distinguishing actions, calculations, and data

Part 2: Using first-class abstractions

Part 1: Distinguishing actions, calculations, and data

1. Actions

The actions are anything that depend on when they are called or how many times they are called. The actions use ingredients or other resources, like the oven or delivery van, so Toni has to be really careful with how they are used.

2. Calculations

The calculations represent decisions or planning. They don’t affect the world when they run. Toni likes them because she can use them any time and any place without worrying about them messing up her kitchen.

3. Data

Toni represents as much as she can with immutable data. That includes the accounting, inventory, and the recipes for her pizzas. Data is very flexible since it can be stored, transfered over the network, and used in multiple ways.

These examples are all parts of Toni’s pizza business. But the distinction applies at all levels, from the lowest JavaScript expression to the largest function. We’ll learn how these three categories interact when they call each other in chapter 3.

Making the distinction between these three categories is vital to FP, though functional programmers might not use these same words to describe them. By the end of Part 1, you will feel very comfortable identifying actions and calculations, and moving code between them. Let’s take a look at how Toni uses stratified design in her codebase.

Organizing code by “rate of change”

A first glimpse of stratified design

Part 2: First-class abstractions

As applied to a robotic kitchen

Timelines visualize distributed systems

Multiple timelines can execute in different orderings

Hard-won lessons about distributed systems

Toni does a postmortem

Cutting the timeline:Making the robots wait for each other

Positive lessons learned about timelines

Retrospective on coordinating robots