Skip to main content

Ownership, Aliasing, and Immutable Snapshots

What This Concept Is​

Encapsulation includes who can change a value after it crosses a boundary. Copying an outer collection changes its structure ownership, but may leave nested objects shared. A live unmodifiable view prevents client mutation through that view while still reflecting backing-store changes. A snapshot promises a selected observation remains stable; say whether that promise includes nested elements.

Why It Matters Here​

A caller may retain history while the account continues changing. The return type alone does not establish which observations remain stable. That distinction determines whether replacing a representation preserves the client's contract.

Concrete Example​

transactions = [{'minor': 10}]
snapshot = tuple(transactions)
transactions[0]['minor'] = 20
assert snapshot[0]['minor'] == 20

The tuple cannot append a row, but it shares the mutable dictionary. For stable history, map to immutable values, e.g. tuple(row['minor'] for row in transactions). That changes the exposed representation, so consider compatibility for existing callers. Copying is not free; estimate work and memory with collection size.

Common Confusion / Misconception​

Two equal Money values can be interchangeable under the lab contract. Two accounts with the same balance remain distinct entities: withdrawing from one must not change the other. Mutable identity and value equality answer different questions. Check constructor input aliases as well as returned values. Python privacy and frozen dataclasses support ordinary API discipline, not a security sandbox.

How To Use It​

ChoiceBenefitObligation
Immutable value snapshotStable retained observationCopy/creation cost and element immutability
Read-only live viewCan avoid snapshot allocationDocument later changes and iteration consistency
Transferred mutable ownershipEfficient exclusive mutationEnsure old owner cannot keep using aliases

Check Yourself​

Does a read-only view promise a snapshot? Can equal balances make two accounts the same entity? Identify the mutable alias in the worked example.

Mini Drill or Application​

Practice: explain why the lab history tuple of integers is sufficient while a tuple of mutable transaction dictionaries is not. Feedback: the required observation includes each signed amount, and integers have no mutating operation.

Read This Only If Stuck​

For Java, verify the distinction in the List contract, including optional mutation and null restrictions. This is versioned API guidance, not a claim that every collection implementation shares one cost model.

For additional caller-contract context, locate code contracts through the Good Code, Bad Code book index. The aliasing counterexample above supplies the concrete ownership distinction.

Use the design contracts and labs to turn these claims into evidence.