Clean Code, Horrible Performance

Categories: Product, Startup

Summary

Uncle Bob Martin's polymorphism-based refactoring pattern runs 1.5-15x slower than plain table switches because strict clean code rules block compiler optimizations. Prioritizing code abstraction over runtime performance can inadvertently handicap your application's speed.

Key Takeaways

  1. Polymorphism-based refactoring patterns can degrade performance by 1.5-15x compared to simpler table switch implementations due to compiler optimization limitations.
  2. Strict adherence to clean code dogma—avoiding long functions, preventing runtime type awareness, always preferring polymorphism—actually blocks modern compilers from performing critical optimizations.
  3. Optimizing compilers can't inline or optimize code paths when they can't determine runtime behavior due to polymorphic substitution, making abstraction a performance liability.
  4. Balance clean code principles with compiler visibility: allow the compiler to understand what's actually happening at runtime rather than enforcing abstract patterns that obscure intent.

Related topics

Transcript Excerpt

You made this video titled clean code horrible performance and essay/video showing how Uncle Bob Martin's [music] polymorphism based refactoring pattern runs about 1.5 to 15 times slower than a plain table switch version. >> So, in this particular video I was talking specifically about the things that were advocated that are like very specific things that are said, like don't have functions over a particular length or these sorts [music] of things. Things should not know the type at runtime or whatever. There's all these kind of rules about it, preferring polymorphism always. And if you look at those things, they're kind of just bad programming practices. I don't really know how else to say them. The compiler has all these options to take what is fundamentally not particularly great code i…

More from Pragmatic Engineer