r/functionalprogramming • • Aug 23 '23

Question Switch statement discouraged? Match expression encouraged?

I get the sense that in imperative programming, OO first and foremost, using the switch statement is generally discouraged in favor of "coding to the interface" and the ensuing dynamic dispatch and runtime polymorphism as something more maintainable, extensible etc. At the same time, in FP, I feel like using match expression is very much encouraged. They do differ in power etc, but what do you feel is the reason for the difference between how the switch is discouraged and match encouraged?

11 Upvotes

9 comments sorted by

View all comments

Show parent comment

2

u/effinsky Aug 25 '23

Yes, let's say this is about the case where the thing switched over might be extended or otherwise changed, like the shape example. would you say the problem with the switch is that then when the modification happens you don't have exhaustive checking so you might easily miss a switch here or there? or that the code is generally messier with the switches and that using an interface/polymorphic type is just cleaner?

1

u/Flyyster Sep 27 '23

The struggle is real if you have big software, and besaid "shape" object gets an update. Then you have to touch every single switch that switches on the shape structure. This in itself is a pain.