Tales from the jar side: Java for beginners, The Caine Mutiny, and Constructive Loyalty
"Ahh, but the strawberries! That's where I had them. They laughed at me and made jokes, but I proved beyond the shadow of a doubt and with geometric logic that a duplicate key definitely did exist!"
Welcome to Tales from the jar side, the Kousen IT newsletter, for the week of May 10 - 17, 2020. This week I taught the Introduction to Gradle course offered by Gradle, Inc, my Managing Your Manager course online on the O’Reilly Learning Platform, and wrote a lot more of my Managing Your Manager book.
I also helped my nephew with his homework, which is where I’ll start.
Arrays and Streams and Lambdas, Oh My!
My sister’s son is a high school student taking a computer science course, and the programming language they’re using, as it turns out, is Java. Of course, that’s my jam. But we’re in a quarantine now, so he’s both learning remotely and no longer has access to the machines where they have Java installed and configured.
For that problem at least, I had a solution. I sent him to the online Groovy Web Console. One of the biggest features of Groovy is that you can take almost any Java program and rename it with a groovy extension, and it will probably compile. With the release of Groovy 3.0, that works for almost everything.
I ran the following script in the console, however:
println System.getProperty("java.version")
println GroovySystem.version
The result is that the console was running Java 1.8.0 with Groovy 2.5.7, which is a bit dated but certainly good enough for what we had to do. So at least I didn’t have to figure out a way for him to install Java on his school-supplied Chromebook, even assuming he had permission to install anything anyway.
Before you wonder about this, let me say that his teacher already knew he had a family member that spoke Java, and he was definitely going to say he had help with his homework, though I imagine that will be pretty obvious anyway. The hard part for me, though, is that while his assignment was understandable, it was seriously out of date with the way Java is written now.
I asked him, “Have you talked about streams or lambdas at all?”
“What are those?” he replied, so I knew where we stood.
Let me give you an idea of the dialog, where most of mine was internal. (I’ll show my internal dialog in parentheses below.)
I certainly didn’t want to give him a hard time. He did remarkably well, all things considered. I didn’t want to give the teacher a hard time, either. Having kids at home is reminding everyone of just how hard teachers work to teach anything at all. Frankly, if my son had been a student during this time, we would have auctioned him off on eBay long ago.
(For the record, my son is a good kid with a heart of gold, but he has always been an “indifferent” student, and the last person he ever wanted to learn anything from directly was me. Every time I got to thinking I was a good instructor, all I had to do was try to teach him something. It was always a lesson in humility — for me. At least in this case I knew my nephew was willing to listen.)
Anyway, the assignment asked him to create an array of ten integers. Save them in a field called myArray.
(Why an array and not an ArrayList? Is there a reason? As it turned out, nope.)
Create static methods (all static? Sigh) to:
populate the array with random integers between 1 and 100,
print the elements in order,
print the even elements in order,
print the elements in reverse order, and
print just the first and last values.
(Yikes, every method returned void. You know why we don’t write methods that return void? Because they’re really hard to test. Oh well.)
(Oh, and Math.random() returns a uniformly distributed pseudorandom double between 0 and 1, so we have to shift and cast. Wait, the assignment gave an example with the java.util.Random class, so we can instantiate it and call rand.nextInt(100) and add one, but we then need an import statement.)
Each of the requested methods involved writing a for loop that iterated over all the elements, one time in reverse order, and once with an if statement.
(I was dying to do this with streams, lambdas, and method references. That’s what we’re supposed to do now, right? Heck, I wrote a whole book on the subject:
It’s called Modern Java Recipes, available wherever fine technical books are sold. I teach classes on this stuff to working professionals, and when those classes are held on the O’Reilly Learning Platform, I still get a huge turnout even though Java 8 came out over four years ago.)
(On the other hand, I should stop that right now. It will only confuse and frustrate the boy, not to mention the teacher.)
(But can’t I at least separate the printing from the iteration? That’s good practice, and would make the results testable. And what’s up with all those static methods? That’s hardly object oriented, though I guess you could argue this was a utility class.)
I finally was reminded of one of the fundamental facts about instructors. There are three stages to the development of an instructor:
In Stage 1, you tell the students everything you know.
In Stage 2, you tell them everything you’ve learned since then.
Finally, if you make it to Stage 3, you tell them only what they need to know, because it’s about them, not you.
I resisted the temptation to go beyond what the assignment wanted. I walked him through the process, making sure that he understood what was going on, so if he’s asked about it, he’ll be okay. I hope. I expect I saved him a few thousand hours of debugging anyway.
By the way, you want to see how to reverse the elements of a stream of integers, then print the results as a space-separated string? How about this lunacy:
public void printElementsInReverseOrder() {
System.out.println(Arrays.stream(myArray)
.collect(ArrayDeque::new,
ArrayDeque::addFirst,
ArrayDeque::addAll)
.stream()
.map(Objects::toString)
.collect(Collectors.joining(" ")));
}
I used an ArrayDeque because you can reverse the stream by adding each element to the front of the data structure. Then I needed a collector to populate the deque, and then created a new stream so I could convert each element into a string and join them with a single space. Hey look, no local variables! Should even work in parallel!
(That wouldn’t work in the Groovy console though, because the online console didn’t support Groovy 3, so I couldn’t use the Java syntax for method references. Still, it was an interesting exercise. For me, that is; not for a teenager just trying to get through this mess.)
Finally, I should mention that as someone who has been teaching training classes for over 20 years, let me say that Introduction to Java is still one of the hardest courses to teach. That’s because in addition to teaching the basic syntax and semantics, you also need to teach how to think like an object-oriented developer. While you can learn syntax just by drill and semantics by studying examples, the last part is what you really need a teacher for. That’s without mixing in the functional parts, too.
I can’t imagine asking high school students to overcome that hurdle on their own, in the middle of a pandemic, when their real goal is to finish this assignment so they can do literally anything else.
Thank goodness for the online Groovy console, though. That reminds me that this coming week I’m teaching a No Fluff, Just Stuff virtual workshop called Groovy for Java Developers.
It’s been a while since I got to do that, and this will be my first chance to use Groovy 3 in anger. I’m really looking forward to it.
The Caine Mutiny
I spent a lot of time this week working on my Managing Your Manager book, which was convenient because I was also teaching the training class of the same name on the O’Reilly Learning Platform. That reminded me that the movie The Caine Mutiny forms the basis of what I teach about relating to management. It’s not too strong to say that movie changed my life.
I was going to describe it here, but I think I’ll hold off on the spoilers for now. I’ll mention that Humphrey Bogart plays Captain Queeg (and got nominated for Best Actor as a result), José Ferrer is phenomenal as the defense attorney, Van Johnson is great as the executive officer, and Fred MacMurray is completely different from any other role I’ve seen him play. It was the second biggest grossing movie of 1954 and garnered seven Academy Award nominations.
Seriously, see it if you can. It’s great up until the last scene, and then the last scene turns it into a classic.
In the book, I make it the foundation of the loyalty discussion. My wife and I watched it again this week (she hadn’t seen it in years; I basically have it memorized but I still love seeing it again). We rented it on Amazon Video, which was a first. Usually I stick with the movies that come with the subscription, but this one was worth it.
During the movie I suddenly realized that the term “constructive loyalty”, which I use over and over in the book, comes directly out of the movie.
I’ll mention in passing that the other subplot, involving the mother-dominated Ensign Keith struggling to build a relationship with his girlfriend, also had a certain resonance in my life years ago, but that’s not relevant to anything management related. At least I don’t believe so. I might have to rethink that.
A couple miscellaneous notes about the movie:
The actor Michael Caine took his last name as a stage name from that movie.
Vince Gilligan (of Breaking Bad and Better Call Saul fame) says that’s one of his favorite movies and has a couple of homages to it in his shows.
I read the book by Herman Wouk as well, and this is one of those rare cases where the movie is better than the book. (The Godfather is another example.)
The Wikipedia article claims that the movie and book it was based on influenced the drafting of the 25th Amendment to the Constitution, which sets forth the conditions on removing a President from office. Just saying.
I hit a mental milestone in writing the book this week, when my current draft passed 100 pages for the first time. That number will fluctuate for a while, and I expect the final book will be longer but still under 150 pages. In a way that’s depressing, because I’ve lived with the subject matter for years, given presentations on it dozens of times, taught many training classes on the same material, and somehow it’s all going to fit in a really short book. The readers will probably be happy about that, however.
Last week:
Introduction to Gradle online for Gradle, Inc
Managing Your Manager, online on the O’Reilly Learning Platform
Wrote more of the Managing Your Manager book, to appear from the Pragmatic Programmers
Next week:
Next Generation Java Testing with JUnit 5 course on the O’Reilly Learning Platform
Deep Dive Into Spring and Spring Boot, online as an NFJS Virtual Workshop. Spaces still available.
Groovy for Java Developers, online as an NFJS Virtual Workshop. Spaces still available.