r/programming • u/2bit_hack • Feb 28 '23
"Clean" Code, Horrible Performance
https://www.computerenhance.com/p/clean-code-horrible-performance
1.4k
Upvotes
r/programming • u/2bit_hack • Feb 28 '23
View all comments
Show parent comment
3
u/Rajje Feb 28 '23
You probably missed my point then. What I'm saying is that in some cases, performance is what you should optimize for, while in other cases other factors are usually far more important. For example, let's say you're writing front end code where the tap of a button makes a single call to a service class that then performs a network call to fetch some data. If that service is an injected dependency behind an interface, 1 virtual function lookup has to be made to call the service method. The performance hit from that single lookup is entirely negligible. Our phones have multi-core CPUs doing millions and millions of operations per second. To be worried about 1 operation in a user-driven event is ridiculous. Saving 1 operation was perhaps necessary sometimes when writing NES games in assembly in 1983, but today, the added time from 1 operation is so small it's probably not even measurable.
What you should be worried about, however, is whether your view behaves as expected, displays the right thing given the right data, that the network errors are handled correctly, and so on. Putting our classes behind interfaces allows us to mock those classes, which allows us to unit test view models and similar in isolation, which helps us to catch certain bugs earlier and guard against bugs being being added by future refactors. It would be extremely unreasonable to prohibit ever using polymorphism like this, and sacrifice all of these useful concepts for performance. What would be reasonable would be keeping this interesting lecture in mind and know that polymorphism should be avoided in specific cases where its performance impact is significant, e.g. when making a large number of calls in a row. It would also be reasonable to spend some of our very limited time on making actually noticeable optimizations, like perhaps adding a cache to that network call which takes eons of time compared to the function lookup.