TWELVE TIPS FOR WRITING MORE READABLE JAVA CODE With Jeanne Boyarsky & Ken Fogel Slides available after the presentation at https://speakerdeck.com/boyarsky @jeanneboyarsky @omniprof
Who are we Jeanne Boyarsky • Java Champion • Java Author • Developer in NYC • @jeanneboyarsky Ken Fogel §Java Champion §JCP Executive Committee Member §Dawson College Research Scholar in Residence §@omniprof @jeanneboyarsky @omniprof
It’s a bird, it’s a plane, its § A noun § Variables, Enum, Interface, Class, Record § A verb § Methods § All capitals § Constants § Not containing a dollar sign $ § Not an abbreviation, except for maybe WTF § In camel case when a phrase @jeanneboyarsky @omniprof
DON’T BE TOO CLEVER, THE PERSON WHO WILL REPLACE YOU ONCE YOU LEAVE MUST UNDERSTAND YOUR CODE. There is no award for code that works but which no one can figure out how. @jeanneboyarsky @omniprof
public int weird(int number) { var result=0; var random = new Random(); if (number % 2 == 0) { try { while (random.nextInt(3) % 2 != 0) result++; } catch (ArithmeticException e) { result = 0; } } else { result = 1; } return result; } @jeanneboyarsky @omniprof
@jeanneboyarsky @omniprof public class BadPassLine { private static int Bankroll; private static int defaultBank; private static int bet; private static int win = 0; private static int lost = 0; private static int point; public static void main(String[] args){ . . . } public static int gamebegin() { . . . } public static void gamestart(){ . . . } public static int rollingDice(){ . . . } } After 3 Java courses, this is what a student submitted for a PassLine dice game as a warm-up assignment in their 4th Java course with me. Impress me, I said. This student depressed me.
IF IT DON’T FIT THE SCREEN, THEN MAYBE YOUR METHOD IS TOO LONG If it doesn’t fit on a screen, then it's too long. No turning the screen to portrait mode or use 6- point text. @jeanneboyarsky @omniprof
THE MAGIC NUMBER FOR A METHOD IS 1, THE NUMBER OF TASKS THE METHOD CARRIES OUT You may have many responsibilities in your day-to-day life, but your methods should only have a Single Responsibility. @jeanneboyarsky @omniprof
private static void playGame() { System.out.println(); int bet = 0; while (true) { System.out.println("\nYou have $" + currentCash); System.out.println("Insert a bet amount:"); if (reader.hasNextInt()) { bet = reader.nextInt(); if (!(checkIfInvalidBet(bet))) { break; } } else { System.out.println("You can only put whole numbers!"); } } Let’s play How Many Tasks Can You Find In This One Method?
if (bet != 0) { boolean victory; int point = 0; int total; while (true) { total = roll2Dice(); if (point == 0) { if (total == 7 || total == 11) { victory = true; break; } if (total == 2 || total == 3 || total == 12) { victory = false; break; } point = total; System.out.println("\nThe point is now " +point + "\n"); @jeanneboyarsky @omniprof
THE STRING CLASS IS FULL OF POORLY AND BADLY NAMED METHODS. Use methods from a string whose name your boss could even understand. @jeanneboyarsky @omniprof
if (email.contains("@")) { var startOfString = "^"; var anyChars = ".*"; var at = "@"; var regex = startOfString + anyChars + at; System.out.println( email.replaceFirst(regex, "")); } @jeanneboyarsky @omniprof
THE TRADITIONAL SWITCH IS FROM THE 1950S. TIME TO CODE LIKE ITS 2022 Switch expression, Switch statement, Switch pattern matching. It is time to finally say goodbye to fall thru in the absence of a break. @jeanneboyarsky @omniprof
double value = 0; switch (point) { case NORTH: value = 12.12; break; case SOUTH: value = 14.14; break; case EAST: value = 16.16; break; case WEST: value = 18.18; break; } The 1950s just called and they want their switch back. @jeanneboyarsky @omniprof
// switch statement switch (point) { case NORTH -> doNorth(); case SOUTH -> doSouth(); case EAST -> { doEast(); doEastern(); } case WEST -> doWest(); default -> doLost(); }; // switch expression double value = switch (point) { case NORTH -> 12.12; case SOUTH -> 14.14; case EAST -> 16.16; case WEST -> 18.18; default -> 0.0; }; Now’s the time to say goodbye to break. @jeanneboyarsky @omniprof
Object value = 4; switch (value) { case null -> System.out.println("null"); case String s -> System.out.println("String"); case Integer i when i > 1 && i < 6 -> System.out.println("Integer"); default -> System.out.println("Something else"); } The future is the Pattern Matching Switch! @jeanneboyarsky @omniprof
public record Book(String title, String numPages) { } Book book = new Book( "Breaking and entering", 289); System.out.println(book.title()); System.out.println(book.toString()); @jeanneboyarsky @omniprof
Automatically get • final record • private final instance variables • public accessors • constructor taking both fields • equals • hashCode • toString @jeanneboyarsky @omniprof
FOR MORE This deck – on Speakerdeck shortly New Java features - https://speakerdeck.com/boyarsky/2022-java17-refactoring New Java features - https://speakerdeck.com/boyarsky/2022-devnexus-java12-17 @jeanneboyarsky @omniprof