Upgrade to Pro — share decks privately, control downloads, hide ads and more …

2022-java17-refactoring.pdf

 2022-java17-refactoring.pdf

Jeanne Boyarsky

August 08, 2022
Tweet

More Decks by Jeanne Boyarsky

Other Decks in Programming

Transcript

  1. @jeanneboyarsky 1 Refactoring to Java 17 and beyond Jeanne Boyarsky

    August 8, 2022 KCDC speakerdeck.com/boyarsky
  2. @jeanneboyarsky Version of Java? 8 <11 11 <16 17 Targets

    12-17. “older Java comments” Align code to future. “older Java comments” Upgrade to LTS or latest Lots of refactoring Even more refactoring
  3. @jeanneboyarsky For each Topic • Example • About the feature

    • Opportunities • IDE Support • What to do if on older Java • What will be explored in more detail in the lab version Wednesday…. 9
  4. @jeanneboyarsky Example: REST API Params public String getJson(String search) {

    String json = "{" + " \"query\": \"%s\"" + " \"start\": \"1\"," + " \"end\": \"10\"" + "}"; return String.format(json, search); } 12 This is hard to read
  5. @jeanneboyarsky Take Two public String getJson(String search) { Path path

    = Path.of( “src/main/resources/query.json"); String json = null; try { json = Files.readString(path); } catch (IOException e) { throw new UncheckedIOException(e); } return String.format(json, search); } 13 Now the String is far away
  6. @jeanneboyarsky Text Block public String getJson(String search) { String json

    = """ { "query": "%s" "start": "1" "end": "10" }"""; return String.format(json, search); } 14 It’s a string literal! 15 Adds line breaks, but still works
  7. @jeanneboyarsky Text Block Syntax String textBlock = """ kcdc,Kansas City,"session,workshop"

    meetup,Various,lecture """; incidental whitespace start block end block 15 15
  8. @jeanneboyarsky Essential Whitespace String textBlock = """ <session> <speaker> Jeanne

    Boyarsky </speaker> </session> """; incidental whitespace essential whitespace 15 16
  9. @jeanneboyarsky Ending lines String textBlock = """ <session> <speaker> Jeanne

    Boyarsky \s </speaker> <title> Becoming one of the first Java 17 \ certified programmers \ (and learning new features) </title> </session> """; continue on next line without a line break new escape character keeps trailing whitespace tab 15 17
  10. @jeanneboyarsky New lines String textBlock = """ <session>\n <speaker> Jeanne\nBoyarsky

    </speaker> </session>"""; no line break at end Two new lines (explicit and implicit) One new line (explicit) 15 18
  11. @jeanneboyarsky Opportunities •Externalized data •Expected values in JUnit •Formats -

    CSV, GraphQL, JSON, SQL, Text, XML, YAML, etc •Others? 15 20
  12. @jeanneboyarsky IDE Support 21 String json = """ { "query":

    "%s" "start": "1", "end": "10"}"""; Literal refactoring - no \n
  13. @jeanneboyarsky IDE Support 22 String json = """ {\ "query":

    "%s"\ "start": "1",\ "end": "10"\ }"""; Preserve lines but still no \n
  14. @jeanneboyarsky On older Java? public String getJson(String search) { //TODO

    convert to text block when on Java 17 String json = "{" + " \"query\": \"%s\"" + " \"start\": \"1\"," + " \"end\": \"10\"" + "}"; return String.format(json, search); } 23 Hard to read but positions for future
  15. @jeanneboyarsky Casting if (num instanceof Integer) { Integer numAsInt =

    (Integer) num; System.out.println(numAsInt); } if (num instanceof Double) { Double numAsDouble = (Double) num; System.out.println(numAsDouble.intValue()); } 26
  16. @jeanneboyarsky Casting 16 if (num instanceof Integer numAsInt) { System.out.println(numAsInt);

    } if (num instanceof Double numAsDouble) { System.out.println(numAsDouble.intValue()); } Pattern variable 27
  17. @jeanneboyarsky Flow Scope 16 if (num instanceof Double d1 &&

    d1.intValue() % 2 == 0) { System.out.println(d1.intValue()); } if (num instanceof Double d2 || d2.intValue() % 2 == 0) { System.out.println(d2.intValue()); } Does not compile because d2 might not be double Compiles 28
  18. @jeanneboyarsky Does this compile? 16 if (num instanceof Double n)

    System.out.println(n.intValue()); if (num instanceof Integer n) System.out.println(n); Yes. Only in scope for if statement 29
  19. @jeanneboyarsky Does this compile? 16 if (num instanceof Double n)

    System.out.println(n.intValue()); System.out.println(n.intValue()); No. If statement is over 30
  20. @jeanneboyarsky Does this compile? 16 if (!(num instanceof Double n))

    { return; } System.out.println(n.intValue()); Yes. Returns early so rest is like an else 31
  21. @jeanneboyarsky Does this compile? 16 if (!(num instanceof Double n))

    { return; } System.out.println(n.intValue()); if (num instanceof Double n) System.out.println(n.intValue()); No. n is still in scope 32
  22. @jeanneboyarsky On older Java? //TODO convert to pattern var when

    on Java 17 if (num instanceof Double) { Double numAsDouble = (Double) num; System.out.println(numAsDouble.intValue()); } 35 Positions for future
  23. @jeanneboyarsky Originally public String getLocation(String store) { String result =

    ""; switch (store) { case "Hallmark": result = "KC"; break; case "Crayola": result = "PA"; break; default: result = "anywhere"; } return result; } You remembered the breaks, right? 38
  24. @jeanneboyarsky Switch Expressions 14 public String getLocation(String store) { return

    switch (store) { case "Hallmark" -> "KC"; case "Crayola" -> "PA"; default -> "anywhere"; }; } Arrow labels No break keyword 39
  25. @jeanneboyarsky Missing value 14 enum Position { TOP, BOTTOM };

    Position pos = Position.TOP; int stmt = switch(pos) { case TOP: yield 1; }; int expr = switch(pos) { case BOTTOM -> 0; }; Does not compile because assigning value (poly expression) 40
  26. @jeanneboyarsky Pattern matching for switch 19 preview public int toInt(Object

    obj) { return switch (obj) { case Integer i -> i; case Double d -> d.intValue(); case String s -> Integer.parseInt(s); default -> throw new IllegalArgumentException("unknown type"); }; } Reminder: Syntax can change 41
  27. @jeanneboyarsky But wait, there’s more 19 preview static void printOddOrEven(Object

    obj) { switch (obj) { case Integer i when i % 2 == 1 -> System.out.println("odd"); case Integer i when i % 2 == 0 -> System.out.println(“even"); default -> System.out.println("not an int"); }; } Reminder: Feature can still change 42
  28. @jeanneboyarsky IDE Support 44 String result = switch (store) {

    case "Hallmark" -> "KC"; case "Crayola" -> "PA"; default -> "anywhere"; }; return result;
  29. @jeanneboyarsky On older Java? public String getLocation(String store) { //TODO

    convert to switch expression on Java 17 String result = ""; switch (store) { case "Hallmark": result = "KC"; break; case "Crayola": result = "PA"; break; default: result = "anywhere"; } return result; } 45
  30. @jeanneboyarsky Record 16 public record Book (String title, int numPages)

    { } New type Automatically get * final record * private final instance variables * public accessors * constructor taking both fields * equals * hashCode 49
  31. @jeanneboyarsky Using the Record 16 Book book = new Book("Breaking

    and entering", 289); System.out.println(book.title()); System.out.println(book.toString()); No “get” Outputs: Breaking and entering Book[title=Breaking and entering, numPages=289] 50
  32. @jeanneboyarsky Opportunities •Immutable POJOs •Don’t have to write equals/ hashCode

    •Vs reflection - EqualsBuilder •Make code coverage tool happy •Others? 17 51
  33. @jeanneboyarsky IDE Support 52 public record Book(String title, int numPages)

    { } Had to make instance variables final. Also didn’t remove my equals() even though generated by IntelliJ
  34. @jeanneboyarsky On older Java? //TODO convert to record when on

    Java 17 public final class Book { private String title; private int numPages; public Book(String title, int numPages) { this.title = title; this.numPages = numPages; } public String title() { return title; } public int numPages() { return numPages; } // hash code, equals 53 Be sure to use al fields for equals/ hashCode
  35. @jeanneboyarsky toList() 56 16 public List<String> listLonger( Stream<String> stream) {

    return stream.collect(Collectors.toList()); } public List<String> listShorter( Stream<String> stream) { return stream.toList(); }
  36. @jeanneboyarsky Teeing Collector 57 12 record Separations(String spaceSeparated, String commaSeparated)

    {} var list = List.of("x", "y", "z"); Separations result = list.stream() .collect(Collectors.teeing( Collectors.joining(" "), Collectors.joining(","), (s, c) -> new Separations(s, c))); System.out.println(result);
  37. @jeanneboyarsky Formatting a String String firstName = "Jeanne"; String lastName

    = "Boyarsky"; String str = String.format( "Hi %s %s!", firstName, lastName); System.out.println(str); System.out.println("Hi %s %s!".formatted( firstName, lastName)); Outputs: Hi Jeanne Boyarsky! Hi Jeanne Boyarsky! 12 58
  38. @jeanneboyarsky Common Conversions Conversion What it does %s Formattable as

    String %d Decimal integer (no dot) %c Char %f Float (decimal) %n New line Many more out of scope. Examples: • %e - scientific notation • %t - time • %S - converts to all uppercase 59
  39. @jeanneboyarsky Formatting a Number Char What it does - Left

    justified + Always include +/- space Leading space if positive Char What it does 0 Zero padded , Group numbers ( Negative # in parens 61
  40. @jeanneboyarsky Flag Examples Code Output "%,d".formatted(1234) 1,234 "%+d".formatted(1234) 1234 “%

    d".formatted(1234) 1234 “%,(d”.formatted(-1234) (1,234) “%,f”.formatted( 1.23456789) 1.234568 12 62
  41. @jeanneboyarsky Compact Number NumberFormat defaultFormat = NumberFormat.getCompactNumberInstance(); NumberFormat shortFormat =

    NumberFormat .getCompactNumberInstance( Locale.US, NumberFormat.Style.SHORT); NumberFormat longFormat = NumberFormat .getCompactNumberInstance( Locale.US, NumberFormat.Style.LONG); System.out.println(defaultFormat.format(1_000_000)); System.out.println(shortFormat.format(1_000_000)); System.out.println(longFormat.format(1_000_000)); 1M 1M 1 million 12 63
  42. @jeanneboyarsky New Files.mismatch() Path kcdc = Path.of("files/kcdc.txt"); Path kc =

    Path.of("files/kc.txt"); System.out.println(Files.mismatch(kcdc, kc)); System.out.println(Files.mismatch(kcdc, kcdc)); 12 11 (index of first character different) -1 (same file contents regardless of whether exists) 64