Author • Developer in NYC • @jeanneboyarsky Ken Fogel §Java Champion §JCP Executive Committee Member §Dawson College Research Scholar in Residence §@omniprof @jeanneboyarsky @omniprof
§ 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
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.
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
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?
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
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
= 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
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
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
features - https://speakerdeck.com/boyarsky/2022-java17-refactoring New Java features - https://speakerdeck.com/boyarsky/2022-devnexus-java12-17 @jeanneboyarsky @omniprof