StackPractices
Decision wizard E×V cost model 100% client-side

Visitor vs Pattern Matching

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.

1. Decision wizard

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.

Language
Who owns the tree traversal?
Who adds new operations?
How often do new element types arrive?
How often do new operations arrive?
Do operations need state, configuration, or accumulated diagnostics?

2. Accumulated cost calculator

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.

Inputs

Node or element classes/variants that exist today.

Visitors or exhaustive operations that exist today.

Classic Visitor

0

weighted edit units

Union + pattern matching

0

weighted edit units

Cumulative cost over time

Year Classic cumulative Union cumulative Classic extra cost

Interpretation

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.

The decision in one table

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.

Why the two halves are separate

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.

Visitor decision FAQ

Should I use the Visitor pattern or pattern matching?

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.

Why does framework-owned traversal change the answer?

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.

What does the accumulated cost calculator measure?

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.

Why is classic Visitor type churn weighted four times higher?

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.

Can classic Visitor still be the right choice when the calculator favors pattern matching?

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.

What should I use in untyped JavaScript?

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.