Tales from the jar side: A Local Variable Type Inference Joke, An obscene chess follow-up, the Wordle disaster, Saturn destroyed a moon, and other Tweets
You know what the first rule of passive-aggressive club is? You know what -- never mind. Just forget it.
Welcome, fellow jarheads, to Tales from the jar side, the Kousen IT newsletter, for the week of September 11 - 18, 2022. This week I taught NFJS Virtual workshops on Reactive Spring and Gradle Fundamentals, and O’Reilly Learning Platform courses in Latest Features in Java and Testing with JUnit 5.
Regular readers of this newsletter are affectionately known as jarheads, and are far more intelligent, sophisticated, and attractive than the average newsletter reader. If you wish to become a jarhead, please subscribe using this button:
As a reminder, when this message is truncated in email, click on the title at the top to open it in a browser tab formatted properly for both the web and mobile.
A Long Way To Go For An LVTI Joke
As a freshman undergrad, I was assigned to a dorm room with two other guys. One day shortly after we arrived, two sophomores came into our room, mostly just to say hi, but also to answer questions.
That surprised us and struck us as almost suspiciously friendly, but they turned out to be genuine and wanted to fill us in on the history of our living group, known as Desmond House (named after Thomas C. Desmond, class of '09). While they talked, they would frequently make references to events that happened before we joined. That was pretty inevitable, given that we’d only been there for about a week. Each time it happened, however, one of them (I think his name was David, but everybody knew him as Burnout, after a kitchen accident the year before. His friend and partner in crime was known as Pyro for the same incident) did something that has remained with me to this day.
Every time they referred to something we didn’t know, Burnout would pause and take a moment to explain the reference to us. He did this over and over again, without acting either bored or condescending, as though helping us keep up was the most natural thing in the world. More than anything he actually said, that act felt extraordinarily generous to me, and made me feel both welcomed and included. I always remembered that feeling, and that’s why whenever I make some joke or reference in this newsletter or in one of my classes, I always try to explain it, just in case someone doesn’t get the connection. It’s easy enough to do, and it really does matter. In a way, every time I explain something like that, I feel like I’m honoring the extra effort Burnout took with us all those years ago.
All this is a way of saying I’m about to explain what is arguably a pretty weak joke that came up in one of my classes this week. I know if you have to explain a joke it’s not funny, but I’ll just admit up front the joke isn’t all that great and go forward anyway, for Burnout. Of course, he probably wouldn’t want to be honored by a bad joke, but if you don’t like it, feel free to blame him anyway.
This week I taught a new class on the O’Reilly Learning Platform called Making New Java Features Work For You, which was a rewrite of my previous course on the features that have been added to Java since version 1.8. There are a lot of them. None are terribly dramatic, but collectively they add up and change the way we write Java code.
One of those features is called Local Variable Type Inference, abbreviated LVTI. The specification can be found as Java Enhancement Proposal (JEP) number 286, and was added as the big new feature of Java 10, released in March 2018. I usually describe LVTI as much less than meets the eye, because it looks like a more dramatic change to the language than it actually is.
Here’s an example from the JEP:
The new item is the “var
” word on the left. In most Java code, for instead of writing “var list
” you would write “ArrayList<String> list
”, and instead of “var stream
” you’d write “Stream<String> stream
”.
The part that catches everyone’s attention (at least if they’re Java developers) is that JavaScript also has a keyword called “var
”, and it’s used to indicate that we’re using a variable whose type is inferred at runtime, because all types in JavaScript are determined at runtime. Java developers seeing this for the first time therefore get either very excited or very worried, wondering if Java has suddenly adopted runtime (i.e., dynamic) typing, rather than compile time (i.e., static) typing, which would fundamentally change how the language works.
That’s not the case. If you use var
, the type still must be determined by the compiler at compile time, and if the compiler can’t figure it out, you get an error. For example, if you write:
then the line declaring y
doesn’t even compile, because even though you’re setting it to null
, Java needs to know what kind of null it is (which is quite an existential question when you think about it, but that’s a topic for another time). The other two lines do compile, because the cast in front of the null
lets Java know what type of variable they are, even if their values are null
.
This var
is not a keyword, fortunately. Since the word var
is such an obvious abbreviation for “variable”, sooner or later pretty much every developer uses var
as a variable name. If Java suddenly made it a keyword, it would break everybody’s code. No, var
is what Java calls a restricted type identifier, which means it can only be used in special cases. Actually, there’s only one special case: it can only be used as a modifier for a local variable (a variable defined inside a method) — thus the name local variable type inference.
Since you can only use var
on local variables — no fields, no return types, no method parameters, no nothing else — this makes LVTI almost, but not quite, completely useless. It feels like the ultimate in passive-aggressive features. It’s like the maintainers of the language are saying, “you kept complaining about how verbose Java is, so we’ve added a new feature that lets you eliminate exactly ONE declaration, but only in the ONE place we allow you to use it. Happy now? Geez, you’re never satisfied.”
(If you’re still waiting for my bad joke, it’s coming. We’re not quite there yet.)
I was really surprised at how many Java developers were excited when LVTI was released. I kept thinking, really? You know how limited this is, right? And that it’s going to confuse as many people as it helps? But here we are.
There is one use case where LVTI helps, at least somewhat, and that’s when you have a variable that involves nested generic types. Say I make a Java Map
where the keys are strings and the values are lists of integers. Then if I try to iterate over the map using an iterator, the code looks like:
You could replace the var
on line one with Map<String, List<Integer>>
, but the real benefit is the var
on line 7, where var
would have be replaced with (I kid you not):
Map.Entry<String, List<Integer>>
That’s a lot of syntax, especially when all I want is an iterator and I’m only going to call getKey()
and getValue()
on it anyway. (The situation is even uglier with bounded wildcards, but I won’t get into that here.)
And now, at long last, we get to the gag. If you can use var
both as a type identifier and as a variable name, my sense of humor means I can’t resist defining a record like:
record Var(String var) { }
(Java records are data holders that are immutable and automatically generate toString
, equals
, and hashCode
methods. They were added in Java 16 and we talk about them in class.)
I can’t make the record name var
(because var
can’t be used as a type identifier) and I can’t replace the String
declaration with var
(because fields need a defined class), but I can write the following test:
Lines 7 and 8 are the joke. This compiles and runs, and even passes the test. Ha, and I repeat, ha. Feel free to use that in your own code, though I guarantee it will come up on a code review.
I’ve been using this, or something similar, in my related presentations since Java 10 came out (Java 19 is coming this week), and I never expected much in the way of laughter. For some reason, however, this struck a chord in the students in my class this week, so I thought I’d include it here. If you like it, great! If not, as I said, blame Burnout.
Chess Follow-Up You Probably Should Skip
Last week I wrote about the controversy that arose in the chess world when World Champion Magnus Carlsen withdrew from the Sinquefield Cup after losing to 19-year-old Hans Moke Niemann, implicitly accusing him of cheating. The controversy went viral, in that it burst out of the chess world into the wider media at large, including articles in Bloomberg, NPR, the Wall Street Journal, the New York Times, and more.
There was an additional reason the event made the news, however, and I deliberately left it out of my summary last week, because eww. But you probably deserve to know. At least it might save you from encountering it unexpectedly.
While cheating in online games is not unknown, and some people even argue it is somewhat common (although not at the so-called Super Grandmaster level), cheating in live games is much harder to do. Many people speculated about how it might be done, but the most absurd suggestion was offered by a streamer who goes by the handle ChessBrah (seriously). ChessBrah is the online Twitch and YouTube name for Canadian GM Eric Hansen, one of the most popular chess streamers in the world.
In an offhand remark, Hansen wondered aloud if live cheating could be done using anal beads. Yes, you read that correctly. Anal beads. He didn’t say how, exactly, and he meant it as a joke, but it was exactly the sort of joke that the popular media would deliberately misinterpret as serious just to get clicks.
Those clicks were more than forthcoming. Here’s an abbreviated list of resulting articles:
Chess grandmaster denies cheating by using anal beads (Euronews)
Chess player denies using anal beads to beat world champion (Pinknews)
Did chess grandmaster use anal beads to beat world No.1 (Daily Mail)
Chess and vibrating anal beads? (Deadspin)
Vibrating Butt Toys Are Exactly What Chess Needs (Rolling Stone)
Social Media Explodes After Chess Player Uses Anal Beads (The Blast)
The Chess Grandmaster Anal Bead Conspiracy, Explained (Kotaku)
Anal Beads Conspiracy Theory Rocks Chess World (Yahoo! News)
Chess player offered to clear his name by playing nude (AV Club)
Click on any of those links at your peril. I could go on and on. Freakin’ Elon Musk retweeted the accusation, because that totally fits his rather demented sense of humor, and that really spread (sorry) the idea. Suffice it to say Hansen issued an explanatory video right away saying he was joking, but Niemann still had to answer some rather awkward questions this week. He did receive an offer from a porn site to play a match naked for 1 million dollars, but there’s no word on whether he considered it or not.
For the record, I never have, nor will I ever … finish that sentence. Still, I thought you might like to know about this, since I left it out of my summary last week. I buried the lede, as it were, which is as close as I’m going to come to making a related joke. For the love of all that is holy, let’s move on.
Other Tweets and Links
The Great Wordle Fiasco
On Friday, the Wordle word was almost impossible to guess, and most people didn’t:
I wasn’t one of those lucky people. My win streak ended at 63 in a row, which I’m disappointed about but not nearly as angry as a lot of people. One in particular was motivated to write a highly entertaining article in one of my favorite online publications, The Defector.
Since Wordle words are only good for a single day, I guess there’s no harm in me telling you last Friday’s word. The word was: PARER. The problem is that if you were solving the puzzle and narrowed down the pattern to PA_ER, as many people did (me included), you would probably guess PAGER, PAPER, PACER, PALER, PAYER, and even PAVER before PARER. There’s no way to distinguish between any of them ahead of time, so unless you got really lucky, you were doomed.
Oh well. My new streak now stands at 2.
Seriously Dated, But Still Funny
Now try to get that Madonna song out of your head. Sadly, I now have it going through my head, but such are the sacrifices demanded by my Art.
Baseball Is A Team Game
The thing about baseball is that one or two players can’t change everything, because you can pitch and/or hit around them. Your best hitter is only going to get four or five at-bats during a typical game, and if he’s too good you can just walk him. A great pitcher can dominate, but probably only for about five to seven innings every fifth day. The Angels are a classic example of this right now, with two of the most brilliant baseball players of all time in Mike Trout and Shohei Ohtani, and they’re still consistently losing, which led to this brilliant tweet about a year ago:
More and more of my favorite baseball writers now refer to the fictional “Tungsten Arm” O’Doyle on a regular basis, and this tweet has never been more applicable than this year, so keep an eye out for it.
GoTo Podcast
An interview I did last summer (!) with Trisha Gee about my book Help Your Boss Help You was finally released this week:
Your boss is not your friend. I harp on this a lot, mostly because I fell into that trap throughout the early part of my career. Enjoy.
Saturn’s Ripped Apart Moon
According to the article, Saturn had an additional moon of moderate size, and between 100 and 120 million years ago, its chaotic orbit took it too close to the planet, which ripped it apart, creating the ring system we know today. That means if you were a Cretaceous-era Tyrannosaurus rex, you could have used a decent pair of binoculars to see the rings, assuming you could find a way to hold them up to your face.
On the other hand, if you were a Jurassic-era Plesiosaur, all you would see is a planet like any other.
(I really feel that given the chess-related item above, there ought to be a “moon” joke in there somewhere, but I can’t find it. That’s probably a good thing.)
This Newsletter Is Already Edgy, So
Finally, there’s this:
Have a good week, everybody. :)
(Thanks to my friend Bill Fly for the link to https://instantrimshot.com/ for the rimshot added to the audio recording.)
As a reminder, you can see all my upcoming training courses on the O’Reilly Learning Platform here and all the upcoming NFJS Virtual Workshops here.
Last week:
Reactive Spring, an NFJS Virtual Workshop
Making New Java Features Work For You, on the O’Reilly Learning Platform
Gradle Concepts and Best Practices, an NFJS Virtual Workshop
This week:
New England Software Symposium, the NFJS live event in Wakefield, MA, Friday - Sunday. You can attend in-person or online. :)
Thank you.
Thanks for the acknowledgement.