3 ms·
The key benefit of pattern matching is not code that is more concise, but code that is more likely to be correct. Consider their first example, interpreting a
by jefftk 5y ago
The key benefit of pattern matching is not code that is more concise, but code that is more likely to be correct.
Consider their first example, interpreting a command in a textual game. In many places the logic is divided into a few pieces: check that there are the right number of arguments, do something with some of them, pull off the rest, etc. It's really easy to make mistakes with this kind of code, where your conditions don't quite align. With pattern matching, you write a single pattern for a case, and I would expect this to give many fewer bugs.
- mikepurvis 5y agoOn the theme of skepticism, though, I would worry that there may only be a fairly narrow band of complexity for this type of logic where Python 3.10's pattern matching is the ideal solution, past which you want some kind of grammar and a proper parser that just spits out a polymorphic object with a do_thing() method.