Member-only story
Clean Code vs. Performant Code
Clean code is not always the best approach. Choose how to structure your code based on your needs!
A couple of days ago, I saw this post on LinkedIn, which pointed to this video on YouTube. The video is a great resource, and I strongly suggest you watch it.
The video analyzes some of the common advice provided by clean code, like avoid switch
or if
statements in favor of polymorphism, and presents some C++ examples to show that the resulting code from them is less performant than code that is not clean.
I wanted to check myself if that holds and, specifically, if that holds for Swift. C++ is a language that notoriously gives developers a lot of freedom, while Swift is much more opinionated. Swift can likely perform some internal optimizations that C++ can’t.
In today’s article, I’d like to show the results of this experiment and the methodology adopted and talk a little about the implications and consequences of that.
Hypothesis: Clean Polymorphic Code Performs Worse Than Code Using If-Else
We want to prepare an experiment to test this hypothesis. So, we need two pieces of equivalent code that we can run to measure their performance.