Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.
Nächste SlideShare
What to Upload to SlideShare
Weiter
Herunterladen, um offline zu lesen und im Vollbildmodus anzuzeigen.

Teilen

Is boilerplate code really so bad?

Herunterladen, um offline zu lesen

It's exhausting staying up to date with the evolution of Java and the myriad other JVM languages. Is it worth it? What
do modern languages give us?

Ähnliche Bücher

Kostenlos mit einer 30-tägigen Testversion von Scribd

Alle anzeigen

Ähnliche Hörbücher

Kostenlos mit einer 30-tägigen Testversion von Scribd

Alle anzeigen
  • Gehören Sie zu den Ersten, denen das gefällt!

Is boilerplate code really so bad?

  1. 1. — Trisha Gee (@trisha_gee) Developer & Technical Advocate, JetBrains Is Boilerplate Code Really So Bad?
  2. 2. Most of the time TL;DR Yes
  3. 3. Code is Read More Than Written
  4. 4. “Indeed, the ratio of time spent reading versus writing is well over 10 to 1. We are constantly reading old code as part of the effort to write new code. ...[Therefore,] making it easy to read makes it easier to write.” Robert C. Martin, Clean Code: A Handbook of Agile Software Craftsmanship
  5. 5. Hello World
  6. 6. Writing: Java & Kotlin
  7. 7. Writing: REPL
  8. 8. package com.mechanitis.demo.boilerplate.demo; public class Main { public static void main(String[] args) { System.out.println("Hello World!"); } } println("Hello World!") Reading: Java
  9. 9. package com.mechanitis.demo.boilerplate.demo fun main(args: Array<String>) { println("Hello") } println("Hello") Reading: Kotlin
  10. 10. System.out.println("Hello World!")println("Hello World!") Reading: JShell
  11. 11. Declaring Variables
  12. 12. Map<Integer, Customer> customers = new HashMap<Integer, Customer>(); Java 5 Map<Integer, Customer> customers = new HashMap<~>();
  13. 13. Java 7 Map<Integer, Customer> customers = new HashMap<>();
  14. 14. Java 10 var customers = new HashMap<Integer, Customer>();
  15. 15. Kotlin val customers = HashMap<Int, Customer>()
  16. 16. var customers = new HashMap<Integer, Customer>(); Reading: Java var customers = HashMap<Integer, Customer>()
  17. 17. val customers = HashMap<Int, Customer>() Reading: Kotlin
  18. 18. Data Classes
  19. 19. Writing: Java
  20. 20. Writing: Kotlin
  21. 21. public class Customer { private final String name; private final int age; @Override public boolean equals(final Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } final Customer customer = (Customer) o; if (age != customer.age) { return false; } return name != null ? name.equals(customer.name) : customer.name == null; } @Override public int hashCode() { int result = name != null ? name.hashCode() : 0; result = 31 * result + age; return result; } @Override public String toString() { return "Customer{" + "name='" + name + ''' + ", age=" + age + '}'; } private String getName() { return name; } private int getAge() { return age; } public Customer(final String name, final int age) { this.name = name; this.age = age; } } Reading: Java
  22. 22. Reading: Kotlin data class CustomerKotlin (val name: String, val age: Int)
  23. 23. Writing Code Is Not The Hard Bit
  24. 24. http://cr.openjdk.java.net/~briangoetz/ amber/datum.html
  25. 25. Casting
  26. 26. Java Kotlin
  27. 27. JEP 305: Pattern Matching for instanceof (Preview)
  28. 28. Nulls
  29. 29. Java Kotlin Java 8
  30. 30. Switch
  31. 31. var port = Integer.valueOf(portInputValue); return switch (port) { case 20 -> PortType.FTP; case 80,8080 -> PortType.HTTP; case 27017 -> PortType.DATABASE; default -> { if (port >= 20_001 && port <= 30_000) { break PortType.SAFE; } else if (port >= 9080 && port <= 9092) { break PortType.BUSY; } else { break PortType.UNKNOWN; } } };
  32. 32. Default parameter values
  33. 33. Java Kotlin
  34. 34. Collections
  35. 35. Java Kotlin Groovy
  36. 36. Java Kotlin Groovy
  37. 37. Lambda Expressions
  38. 38. Java Kotlin
  39. 39. Functional Parameters
  40. 40. Java 8
  41. 41. Java 8
  42. 42. Kotlin
  43. 43. Navigating Collections
  44. 44. Java 8 Kotlin Kotlin
  45. 45. Can we go too far?
  46. 46. Dangers of var/val Customer c = CustomerFactory.create("Sam");Customer c = create("Sam");var c = create("Sam");
  47. 47. Implicits in Scala
  48. 48. When Is Boilerplate Code Good?
  49. 49. Kotlin Kotlin
  50. 50. Groovy def 'emits an event for new projects'() { @Subject def service = new EventService() given: def subscriber = Mock(Subscriber) subscriber.onSubscribe(_) >> { Subscription sub -> sub.request(MAX_VALUE) } Flux<Schedule.Project> projects = service.getProjects(); projects.subscribe(subscriber) when: service.recordNewActivity(schedule) then: 1 * subscriber.onNext(schedule.projects[0]) }
  51. 51. In Summary
  52. 52. Elimination of Boilerplate Can Improve Readability You can focus on the business logic
  53. 53. data class CustomerKotlin (val name: String, val age: Int)
  54. 54. Not All “Boilerplate” is Boilerplate …and removing it can change the meaning
  55. 55. val emails = customers.asSequence() .filter { it.name.startsWith("A") } .map { it.email } .toList() val emails = customers .filter { it.name.startsWith("A") } .map { it.email }
  56. 56. Less Code Is Not Always Better Additional code can improve readability
  57. 57. def 'emits an event for new projects'() { @Subject def service = new EventService() given: def subscriber = Mock(Subscriber) subscriber.onSubscribe(_) >> { Subscription sub -> sub.request(MAX_VALUE) } Flux<Schedule.Project> projects = service.getProjects(); projects.subscribe(subscriber) when: service.recordNewActivity(schedule) then: 1 * subscriber.onNext(schedule.projects[0]) }
  58. 58. Readability is subjective …and is often related to familiarity
  59. 59. List<String> emails = customers.stream() .filter(customer -> customer.getName().startsWith("A")) .map(CustomerJava::getEmail) .collect(toList()); List<String> emails = new ArrayList<>(); for (CustomerJava customer : customers) { if (customer.getName().startsWith("A")) { String email = customer.getEmail(); emails.add(email); } }
  60. 60. Modern languages offer the option to reduce code Will this make it more readable?
  61. 61. http://bit.ly/BoilJVM

It's exhausting staying up to date with the evolution of Java and the myriad other JVM languages. Is it worth it? What do modern languages give us?

Aufrufe

Aufrufe insgesamt

718

Auf Slideshare

0

Aus Einbettungen

0

Anzahl der Einbettungen

407

Befehle

Downloads

5

Geteilt

0

Kommentare

0

Likes

0

×