liveBook · Manning

Chapter 6: Staying immutable in a mutable language

What you will learn in chapter 6 of Grokking Simplicity

Introduces the chapter’s plan to use copy-on-write for arrays, objects, and deeply nested data.

After this chapter you will be able to

What it builds on

Section by section

Introduction

Introduces the chapter’s plan to use copy-on-write for arrays, objects, and deeply nested data.

6.1 Can immutability be applied everywhere?

The section recalls copy-on-write and defines nested data to frame why updating an item inside a shopping cart is a harder immutability problem.

Key terms: nested data, deeply nested, copy-on-write discipline

6.2 Categorizing operations into reads, writes, or both

The section classifies shopping-cart and item operations as reads or writes and explains why writes need an explicit immutability discipline in JavaScript.

Key terms: reads, writes, calculations, immutable discipline, copy-on-write, mutable data

6.3 The three steps of the copy-on-write discipline

Explains the three-step copy-on-write discipline and why it lets a function produce an updated value without changing the original data.

Key terms: copy-on-write discipline, copy, local scope, immutable, read, write, add_element_last(), copy-on-write

6.4 Converting a write to a read with copy-on-write

The section converts a cart-removal function to copy-on-write and explains how callers must use its returned cart.

Key terms: copy-on-write, remove_item_by_name(), cart.splice(), call site, return value, global shopping_cart

6.5 Complete diff from mutating to copy-on-write

6.6 These copy-on-write operations are generalizable

This section explains why recurring copy-on-write operations should be made reusable, using array splice in remove_item_by_name() as an example.

Key terms: copy-on-write, splice(), remove_item_by_name(), boilerplate

6.7 JavaScript arrays at a glance

This section surveys JavaScript array operations and applies them to a copy-on-write mailing-list update.

Key terms: array, index, .length, heterogeneous, assignment operator, .push(el), .pop(), .unshift(el), .shift(), .slice()

6.8 What to do if an operation is a read and a write

The section identifies operations that both read and write, then names two ways to handle them with copy-on-write.

Key terms: .shift(), read, write, copy-on-write, split the function, return two values, separates the responsibilities

6.9 Splitting a function that does a read and write

This section shows how to turn an operation that both reads and writes an array into separate operations, then make the write copy-on-write.

Key terms: read, write, calculation, copy-on-write, first_element(), drop_first()

6.10 Returning two values from one function

Shows how to turn array operations that both return a value and mutate an array into copy-on-write reads, then discusses reusing those operations.

Key terms: copy-on-write, read, write, .shift(), .pop(), calculations, add_element_to_cart()

6.11 Reads to immutable data structures are calculations

The section explains why converting writes to reads makes data immutable and turns reads of that data into calculations.

Key terms: mutable data, actions, writes, immutable, reads, calculations

6.12 Applications have state that changes over time

This section explains how an application can track changing state while keeping each version of its shopping cart immutable.

Key terms: shopping_cart global variable, swapping pattern, immutable

6.13 Immutable data structures are fast enough

Explains why immutable data is usually fast enough in practice despite its performance costs.

Key terms: immutable data, mutable data, performance bottlenecks, copy-on-write, shallow copy, structural sharing, references, garbage collector, immutable data structures

6.14 Copy-on-write operations on objects

This section applies copy-on-write to JavaScript objects and explains how shallow copies create structural sharing.

Key terms: copy-on-write, Object.assign(), set_price(), shallow copies, structural sharing, references

6.15 JavaScript objects at a glance

This section summarizes JavaScript object operations and shows how copying an object supports copy-on-write updates.

Key terms: object, key/value pairs, undefined, dot notation, delete, Object.assign(), copy-on-write, objectSet(), Object.keys()

6.16 Converting nested writes to reads

This section shows how to convert a nested shopping-cart write into a read while keeping the original cart and its items unchanged.

Key terms: nested writes, copy-on-write, setPrice(), setPriceByName(), immutable, nested data structure, references

6.17 What gets copied?

The section uses a shopping-cart price change to show that copying nested data can preserve unchanged objects through structural sharing.

Key terms: nested data, shallow copy, structural sharing, setPriceByName()

6.18 Visualizing shallow copies and structural sharing

The section uses a shopping-cart price change to show which pieces copy-on-write duplicates and which unchanged pieces the old and new carts can safely share.

Key terms: copy-on-write, shallow copy, setPrice(), setPriceByName(), structural sharing

6.19 Conclusion

The conclusion explains why copy-on-write in JavaScript takes discipline and why utility functions make it easier to follow consistently.

Key terms: copy-on-write discipline, utility functions, wrapper functions

6.20 Summary

6.21 Up next…

The section previews defensive copying as a way to exchange data with existing code that does not follow copy-on-write.

Where this is used later

Concepts explained in this chapter

← Chapter 5: Improving the design of actions Chapter 7: Staying immutable with untrusted code →

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.