If you’re still using if:else statements it’s official. You’re an old little hobbit, stuck in your hobbit hole of comfort, sitting there eating your danties and smoking a pipe.
Well, I’m Gandalf and I’m here to shake things up a bit. You’ve been complacent for far too long. You’re getting stale. Comfortable. You’re in danger of becoming predictable.
It’s time to set out over the hills and through the woods to new lands full of promise and sunshine.
Thanks to Delta for sponsoring this newsletter! I personally use Delta Lake on a daily basis, and I believe this technology represents the future of Data Engineering. Check out their website below.
Don’t get me wrong, I sympathize with you. I’ve been writing if:else since some of you were chasing your crush around on the playground. But we must change with the times, and embrace new things.
Today, we embrace the match statement.
We need to have a talk … about if:else
This conversation has been a long time coming. You have a problem. I have a problem … with if:else. We’ve gotten so used to it, now it’s like a third arm.
Sometimes it seems impossible to break old habits or imagine doing something different. But, at the same time, it’s fun to “learn” something new, or solve problems using a different approach.
Even if it only means adding a new tool to the old tool belt, it’s nice to have another way to solve problems.
I use if:else statements all the time, and I have some complaints about my own code.
The logic gets complex very fast.
It’s hard to read after a point.
It’s not obvious what’s going on all the time.
It’s easy to miss edge cases.
Just out of curiosity, I did a search of my personal GitHub account looking for all the if:else logic I could find, sure enough, there was plenty.
For example, in tinytimmy my open-source data quality framework for Dataframes, there are many examples of Python if:else statements … with lots of else’s!
I guess folk are probably used to seeing logic like the long if:else above. It’s just easy to write, something we default to. I don’t know, I suppose the more complex the business logic becomes … the harder the if:else becomes to read.
Then, fortunately, or unfortunately, I’m not sure which, I had the idea about a year ago to start working with Rust. That’s where I met match for the first time … face to face … I can thank enums for that.
Introduction to match statement - Python and Rust.
First, if you are unfamiliar with match statements, we should probably start from the basics. What are they (we will start in Rust, and then later check them out in Python)?
“Match statements in Rust offer a powerful and expressive way to handle pattern matching, similar to using switch statements in other languages. They allow you to compare a value against multiple patterns and execute specific code based on the match.”
- Google’s Bard
In code …
What can it look like in real life? Here is an example of a match statement in Rust used in conjunction with an enum, from a recent project I wrote, reepicheep.
In this example, the code matches one of two options, Morning or Evening, and does something different in each case.
Readability and other benefits of matching.
I guess it might be a personal opinion, but it seems like match statements are what I would call “more precise” when written out in code, at least when trying to understand code that is trying to react to multiple conditions.
If you do a little Googling there are a lot of high and mighty claims about how matching is better than if:else, which I don’t disagree with, but I see them as stretching a little.
I mean, if you are purposeful with your if:else I suppose it’s just as good as anything else. But, since when are we all perfect? I think the readability argument is the best one, even if it is the least technical reason to use matching.
Other claims for matching over if:else are often summed up as …
Errors: Explicitly matching patterns reduces logic errors.
Decomposition: Extracting components from matched pattern(s) within the code block.
Exhaustiveness: All patterns are covered, preventing code gaps.
Python’s match-case (>= 3.10)
Lest you think we forgot about Python, never fear. It appears in a slightly different form, but starting in Python version 3.10 and greater there is now match-case functionality.
Hey, and improvement on if:else eh?
Let’s look at the example I showed above from my own code, convert it to a match-case statement, and see what we think.
I mean, doesn’t seem like much, but I suppose when we are dealing with larger statements that are more complex, match might our lives easier, especially when understanding and debugging new codebases.
Always good to have another tool in the belt anyway.
What about you? Have you used match statements, why or why not? Let us know in the comments.
Imho match does not add anything of substance, while it makes your code incompatible with Python < 3.10. Not worth it at the moment (if ever).