Sequential Coupling
Revised October 19, 2026. What changed
Sequential coupling appears when callers must know an undocumented order of operations: initialize, prepare, process, then finish. Some protocols genuinely require an order. The design question is whether invalid orderings are easy to express and hard to diagnose.
Own the sequence
If every caller repeats the same lifecycle, give one component responsibility for it. Distinguish a successful commit from cleanup that must happen even when processing fails.
static void withCleanup(Runnable process, Runnable cleanup) {
try { process.run(); } finally { cleanup.run(); }
}
This fragment guarantees that cleanup is attempted. It does not guarantee cleanup succeeds, make the operation transactional, or roll back partial external effects. If both operations can throw, preserve the original error or use try-with-resources for an AutoCloseable resource so suppressed exceptions remain inspectable.
Make states explicit
For a longer protocol, model states such as new → prepared → committed with a failure path. Reject a second commit or a commit after cancellation. A builder can make required preparation part of construction; a state machine can make allowed transitions visible.
Test failure between each pair of stages. In particular, “finish” should not mean both “commit successful output” and “release the connection.” Those operations have different preconditions. The same issue appears in agent ownership and late writes.
The workbench includes the complete LegacyExamples.java program, imports, assertions, and a run command: java LegacyExamples.java with JDK 17 or later. The fragments below are extracted from that same tested source.