Venkat Subramaniam on The Elegance of Pattern Matching in Modern Java
Jim Grisanzio with Venkat Subramaniam
Duke’s Corner Podcast — OracleDevLive Preview — February 24, 2022
Duke’s Corner Podcast with Venkat Subramaniam who previews his upcoming session at Oracle Developer Live Java Innovations — The Elegance of Pattern Matching in Modern Java.
Transcript
(00:00:00):
Hey, it’s Jim.
(00:00:01):
Quick conversation here with Venkat Subramaniam on the elegance of pattern matching in modern Java.
(00:00:07):
This is just a preview of his session that he’s going to be running at Oracle
(00:00:12):
Developer Live Java Innovations,
(00:00:14):
which is in mid-March,
(00:00:15):
a few weeks now,
(00:00:16):
so March 22nd and 24th.
(00:00:19):
It’s going to be a pretty good conference.
(00:00:21):
There’s, I think, a couple dozen speakers there from Oracle, Oracle Java team, as well as the community.
(00:00:29):
There’ll be Bob sessions, hands on labs.
(00:00:32):
And so it should be should be a good time.
(00:00:35):
And I will have the next few weeks.
(00:00:38):
I will have some of these previews with a few of the speakers.
(00:00:42):
So watch out for that.
(00:00:44):
But Venkat’s up first.
(00:00:45):
That’s it.
(00:00:46):
Talk to you soon.
(00:00:53):
Venkat, Venkat Subramaniam, welcome.
(00:00:55):
Welcome back to the program.
(00:00:57):
Good to see you after quite a while.
(00:00:58):
It’s been interesting times and very delighted to see you.
(00:01:02):
Very interesting times indeed.
(00:01:04):
Yes, and we’re back here because we need to talk about Oracle Developer Live.
(00:01:09):
Java Innovations is coming up fast next month in March, mid-March.
(00:01:18):
You’re one of the speakers and I wanted to touch base.
(00:01:20):
You’re doing a session on the elegance of pattern matching in modern Java.
(00:01:26):
I’d love to hear about pattern matching, but I’m especially interested in the elegance of it.
(00:01:31):
Let’s start off with where does it come from?
(00:01:34):
Honestly, pattern matching is something that has intrigued me for a few decades now.
(00:01:39):
It’s been in other languages.
(00:01:41):
I remember spending time being fascinated about it back in time when I used to
(00:01:45):
tinker with the language like Erlang.
(00:01:48):
And then moving forward with even languages like Ruby and other languages,
(00:01:52):
I always go back and look for pattern matching.
(00:01:55):
So it definitely excites me to see that Java now has pattern matching.
(00:02:00):
And part of the reason for that excitement is,
(00:02:03):
again,
(00:02:03):
depending on the language you are working with,
(00:02:06):
it’s a way for you to take a set of data coming into a function and de-sync it apart.
(00:02:12):
It’s a polymorphism in a different way, if you think.
(00:02:15):
Think of this as a marriage between polymorphism and overloading without the ceremony, if you will.
(00:02:22):
So it’s pretty interesting to see how you are taking in a piece of data and saying,
(00:02:26):
well,
(00:02:27):
if it is this kind of data,
(00:02:28):
I want to do this.
(00:02:29):
If it’s a different kind of data, I want to do this.
(00:02:32):
But without being inundated with all these if and else and complexities,
(00:02:40):
that’s where the elegance really comes in.
(00:02:42):
But it’s been around for a long time in other languages.
(00:02:45):
Well, I want to get to that actual point about being around for a long time.
(00:02:49):
So this is really basically a feature to help people read and write code that’s a
(00:02:55):
lot easier in terms of being able to read it after it’s written and being able to
(00:02:59):
actually write it and understand it.
(00:03:02):
And certainly, I mean, it should result in fewer errors and things like that, right?
(00:03:08):
So it’s a simplicity issue.
(00:03:10):
Well, so the code is written once, but it’s rewritten several times over.
(00:03:15):
I’ll give you one example where personally I was pretty intrigued by it.
(00:03:20):
I was implementing a system which deals with payment processing.
(00:03:24):
And when a request comes in, depending on different payment methods, I’ve got to do different things.
(00:03:30):
I’m a big fan of this mantra, make it better real soon.
(00:03:33):
So I write the code, have the test running, the code is working.
(00:03:37):
And when you finish writing it, there’s that sense of unhappiness in you.
(00:03:42):
And you’re like, I wrote the code, but it’s not good enough.
(00:03:47):
And I look at the code and all this if and else statements,
(00:03:50):
and I’m like,
(00:03:50):
if I want to come and make a change to this later on,
(00:03:53):
I’m not going to be smiling at this code.
(00:03:56):
And immediately, I used pattern matching and refactored the code.
(00:04:00):
The tests are still passing.
(00:04:02):
But when I finished refactoring it, it’s removed that clutter, the noise from the code.
(00:04:07):
And a couple of weeks from then,
(00:04:09):
when I came back to modify the code,
(00:04:11):
oh,
(00:04:11):
that was very easy to see where things are.
(00:04:14):
So one thing is about simplicity, but more is code maintainability.
(00:04:19):
you are removing all that noise,
(00:04:21):
you are making it easier for just about anyone,
(00:04:24):
including ourselves,
(00:04:25):
to come back and modify the code.
(00:04:28):
It’s paying forward.
(00:04:29):
You’re doing something a little bit now,
(00:04:31):
so it becomes easier in the long run to come back and change the code.
(00:04:35):
It’s a small step,
(00:04:38):
but it’s got a big payback in terms of the ability to maintain the code in the long run.
(00:04:43):
Right,
(00:04:43):
because it’s not always you maintaining the code,
(00:04:45):
it’s somebody else,
(00:04:46):
and they have to be able to come in and read it and understand it and actually
(00:04:50):
maintain it,
(00:04:51):
right?
(00:04:51):
So,
(00:04:51):
you know,
(00:04:52):
you sort of paint it forward in that way as well with your colleagues or your
(00:04:55):
future colleagues.
(00:04:57):
Or the future yourself, too.
(00:04:58):
Oh, yeah, sure, sure.
(00:05:02):
So, okay.
(00:05:03):
So you mentioned earlier that this is, this is not a new concept.
(00:05:07):
So,
(00:05:07):
so why is it new,
(00:05:09):
you know,
(00:05:10):
coming to Java now,
(00:05:11):
or are they actually being emerging in the last,
(00:05:13):
in the last,
(00:05:14):
you know,
(00:05:15):
last few versions?
(00:05:16):
Yeah, so the emerging part is really intriguing.
(00:05:18):
I have the deepest respect for that.
(00:05:21):
The reason is when you introduce a concept,
(00:05:24):
you know,
(00:05:24):
Java as a language doesn’t have the luxuries a lot of other languages do.
(00:05:30):
And I say this in a good way because
(00:05:33):
The folks behind Java, to the great degree, care about creating things that are sustainable.
(00:05:40):
Backward compatibility is an issue.
(00:05:42):
You introduce a concept, you want to make sure you’re able to build on that concept.
(00:05:47):
You don’t want to come down five years down and saying, oops, we didn’t get that right.
(00:05:52):
Let’s go back and change everything.
(00:05:53):
You don’t have that luxury in the Java ecosystem.
(00:05:57):
And so what they have done is a is a very pragmatic and a and a prudent approach in saying,
(00:06:04):
you know,
(00:06:04):
we are here,
(00:06:05):
we want to get there,
(00:06:07):
but we want to get there incrementally so that we are investing into it,
(00:06:12):
but getting the feedback also to make sure we are heading in the right direction in
(00:06:16):
a way.
(00:06:17):
If you think about it, think of this evolution like I admire a tree that I have in the backyard.
(00:06:24):
And it’s a beautiful tree.
(00:06:25):
I love the tree.
(00:06:26):
And I was explaining to somebody the other day, I saw the tree 14 years ago when it was small.
(00:06:33):
And seeing that grow is heartwarming.
(00:06:37):
In a similar way, you can see this feature grow and blossom, if you will, in that sense.
(00:06:44):
And that makes really good sense.
(00:06:45):
And so they said, hey, we want to really introduce pattern matching.
(00:06:48):
We’re going to have all these capabilities, but where do we start?
(00:06:52):
You can either start by adding a newer syntax to the language entirely, or you can say,
(00:06:58):
This is not something that’s going to be used in isolation.
(00:07:01):
We got existing code that probably is going to transform into this.
(00:07:06):
So why not make use of the switch statement?
(00:07:09):
And so the very, very first step was to turn the switch from being a statement to an expression.
(00:07:16):
In itself, that is a huge benefit, even with
(00:07:20):
without considering pattern matching.
(00:07:22):
So you can see how we are getting the benefits right out of the door.
(00:07:26):
Yes, pattern matching is great.
(00:07:28):
You can reap the benefit over the years.
(00:07:30):
But right now, today, you can benefit from a switch as an expression.
(00:07:36):
It removes mutability from your code in unnecessary places.
(00:07:39):
It makes the code expressive.
(00:07:41):
You are able to build expressions.
(00:07:43):
It becomes easier to test it.
(00:07:45):
It becomes easier to work with it.
(00:07:47):
It puts you in the mindset of,
(00:07:49):
that expression, a switch as an expression, and then incrementally you’re building.
(00:07:54):
So on one hand, you have a switch as an expression.
(00:07:56):
The other hand is a pattern matching with the instance of.
(00:07:59):
And you can see those kind of two lanes merge into this highway of pattern matching.
(00:08:05):
And so you can kind of reap the benefits in both ways.
(00:08:08):
And also, it reduces the learning curve.
(00:08:10):
Hey, I’m used to this now.
(00:08:12):
I’m used to this now.
(00:08:13):
Ah, look, I can mix them together to get to the next level.
(00:08:17):
I love this incremental approach.
(00:08:20):
It is better for the community.
(00:08:22):
It’s better for the ecosystem.
(00:08:24):
It’s better from the technology and the engineering point of view.
(00:08:26):
I mean, the benefit is entirely in each of the sections and then collectively as well.
(00:08:34):
That’s actually what I was going to bring up next in the sense of the iterative process.
(00:08:38):
Developers can become used to the feature over time and it just becomes a part of
(00:08:45):
their normal development.
(00:08:47):
Even though it’s incremental, is it still something that developers will pick up on relatively easy?
(00:08:55):
Is the syntax easy or is it a little bit of a time investment?
(00:09:02):
The syntax is really easy,
(00:09:04):
but what is really even better is I like to call this bite-sized improvements.
(00:09:10):
So you are able to take this little nugget and say,
(00:09:13):
oh,
(00:09:14):
I can actually master this within a few minutes,
(00:09:17):
and then find ways to apply it in so many different places.
(00:09:23):
On one hand,
(00:09:24):
it is easier to embrace,
(00:09:25):
easier to learn,
(00:09:26):
but on the other hand,
(00:09:28):
you’re also dividing your work on your project incrementally as well.
(00:09:33):
You can refactor code in an incremental fashion to reap that benefit,
(00:09:38):
and then over time,
(00:09:39):
you can apply the next benefit.
(00:09:42):
So this also eases not only the pain of learning,
(00:09:45):
it also eases the pain of refactoring as well,
(00:09:48):
because you’re able to reap the benefits of refactoring one step at a time.
(00:09:52):
And where maybe one little concept is useful,
(00:09:54):
you can use that for refactoring,
(00:09:56):
where another concept is useful,
(00:09:57):
you can use that
(00:09:58):
So it’s not like it’s a all or nothing proposition.
(00:10:02):
You can improve in various degrees in various parts of the code as well.
(00:10:07):
And then why not, right?
(00:10:08):
We want to improve the code.
(00:10:10):
It doesn’t have to be that or nothing.
(00:10:12):
So you can benefit in incremental way in that sense as well.
(00:10:16):
Excellent.
(00:10:16):
Well, it sounds really intriguing, your session.
(00:10:20):
I’ll be there.
(00:10:21):
It’s, let’s see, get the dates right here.
(00:10:24):
It’s March 22nd for the Americas and March 24th for Europe, Middle East, Africa, and Asia.
(00:10:31):
And that’s Oracle.
(00:10:32):
Oracle Developer Live, Java Innovations.
(00:10:35):
And Venkat, it was great seeing you.
(00:10:37):
And we’ll have links for everybody to register down below.
(00:10:41):
And hopefully, we’ll see you at a real conference live soon.
(00:10:45):
VENKATESH RAMASWAMY- Can’t wait for that.
(00:10:46):
Thanks, Jim.
(00:10:47):
Always a pleasure talking to you.
(00:10:48):
Thanks.
(00:10:48):
We’ll talk to you soon.
(00:10:49):
Bye.
