Ensuring proper encapsulation
medium 40 minPrivate fields are the start, not the end.
Watch lesson4:46
Check yourself
One question at a time. Each answer explains why.
Question 1 of 13
What is a defensive copy?
When should a getter return a copy?
What is a benefit of private fields with methods?
A constructor checks `slot.start < slot.end` and then stores `copy(slot)`. What is wrong?
`return tuple(self._orders)` with changeable orders. What can a caller do?
Which is enforced at run time?
What does `Collections.unmodifiableList(xs)` give?
All methods are synchronized. Which line is still a race?
Where should `notify(listeners)` stand in `add`?
In Java, who can read a `protected` member?
Two fields `low` and `high` with `low <= high`. Best way to change them?
60 callers use a leaking getter. First step?
Which type has no invariant to protect?
Pick an answer to see why.
Not quite. Caller and class no longer share the changeable object.
Not quite. Immutable values can be shared.
Not quite. One place for the rule, and the inside may change.
Not quite. Copy first, then check the copy.
Not quite. The tuple is shallow.
Not quite. The engine enforces `#`. TypeScript's `private` disappears.
Not quite. It is a window on the same list.
Not quite. Two atomic calls are not one atomic step.
Not quite. Foreign code may run only when the invariants hold.
Not quite. Package access comes with it.
Not quite. With two setters some valid moves pass through an invalid state or are refused.
Not quite. Writers need an action before the door closes.
Not quite. x and y vary independently.
Flashcards
Say your answer, then reveal it.
Card 1 of 13
Proper encapsulation in one sentence?
No sequence of calls from outside can make the rules of the object false.
Say your answer, then reveal it.
What does `private` protect?
The variable's name. Not the object it points to.
Say your answer, then reveal it.
Defensive copy on input: which order?
Copy first, check the copy, store the copy.
Say your answer, then reveal it.
View or snapshot?
A view is free and shows later changes. A snapshot costs a copy and shows one moment.
Say your answer, then reveal it.
When is no copy needed?
When the value cannot change, all the way down.
Say your answer, then reveal it.
Shallow copy trap?
The container is new, the elements are shared.
Say your answer, then reveal it.
Why no `setStart` and `setEnd`?
The rule covers both. One method must change both.
Say your answer, then reveal it.
What is better than a setter?
An action that says what happened: `give_raise(10)`.
Say your answer, then reveal it.
When do callbacks run?
After all writes, when every invariant holds, over a copy of the listener list.
Say your answer, then reveal it.
How does `this` escape?
Registering or starting a thread in the constructor. Use a factory.
Say your answer, then reveal it.
Why a private lock object?
Callers can lock on `this` and stop you.
Say your answer, then reveal it.
When is sealing too much?
Values that vary independently have no invariant: use a plain immutable record.
Say your answer, then reveal it.
Float salary trap?
`nan < 0` is false, so NaN passes the check. Use whole cents.
Say your answer, then reveal it.
These cards come back in Today, just before you would forget them.