liveBook · Manning

Chapter 7: Staying immutable with untrusted code

What you will learn in chapter 7 of Grokking Simplicity

The chapter introduces the problem of preserving immutability when copy-on-write code interacts with code that may mutate data.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The chapter introduces the problem of preserving immutability when copy-on-write code interacts with code that may mutate data.

Key terms: immutability, copy-on-write, legacy code, mutable

7.1 Immutability with legacy code

The section introduces a legacy promotion function that mutates an otherwise immutable shopping cart and names defensive copying as the way to interact with it safely.

Key terms: legacy code, copy-on-write, defensive copying, black_friday_promotion()

7.2 Our copy-on-write code has to interact with untrusted code

This section explains why exchanging data with code that does not follow copy-on-write threatens an immutable safe zone and calls for stronger protection.

Key terms: safe zone, untrusted code, inputs and outputs, immutability, copy-on-write pattern, defensive copying, modifications

7.3 Defensive copying defends the immutable original

This section explains how defensive copying protects immutable data when it crosses the boundary between trusted and untrusted code.

Key terms: defensive copying, deep copy, safe zone, immutable original, untrusted code

7.4 Implementing defensive copies

This section shows how to use defensive copies when calling code that mutates a shopping cart, without exposing the original or trusting the returned data.

Key terms: defensive copies, deep copy, immutable discipline, black_friday_promotion()

7.5 The rules of defensive copying

This section gives two deep-copying rules for preserving immutability when data crosses a boundary with untrusted code.

Key terms: defensive copying, untrusted code, deep copy, immutable data, copy as data leaves your code, copy as data enters your code

7.6 Wrapping untrusted code

This section shows how to make untrusted code safer to call by putting defensive copying inside a reusable wrapper and copying data at trust boundaries.

Key terms: defensive copying, black_friday_promotion_safe(), untrusted code, trusted code, unsafe zone, payrollCalc(), payrollCalcSafe(), defensive copies

7.7 Defensive copying you may be familiar with

The section shows defensive copying at service and process boundaries, explains why multiple copies of user data are acceptable, and contrasts its cost with copy-on-write.

Key terms: defensive copying, shared nothing architecture, references, data, facts about events, copy-on-write, deep copies, shallow copies, safe zone

7.8 Copy-on-write and defensive copying compared

This section compares when and where to use copy-on-write and defensive copying, and how their copying rules and costs differ.

Key terms: copy-on-write, defensive copying, safe zone, untrusted code, shallow copy, deep copy

7.9 Deep copies are more expensive than shallow copies

This section contrasts deep and shallow copies and explains why deep copies are reserved for boundaries with untrusted code.

Key terms: deep copy, shallow copy, structure, untrusted code, copy-on-write

7.10 Implementing deep copy in JavaScript is difficult

This section explains how deep copying traverses nested data, contrasts it with shallow copying, and cautions against using a teaching-only JavaScript implementation in production.

Key terms: deep copying, shallow copying, share structure, untrusted code, standard library, _.cloneDeep(), Lodash, JSON-legal types

7.11 A dialogue between copy-on-write and defensive copying

The section compares copy-on-write and defensive copying, then applies their roles to code that interacts with an immutable safe zone.

Key terms: copy-on-write, defensive copying, safe zone, shallow copies, deep copies, untrusted code

7.12 Conclusion

The conclusion contrasts defensive copying’s ability to preserve immutability on its own with its copying cost, then explains why it pairs well with copy-on-write.

Key terms: defensive copying, copy-on-write, shallow copies

7.13 Summary

7.14 Up next…

Concepts explained in this chapter

← Chapter 6: Staying immutable in a mutable language Chapter 8: Stratified design: Part 1 →

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.