Chapter 2

Classes Considered Harmful

Not because they can't work. Because they lie about what they do.
Demo: Order-dependent calls: addItem → applyCoupon → calcTotal → calcDiscount → calcTax
☠ Dishonest: Order class (Java)
addItem()
applyCoupon()
calcTotal()
calcDiscount()
calcTax()
Order
items[]
total0.00
discount0.00
tax0.00
couponnull
updatedAtnull
addItem()
applyCoupon()
calcTotal()
calcDiscount()
calcTax()
CouponRegistry
«singleton»
TaxService
«singleton»
✦ Honest: two pure functions (Python)
apply_coupon(items, "SAVE10", coupons)
↓
total = sum(prices); discount = total * rate
↓
return (total, discount, subtotal)
↓
calculate_tax(result, "NY", tax_rates)
↓
return grand_total
total89.97
discount9.00
tax6.48
grand_total87.45
0 steps advantage
0
Steps (class)
0
Steps (function)
0
Field mutations
0
Singleton lookups
Speed: |

What you just saw

The left panel is a Java Order class with 6 mutable fields. The caller makes five calls: addItem, applyCoupon, then calcTotal, calcDiscount and calcTax. Nothing in the class says the last three must come last, and in that order. Each one reads fields that the calls before it wrote. Run calcDiscount before calcTotal and it multiplies a total of zero. Two of the calls reach into Singleton global state: the CouponRegistry and the TaxService. Each Singleton is a hidden dependency on state that no signature admits.

The right panel is two pure functions. apply_coupon takes the items and the coupon table and returns a total, a discount and a subtotal. calculate_tax takes that result and returns the grand total. Each function gets everything it needs as arguments, so there is no wrong order to call them in: the second cannot run until the first has returned. The function side finished in 5 steps and sat idle while the class side was still working through 9.

The counters are the real story. The class version takes 9 steps, writes 6 fields and reaches into global state 2 times, and it gives the right answer only if the caller remembers the order. The function version takes 5 steps and mutates nothing. Every extra step is a place where the answer can change without the caller knowing.

The Trap

When classes are the right tool: file handles, network connections, database cursors, game entities with frame-by-frame state. Resources that are inherently stateful and whose lifecycle must be managed explicitly. These are narrow, well-defined cases where a small class with explicit state transitions makes sense.

The first trap is generalizing from these cases. “Some things need classes, therefore architecture should be class-based.” File handles need classes. Order calculations do not.

The second trap is the Singleton dressed as architecture. Every time you see SomeThing.getInstance() or @Injectable({ providedIn: 'root' }), you are looking at a global variable. The design pattern literature named it something else and documented it to give it respectability. It remains a global variable. The need for it signals that the architecture lacks a message-passing strategy: components cannot communicate directly, so they communicate through shared mutable state with a fancy name.

The third trap is the framework that requires classes. Spring requires @Service classes. Angular requires @Component classes. The instinct is to organize all code into classes because the framework demands it at the boundary. The alternative: keep the framework boundary as thin as possible. The class that Spring or Angular requires should contain one line of code that calls a function. All logic lives in functions. The framework gets its class. You get to keep your sanity.

How this works: This demo visualizes the execution difference between dishonest and honest code. This demo counts steps, mutations and global lookups, which are architectural facts, not timings. Every step takes the same time on screen. Instrumented source code, Dockerfile, and raw trace data: github.com/adamzwasserman/honest-code-traces
← Ch.1: All Languages are Good Ch.3: Data Is Just Data →