History Keeps a Checkpoint, Not Editor Internals
Medium About 25 min Free, no account needed
Capture immutable mementos that can be restored repeatedly while the history caretaker stays unaware of the editor’s stored representation.
The brief
An editor's undo history currently reads and rewrites its public text property. That caretaker now owns knowledge of the editor's representation, and every new piece of editor state would force the history to change.
Refactor the Editing types into an opaque memento protocol. TextEditor.Capture() returns IEditorCheckpoint; TextEditor.Restore(IEditorCheckpoint checkpoint) restores a checkpoint produced by the editor. EditorCheckpoint is sealed and immutable and stores the captured text. Its content may be internal for the owning editor to use, but the history must not name that concrete type or inspect its representation.
CheckpointHistory stores only IEditorCheckpoint values. Remember(editor) captures one; Undo(editor) restores and removes the newest saved checkpoint, or does nothing if none exists. TextEditor exposes Append(string) and Read() rather than a writable public text property. Both editor and history remain sealed. A captured checkpoint is a stable moment: later edits, restores, and further edits cannot change its meaning.
Preserve Run(string initial, string[] commands). Commands are exactly save, undo, or append:<text>; appended text contains no pipe character. Return the editor text after every command joined by |. Empty commands return an empty string. Repeated saves are independent history entries, and undo on an empty history is harmless.
The trusted caller captures one checkpoint, restores it more than once after separate edits, and consumes it only through the interface. An unused snapshot beside the old public setter protocol cannot satisfy that caller.
How it is graded
Executable structural checks grade the shape of your types as well as their behavior.
What it practices
- Command and memento patterns
- Capture actions and opaque snapshots so undo and history stay correct.
- Encapsulation
- Protect invariants: no exposed mutable collections, shared static state or train-wreck navigation.
Related katas
- Kill the Global Counter Medium Free
Counterkeeps its running total in apublic staticfield every caller shares, and it's an open, inheritable class on top. Give the state one explicit owner instead of just hiding the field, and don't let the reflexprivate staticfix trick you into leaving the second smell standing. - Undo What Actually Happened Medium Free A stock room describes every adjustment twice, once in an Apply switch and again in an Undo switch. Turn each adjustment into a command that undoes itself behind a history that knows no stock, so a mis-keyed shipment can be reversed on its own without rolling back the deliveries that came after it.
- Make It a Value Object Hard Free A Reservation is a mutable two-phase DTO that also hands out its backing guest list. Make it a real value object, frozen at construction, and don't let the obvious
private setfix fool you into leaving the collection leak wide open. - Seal the Aggregate Hard Free
Orderenforces a three-line cap inAddLinebut hands out its backingList<OrderLine>through a public, settable property. Seal the aggregate so callers can only change it through its methods, and don't let the obviousprivate setfix fool you into leaving the collection leak wide open.