Classic Visitor
0
weighted edit units
Decide whether classic Visitor, a discriminated union, a visitor-like object, or polymorphic methods fit your tree. Then quantify the maintenance side of the trade with the same E×V model explained in the Visitor pattern guide.
Answer for the codebase you have, not for the textbook example. Mixed answers produce mixed recommendations; the tool says when two designs are viable instead of inventing certainty.
Enter the current tree size and the change rates you expect. The model spreads changes evenly through each year: a new operation costs the current number of element types E, while a new element type costs the current number of operations V. Classic Visitor also pays one accept method per new type.
The visitor-update portion of classic type churn is weighted by 4×. The direct accept edit stays one unit. That weight comes from the companion experiment: adding one node without updating three operations produced 12 TypeScript diagnostics with classic Visitor versus 3 direct exhaustiveness errors with the union. The experiment and its limits are documented in the compiler experiment section.
Node or element classes/variants that exist today.
Visitors or exhaustive operations that exist today.
0
weighted edit units
0
weighted edit units
| Year | Classic cumulative | Union cumulative | Classic extra cost |
|---|
These units are not hours. They compare edit surface and compiler-noise exposure; they do not price operation cohesion, public extensibility, traversal control, or team familiarity.
| Classic Visitor | Stable hierarchy, cohesive operation objects, public extension points. |
|---|---|
| Union + pattern matching | Same-team code, changing element types, language-level exhaustiveness. |
| Visitor-like object | ESLint, Babel, and other frameworks that already own traversal. |
| Polymorphic methods | Few intrinsic operations that belong naturally to each element. |
The wizard captures architectural facts that a cost formula cannot see: who owns traversal, whether outsiders extend the API, and whether operations carry state. The calculator captures the part that can be counted honestly: the E×V edit surface as types and operations multiply.
Read them together. If the calculator says the edit ledger is close, the wizard's qualitative factors should decide. If type churn dominates the ledger, classic Visitor needs a strong cohesion or public-API reason to survive that cost.
The companion guide compares javac, ANTLR, ESLint, Babel, TypeScript narrowing, Java sealed types, and Python match with runnable examples. It also publishes the compiler experiment behind the 4× weighting used here.
Use classic Visitor when the element hierarchy is stable, operations benefit from cohesive objects, or third parties need a public extension point. Use a discriminated union or pattern matching when the same team owns the code and element types change often. If a framework owns traversal, use its visitor-like object API instead of adding accept methods.
ESLint, Babel and similar tools already walk the tree and dispatch type-specific handlers. Adding accept methods or a parallel hierarchy would fight the framework rather than help it. In that situation the useful part of Visitor is only the operation object keyed by node type.
It measures weighted edit surface over time: how many methods or cases you write as new operations and element types arrive. It does not measure runtime performance, readability, team familiarity, or the value of keeping a stateful operation cohesive. Treat it as a maintenance ledger, not as an automatic architecture verdict.
The companion compiler experiment added one node type without updating three operations. The discriminated union produced three direct exhaustiveness diagnostics, while classic Visitor produced twelve because interface implementation failures cascaded through the classes and recursive accept calls. The ratio, twelve divided by three, is used here as a calibrated noise weight.
Yes. The calculator deliberately isolates edit cost, and that narrow ledger cannot price cohesion, public extensibility, accumulated diagnostics, or controlled traversal. Those are exactly the factors that can justify classic Visitor, so use the wizard and the calculator together.
Plain JavaScript has neither compiler-enforced visitor completeness nor exhaustive narrowing. A registry keyed by node type is often clearer than simulating visitX methods, but missing handlers can fail silently. Whichever style you choose, tests should enumerate the element types and operations you expect.