DEV Community

Discussion on: It's Just Syntactic Sugar

Collapse
 
idanarye profile image
Idan Arye

Several years ago I encountered a StackOverflow answer about C++ virtual functions - a seemingly unrelated subject, but one paragraph has changed my view about these things:

However, also keep in mind that replacing virtual function calls with other means of branching (if .. else, switch, function pointers, etc.) won't solve the fundamental issue -- it may very well be slower. The problem (if it exists at all) isn't virtual functions but (unnecessary) indirection.

I believe this wisdom can be applied to many things - not just virtual functions and not just performance. Many facilities and practices are shunned for reasons like "reducing performance", "introducing complexity" or "increasing the risk of bugs". But when they explain why that thing has these harmful effects, they usually ignore their purpose.

In the case of virtual functions, for example, the purpose is polymorphism - but the comparisons don't use polymorphism. So you run polymorphism code for nothing - of course it would be slower. If you used a benchmark where runtime polymorphism is actually needed, you would have had to implement it in some other fashion - which would have probably reduced performance as much as virtual functions.

Back to the subject

A syntactic sugar usually replaces a more complex code. Removing the sugar does not mean removing it's complexity - it means trading it for the complexity of the desugared code, and I fail to see the gain in that...