Tales from the jar side: Range anxiety, Java, Groovy, and Kotlin
The newsletter is now on Substack, so in addition to the regular content, I added pictures from my Tesla adventure
Welcome to Tales from the jar side, the Kousen IT newsletter, for the week of Jan 19 - 26, 2020. This week I taught an online What’s New In Java course, a private Introduction to Groovy course, and appeared on the Talking Kotlin podcast (thanks Hadi!). I also had a travel adventure with my Tesla, which made for an overly long story at the bottom of this newsletter.
As mentioned in the subtitle, this newsletter is now hosted by Substack, mostly for the reasons described in the last issue. Namely, it’s easier to add images, tweets, YouTube links, and more. If you are an existing subscriber, your email address should already have been ported over to the new system. If you’re not a subscriber and would like to be, the sign-in page is now at here, which also shows the six most recent issues to help you decide.
The only downside I can see so far is that the editor doesn’t give a word count they way Tiny Letter did, and you may have noticed I have a tendency to write long, so sorry about that.
Finally, given the new system, expect me to make some errors for a while as I figure out how to use the new features. Thank you ahead of time for your patience. :)
Java for Java Developers
This week started off promising enough with a What’s New In Java course online at Safari. That’s usually a fun class, with a very receptive audience. I cover the features of Java that have been added since Java 8, and do a quick demo of streams, lambdas, and method references as well.
The class discussion included the usual annoying issues currently surrounding Java, which mostly come from Oracle:
All the FUD (fear, uncertainty, and doubt) around the new licensing model for Java. Yes, Java is still free, but you have to get a version based on OpenJDK to ensure that.
You would think Oracle could simplify that story, but apparently that’s impossible. I suppose I shouldn’t be surprised, given that the company is dominated by its legal department.
Yes, the Java Platform Module System still causes lots of people problems. No, you don’t have to use it. Yes, Eclipse seems to understand it but somehow still doesn’t. No, hardly anyone in the industry, other than library developers, is using it.
The rest of the new features are helpful, though. Collection factory methods, private methods in interfaces, local variable type inference, and so on, are all easy enough to understand and use. We had fun with it.
The producer for the online course added the sign-up link to this newsletter to the class resources, and a handful of people did sign up. To them, I want to let you know that I added a resource to one of the two the GitHub repositories associated with the class, namely this one. During class I showed a Java 8 example where I use streams, lambdas, method references, and static and default methods in interfaces to find the ten longest words in the dictionary. Part of the code looks like this:
public void printTenLongestWords() {
System.out.println("\nTen Longest Words:");
try (Stream<String> lines = Files.lines(dictionary)) {
lines.filter(s -> s.length() > 20)
.sorted(Comparator.comparingInt(String::length)
.reversed()
.limit(10)
.forEach(w ->
System.out.printf("%s (%d)%n", w, w.length()));
} catch (IOException e) {
e.printStackTrace();
}
}
The dictionary consists of a file called “words”. This file is located on any macOS system (and any operating system ultimately based on BSD). It is a list of about 235 thousand words, one per line, all from Webster’s Second International Dictionary, copyright 1934. One of the reasons I didn’t have an explicit test for that class is that I couldn’t rely on that file being on the client machine (which isn’t really a good reason, since I could use any arbitrary file).
Just to make the process easier, though, after class I decided to add that dictionary to the Java 8 GitHub repository for my Modern Java Recipes book. If you look in the src/main/resources
folder now, you’ll see a directory called dict that includes everything, including the README file that explains how the copyright has expired. Now anyone can run the example locally, even if you’re on Windows.
I also added a test case. I believe all the tests have been ported to JUnit 5 by now, which I really like.
After the class was over, it was time to go on my long trip to teach a Groovy training class in State College, PA. State College is in the geographical center of the state, and there’s no good way to get to it by air. I figured it was worth a drive, even if at best it was going to be about six hours.
Before I get to that, let me cover a couple of other events.
Talking Kotlin with Hadi Harriri
Back in October, I was in the final stages of completing my Kotlin Cookbook when Hadi Harriri was kind enough to invite me on the Talking Kotlin podcast. The direct link to the podcast page is this one, but one of the features of Substack is that you can embed SoundCloud links, like this:
That’s pretty cool, though he unfortunately grabbed an older picture. You can click on the link embedded here to listen to it, or you can go to the main Talking Kotlin website, or you can subscribe in the usual way using your regular podcast player. It’s all good.
Talking Kotlin is posted twice a month, which is the reason for the delay in publishing until now. Still, that puts him way ahead of my Groovy Podcast. We do hope to publish a new one of those this coming week.
Speaking of my Kotlin Cookbook book, this week it is part of the book promotion schedule at Java Ranch. That means I will be answering questions about it in the Kotlin forum there from Tuesday through Friday, and if you ask a question you will be eligible for one of several copies that will be given away at the end of the week. Think of that as a reddit AMA if you like, though I expect more questions about the book and the language than about me.
Groovy for Non-Java Developers
Groovy is an alternative language for the Java Virtual Machine. It’s arguably the most mature and complete of the non-Java JVM languages, but since it comes from the Java world, most people learning it have a Java background. That’s helpful, because Groovy is unique in that it wasn’t designed to replace the language that came before it (Java), it was designed to make it better. Heck, I wrote a whole book about that, called Making Java Groovy.
Teaching Groovy to existing Java developers is one of my favorite things to do, because they’re always so happy to see how much simpler it makes their lives. They also already know the Java libraries, which are also used, and augmented, by Groovy. Last week the 97 Things group at O’Reilly Media published my Medium post about that, entitled Make Your Java Groovier.
A fair number of developers come to Groovy without the prior Java background, however. There are a few products in the marketplace that support Groovy scripts, and often developer from other backgrounds need to learn how to write Groovy to support those. That’s not nearly as much fun to teach, however, because they discover that in addition to the Groovy language and libraries, they’re probably going to have to pick up a decent amount of Java, too.
I taught one of those classes this week. Fortunately, Groovy is sufficiently clear and intuitive that most developers like it anyway, even if they’re coming from a non-object-oriented background. The students seemed to enjoy the class (or, at least, didn’t actively dislike it, which is the low bar I’ll settle for). It also gave me a chance to update my IntroGroovy GitHub repository to the latest release version of both Groovy (2.5.9) and Gradle (6.1).
If you decide to check out that repository, you should know that the Groovy script that downloads cat pictures from Flickr and renders them inside a simple Swing GUI is here.
By the way, if you’ve ever wondered about the economics of technical book publishing, I recently received a royalty check for that book. Manning pays royalties quarterly, one quarter behind, so the check was for the Fall of 2019. That quarter book yielded $49.56. I suppose that’s good for a book published back in 2013, but I don’t think I’ll be giving up my day job just yet.
The report also says that the book has sold 3964 copies during its publishing life, so maybe someday it’ll hit 4000. We’ll see.
Fun With Range Anxiety
Long time readers of this newsletter (thank you for that) will know that in August of 2018 I purchased a Tesla Model 3. It truly is an awesome car. Sometimes I’ll just go for a drive in it, which my wife refers to as Tesla Therapy. I don’t talk about the car much here, because it’s not normally relevant to my professional life.
This week was the exception. Google estimated the my trip to central Pennsylvania would require around 350 miles. When my car was new, its maximum range was about 300 miles, and now it’s closer to about 270. That meant I was going to have to charge it along the way.
I also learned last year that the battery really, really doesn’t like cold weather. I lose a hefty percentage (how much is hard to estimate) as the temperature drops, and during my trip the high was going to be in the 20s (Fahrenheit, or about -5 or -6 C).
To give you an idea what the dashboard screen looks like when the battery is low, see the following picture and note (1) the little mileage indicator next to the battery, and (2) the temperature in the upper right.
That’s pretty low, but I’ve seen worse
I’ll come back to that picture, but at least you can see the numbers.
I took what turned out to be the bold move of letting the car plan the trip for me. In the Tesla navigation app, if the car detects you will be going beyond its existing range, it can automatically add supercharger stops along the way.
The car decided that my trip should go through the supercharger station in Tannersville, PA. You can see from the map below that the charging station looks to be right along the way. (Note I had to reconstruct this path from Google Maps — some details may not be right).
Seems reasonable, right?
Google Maps estimated the distance to the supercharger to be a bit over 210 miles, which should be fine, given that I started out with over 260.
Well, not so much. For one thing, it was very cold outside. For another, the battery estimates are notoriously off, at least for me. For another thing, it was cold outside.
Oh, and did I mention it was cold outside?
Another problem that I realized only in retrospect is that apparently the battery usage rate goes up the faster you drive. I suspected that, but hadn’t really experienced it directly before. I wasn’t going all that fast, but I wasn’t crawling along, either.
What’s hard to see from the map is the turn south off of I84 West. The car already estimated that I would have about 8% battery left when I reached Tannersville, which meant I’d have about 15 to 20 miles left (whoa — I didn’t do that calculation until now). In other words, I really couldn’t afford to miss that exit, because if I missed it, I’d have to go to the next one and come back.
Somehow, despite watching for it for over 20 miles, I missed it. Ugh. Somehow I wound up in the wrong lane as the exit came up and couldn’t get over to it in time. That cost me at least 8 to 10 miles I didn’t have. At the next exit I pulled over and did the search again, and once again the car suggested the same stop. That stop was around 40 miles away, but the car estimated I had 50 or so available, so that seemed like enough. The revised estimate was that I’d have 2% battery when I arrived.
Yikes, but — and this is a recurring theme in this story — where else was I to go? It’s not like I could pop around to a local station and be on my way.
(Again, in retrospect I should have searched for local “destination” chargers, which need an adapter and are much slower, but would have worked. They very well may have existed, but I didn’t look for them.)
I headed for the supercharger, keeping an eye on the remaining mileage from the navigation app and the battery mileage estimate. The gap between them started out around 12 miles, and steadily eroded as I went along.
When I had 30 miles to go, the gap was only 10. When I had 20 miles to go, it was down to 8. At 10 miles left, it was at 6, and I was starting to get scared.
At 6 miles left, the gap was only 4. Then, at 3.5 miles left, the battery estimate was suddenly 3, meaning I didn’t have enough charge to get there.
Yikes, again. In a normal car, if you run out of gas, you get a lift to a gas station, buy a couple of gallons, go back to your car, and then drive it to the station and you’re all set. What was the charging equivalent? Get a tow to the supercharger? It’s not like I was going to find a closer charger than 3.5 miles away. So I kept going, and the numbers kept dropping.
Finally, at 1.5 miles from the charger, the battery estimate hit zero. That’s zero, zip, nada. Now what? I’m not ashamed to say I said a little prayer, kept going, and watched for places to pull off to the side of the road when the car died.
I also remembered that every gas-powered car I’ve ever owned could drive at least 10 miles even when the gas gauge was sitting below empty. Maybe the battery would do the same. After all, I only needed another mile or so.
Amazingly enough, I found the exit, rounded the bend, and lo and behold there was the supercharger, at the side of an outlet mall. As a final indignity, you couldn’t get to it unless you went through the main entrance and drove all the way around to the back, but somehow the car made it.
When I finally parked, I got this message:
Yeah, okay. Let’s do that.
It took about 45 minutes to charge it up to full again. I went and found a restroom and some dinner while that was going on, so effectively I charged myself, too.
I actually forgot to take a picture of the battery at zero. You could almost hear the car sigh in relief, though, when I hooked it up to the juice, or maybe that was just me.
When the car was full I continued my trip. By the time I got to the hotel I was down to around 35 miles again, so after checking in I decided to find a “local” supercharger and charge it up again. The picture I included above is from when I hooked up to the charger in Bellefonte, PA, about 11 miles from State College.
For the record, this was what it looks like when you’re done. Best $9.75 I ever spent.
This is what it’s supposed to look like
For the ride home, life was considerably less dramatic. The car suggested two supercharger stops rather than one, and that made a big difference. In neither case did I drop below 25 miles, though I admit I only had about 12 miles left when I reached home. The only downside was that I drove home in a driving rainstorm, but at least I made it.
So what did I learn from this teachable moment?
Empty doesn’t mean actually empty, and thank goodness for that
My car’s battery really, really hates cold weather
Even with the extra charging, the result was still less money than what I would have paid for a tank of gas and way less than what I would have paid to fly
It’s all good.
Last week:
Taught What’s New In Java online at Safari
Taught a private Introduction to Groovy course
The Talking Kotlin podcast I appeared on was released
Had a bit of a Tesla adventure
Next week:
Teach Kotlin Fundamentals online at Safari
Teach Spring and Spring Boot online at Safari
My Kotlin Cookbook is featured on Java Ranch, so I will answer questions from Tues - Fri in the Kotlin forum. That should be fun :)
Really, really overdue for a Groovy Podcast, so hopefully will do one