Using a Local Record for Readability

Sometimes there is something we really want to put in our cert book that doesn’t fit. You can only have so many case studies/real world scenarios after all. I rescued this from the trash heap and made this blog post./

Suppose we want to count the number of names that have both a first and last name beginning with E. We want to call the method as:

var names = List.of("Sarah Smith", "Eve Edwards");
System.out.println(local.countNamesStartingWithEs(names));

We can read this with streams:

public long countNamesStartingWithEs(List<String> names) {
   return names.stream()
      .map(t -> t.split(" "))
      .filter(a -> a[0].startsWith("E"))
      .filter(a -> a[1].startsWith("E"))
      .count();
}

The code works. However, it has array positions sprinkled throughout. This requires you to keep remembering the format. We can refactor using a local record:

public long countNamesStartingWithEsWithRecords(List&lt;String> names) {
   record Name(String first, String last) {}

   return names.stream()
      .map(t -> t.split(" "))
      .map(t -> new Name(t[0], t[1]))
      .filter(n -> n.first().startsWith("E"))
      .filter(n -> n.last().startsWith("E"))
      .count();
    }

The record lets us give the fields names so we can reference them as n.first() and n.last(). The only place that has to know about the order is the call record constructor. For such a simple example, the approaches are similar. For longer and more complex pipelines, the local record approach can make the code easier to read.

Presenting vs interacting online

Today I ‘presented” at the NYC Scrum User Group on “Remote Agile Games“. I’ve been avoiding presenting remotely. I did do a panel. Today wasn’t bad. I think the key reasons were

  • It wasn’t a lecture
  • No slides (or code). Just me and the audience.
  • Enough people were on video
  • Part of it was a game (so I wasn’t speaking)
  • The audience participated, shared experiences, etc

All this made it feel like a conversation. Which isn’t draining the way an online presentation is.

Oracle support EOL for Java LTS versions

The current end of life support dates are interesting. I extracted some dates from the official support table to make it easier to see this.

VersionGeneral AvailabilityPRemiereExtended
77/117/197/22
83/143/2210/30
119/189/239/26
179/219/269/29

First of all, extended support for Java 7 is less than a year away. You aren’t still on Java 7, right? 🙂

Second, premiere support for Java 11 ends later than Java 8. However, extended support for Java 8 ends way later. This is interesting. I guess Oracle recognizes it is harder to move off Java 8 than other versions?