Advertisement
Advertisement

More Related Content

Advertisement

Coming Clean on Records at Tirana JUG

  1. Coming Clean on Records A glance at what’s coming your way Albin Hasani Twitter: @albihasani94
  2. How many times have you written..
  3. One of these good-old Java Classes
  4. One of these good-old Java Classes Fields
  5. One of these good-old Java Classes Fields Constructor
  6. One of these good-old Java Classes Fields Constructor Getters and setters
  7. One of these good-old Java Classes Fields Constructor Getters and setters equals
  8. One of these good-old Java Classes Fields Constructor Getters and setters equals hashCode
  9. One of these good-old Java Classes Fields Constructor Getters and setters equals hashCode toString
  10. Just to store a values pair (name, age)
  11. And how many times did you wish it did end?
  12. – Cicero “When in doubt, run to the IDE.” Demo
  13. Some Inconveniences • Not perfect for classes that are just wrappers for some data • Lots of boilerplate: constructors, accessors, equals, 
 hashCode, toString • No fun to write => Delegate to the IDE • Reading does not feel like reading developer’s intent
  14. More code = More places for bugs
  15. The Pair Awakens • Part of the javafx.util package • Pair<K, V> pair = new Pair<>(K key, V value) • K getKey( ), V getValue( ) • It gets you a standard implementation of equals, 
 hashCode, and toString
  16. Code.. Code.. Code.. Demo
  17. All is good.. right?
  18. Well, not exactly • The notion of type names is lost, only key-value pair
 gets printed out • JavaFX has been removed from JDK 11 • Its module/jar has to be included on each compilation
 and runtime • Overhead saved from the Person class => traded with
 some overhead in configuration
  19. Far from an ideal solution
  20. Rise of the Record • record Person (String name, int age) { } • Transparent holders for shallowly immutable data • Modeling data as data • The data, the whole data, and nothing but the data What are records?
  21. Living with records • Boilerplate reduction, obviously • Good semantics => easier to read, less error prone • Defaults for constructor, getters, equals, hashCode,
 and toString • Extension by methods, custom constructor What do they offer?
  22. More show me Demo
  23. Record-ify the Person Class Demo
  24. Getters • record.fieldName( ) Demo
  25. Custom Logic • record.methodName( ) Demo
  26. Custom Constructor • public record(String name, int age) {…} Demo
  27. Strip the constructor down • public record {…} Demo
  28. Stream Case Study • Method computed for every comparison yearsTo100( ) Demo
  29. Record: Performance Gain • Method computed for every comparison yearsTo100( ) Demo
  30. Making Life Simple Java Developers
  31. Records • Convenient data stores • Ideal for DTOs and classes without much business logic • Far less boilerplate • Lightweight constructor syntax • Readability over lines of code
  32. – Obi-Wan Kenobi “Use the record, Luke.”
  33. Pay Java records a visit! 115  W CHIPPEWA ST - DOWNTOWN BUFFALO, NY (corner of Delaware)
  34. If you have any questions Please reach out directly after the talk in person or leave me a message @albihasani94 on twitter Albin Hasani Twitter: @albihasani94
Advertisement