HeadlinesBriefing favicon HeadlinesBriefing.com

Clean Code vs. Performance Costs

Hacker News •
×

The video from the Performance‑Aware Programming series highlights real‑world performance costs of following “clean code” guidelines. While many rules—like keeping functions small or using polymorphism—don’t change runtime, several do. The example uses a classic shape hierarchy: a base class with virtual Area() and derived classes for square, rectangle, triangle, and circle. The total area is computed by iterating over an array of shape_base pointers. A hand‑unrolled loop shows little difference in cycles.

Measured results indicate roughly 35 cycles per shape when using the clean, polymorphic approach, whether in a cold cache state or a warm, optimized one. Switching to a flat union with a switch statement—the “old‑school” style—removes virtual calls but introduces a branching decision that can hurt performance when types are mixed.

The key takeaway is that clean code rules are not automatically bad for performance; the impact depends on how they are applied. The comparison demonstrates that well‑structured polymorphism can still be efficient, especially when the compiler and hardware can optimize the virtual calls.

In practice, developers should weigh readability and maintainability against measurable overhead, especially in tight loops or performance‑critical sections.