Skip to content
Katabench
Try free

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.

SOLID principles exercises →

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