liveBook · Manning

Defensive copy

A defensive copy is an independent copy of data made to prevent changes through another reference from altering retained data or internal state.

How 2 Manning books explain it

Grokking Simplicity

A technique that copies data intended to be retained so subsequent modifications cannot alter the retained version.

How it is used
Protects immutable data while exchanging it with legacy code, libraries, or other code that may mutate shared references.
How deep it goes
Defines two boundary rules, explains deep versus shallow copies, and demonstrates JavaScript wrappers without detailing deep-copy internals.
Examples
Uses a shopping cart promotion, payroll library wrapper, user-change subscription, web APIs, and Erlang or Elixir processes.
What you can do after
Apply it via deep-copy wrappers that safely exchange data with mutating functions and return protected results.

Read how Grokking Simplicity explains defensive copy →

Data-Oriented Programming in Java

A defensive copy is an independently created duplicate of mutable data that prevents external mutation from changing the original representation.

How it is used
Uses defensive copies to keep validated cart data immutable after construction.
How deep it goes
Demonstrates collection copying in code, but does not develop a broader catalogue of defensive-copy techniques.
Examples
Uses active and saved cart lists copied with List.copyOf after invariant validation.
What you can do after
Apply defensive copying when constructing immutable validated data from mutable collections.

Read how Data-Oriented Programming in Java explains defensive copy →

Also covered in

What to know first

Related concepts

Questions about defensive copy

What is defensive copy?

A defensive copy is an independent copy of data made to prevent changes through another reference from altering retained data or internal state.

Which Manning books explain defensive copy?

Grokking Simplicity and Data-Oriented Programming in Java.

What should I know before learning defensive copy?

Manning books that teach defensive copy usually introduce state, object, list and tuple first.

What is the difference between defensive copy and copy-on-write?

A defensive copy is an independent copy of data made to prevent changes through another reference from altering retained data or internal state. Copy-on-write is a technique that shares data until modification requires creating an independent copy, preserving the original for existing users.

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.