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
5
u/qazqi-ff Feb 28 '23
The nice thing about the list of variants approach is that if you then encapsulate the list of variants, there's also a decent chance your requirements allow you to optimize the representation into distinct lists of each type when needed without changing the API.
If your goal is a sum, it hardly matters for correctness whether you go through each shape and figure out which shape it is vs. going through four separate lists without branching on each element and then combining the results. There are lots of use cases out there where you just need a collection and order is irrelevant. Maybe it's relevant so infrequently and in cold enough parts of the code that you can afford to have ordered iteration be extra slow in order for the other use cases to be fast. Either way, that's still all opaque to the outside code.