r/functionalprogramming • u/effinsky • 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
3
u/Flyyster Aug 24 '23 edited Aug 25 '23
If you listen to people like r c martin they will tell you to don't use switch statements. However it is a matter of how you use them. If you switch on / match on types that are likely to change in the future (like the scenario in which "shape" class derivatives "triangle" "circle" are implemented and later another derivative "square" is added and now every switch statement has to be updated) then don't use them, but in scenarios where the category is not likely to change, like switching on a boolean result or on a result category that programmatically has a deterministic set of outcomes, then use them. They yield the same benefits as pattern matching does, making the branches of your program more explicit.
4
u/andrewcooke Aug 23 '23 edited Aug 23 '23
it's the difference between making decisions based on value and type. roughly, types are treated differently, while values of any one type are treated similarly.
in "older" imperative languages there's little support for types so values become a proxy.
(edited to fix a typo)
4
u/Trequetrum Aug 23 '23
Switch statements for a lot of imperative languages exists to mirror how many PLC Programs are structured. They don't interoperate very well with the rest of the abstractions offered by the languages that employ them.
Match statements, on the other hand are quite a bit different:
It really comes down to how well each construct matches the structure of your program. Match statements in functional languages express the idea of arrows between sets really well and switch statements in PLC programs express boolean logic really well. Switch statements in more modern languages don't really express much. They're lack-luster at best and error prone at worst.
IMO
3
u/DeepDay6 Aug 25 '23
Two more thoughts, although they might be wrong. It's hard to tell why somebody tells you "don't do x" when you neither know their domain of expertise very well nor the origin of the quotation.
If you use a functional language that supports type classes, those are in some ways very similar to interfaces in OO languages. So destructuring/matching can do what "coding to the interface" does.
And then there's the fact that FPers tend to use plain immutable data to represent things as opposed to objects, which intermingle/package possibly mutable value and computation. So it's easier and more safe to match on immutable bins of values than considering mutable objects.
6
u/[deleted] Aug 23 '23
[removed] — view removed comment