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.
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.
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
- stateState is information describing the condition or configuration of a system or computation at a particular time that may influence its behavior or subsequent transitions.
- objectAn object is a distinct entity represented or manipulated within a computational system, typically as a value, data structure, resource, or instance of a type.
- listA list is an ordered collection of elements arranged in a sequence.
- tupleA tuple is a finite, ordered collection of values grouped as a single composite value, often with elements of different types.
Related concepts
- deep copyuses · A deep copy is a recursive duplicate of an object or data structure that copies its nested contents rather than sharing them with the original.
- immutable collectionimplements · An immutable collection is a collection whose contents cannot be changed after it is created.
- copy-on-writealternative to · Copy-on-write is a technique that shares data until modification requires creating an independent copy, preserving the original for existing users.
- untrusted codeaddressed by · Program code whose behavior or data-modification effects cannot be assumed to follow the calling system's safety or immutability rules.
- contractused by · A contract is an agreed specification of the conditions, expectations, rights, or obligations that govern behavior or interactions between parties or software components.
- wrapperused by · A wrapper is a software component that encloses or delegates to another component or operation while exposing an adapted interface or adding behavior.
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.

