liveBook · Manning

Chapter 4: Extracting calculations from actions

What you will learn in chapter 4 of Grokking Simplicity

The chapter introduces a planned refactoring of existing software to extract calculations from actions and improve reuse and testability.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The chapter introduces a planned refactoring of existing software to extract calculations from actions and improve reuse and testability.

4.1 Welcome to MegaMart.com!

Introduces MegaMart’s always-visible shopping-cart total and explains what the DOM is.

Key terms: shopping cart, total cost, document object model (DOM), HTML page

4.2 Calculating free shipping

The section introduces MegaMart’s free-shipping icon requirement and a straightforward imperative way to keep the icons updated.

Key terms: free shipping, buy button, calc_cart_total()

4.3 Calculating tax

The section adds tax updating to the cart-total workflow while noting that simply adding it to the existing implementation is not the functional programming approach.

Key terms: tax, cart total, calc_cart_total()

4.4 We need to make it more testable

The section explains why MegaMart’s business rules are difficult to test and identifies changes that would make testing easier.

Key terms: business rules, DOM updates, global variables

4.5 We need to make it more reusable

The section explains why the shopping-cart code cannot be reused by accounting and shipping, and what must change to make it reusable.

Key terms: global variables, DOM, return the answer, reusable

4.6 Distinguishing actions, calculations, and data

The section classifies the current functions as actions and explains why an action inside a function makes the whole function an action.

Key terms: actions, calculations, data, Actions spread

4.7 Functions have inputs and outputs

The section defines how information and effects enter and leave functions, then shows how that distinction identifies actions and guides their conversion into calculations.

Key terms: inputs, outputs, explicit inputs, explicit output, implicit inputs, implicit outputs, action, calculation, side effects, arguments

4.8 Testing and reuse relate to inputs and outputs

This section connects advice for making code easier to test and reuse with the distinction between implicit and explicit inputs and outputs.

Key terms: business rules, DOM updates, implicit output, global variables, implicit inputs, implicit outputs, arguments, return value, explicit output

4.9 Extracting a calculation from an action

This section extracts cart-total logic from an action, then turns it into a calculation by replacing global-variable reads and writes with an argument and a return value.

Key terms: extract subroutine, refactoring, action, implicit input, implicit output, shopping_cart, shopping_cart_total, calc_total(), cart, return value

4.10 Extracting another calculation from an action

This section shows how to extract calculations from actions by turning implicit inputs into arguments and implicit outputs into return values.

Key terms: calculation, action, implicit inputs, implicit outputs, arguments, return values, add_item(), shopping_cart, implicit input, implicit output

4.11 Let’s see all of our code in one place

The section revisits how to label code as actions, calculations, or data and emphasizes that one action makes an entire function an action.

Key terms: action, calculation, data

4.12 Conclusion

The section closes the MegaMart example by reporting that the revised code pleased its users and hinting at further design improvements.

4.13 Summary

4.14 Up next…

The section closes the chapter by noting that some actions remain after calculations are extracted and previews improving those actions in the next chapter.

Where this is used later

Concepts explained in this chapter

← Chapter 3: Distinguishing actions, calculations, and data Chapter 5: Improving the design of actions →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.