Representation exposure (source code)

= Representation exposure

<Representation exposure> gives outside code access that can modify storage on which the <representation invariant> or promised behavior depends. Returning the internal <array> exposes a route around the <API>; merely hiding the field name does not close it. Incoming shared mutable storage can create the same problem. Copies or immutable values can prevent such modification, but their costs and sharing rules belong in the design.<6.005 Software Construction, Spring 2016/Reading 13: Abstraction Functions & Rep Invariants>[Reading 13, Invariants, Immutability: mutable aliases through both returned values and constructor arguments]{ref}

The <bounded queue with encapsulation> deliberately stores immutable labels. A <FIFO queue> of mutable objects needs a separate promise about whether changing a returned object changes stored content; protecting the queue's ordering and size does not automatically make every stored object immutable.