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
Migrating to IntelliJ IDEA from Eclipse
Weiter
Herunterladen, um offline zu lesen und im Vollbildmodus anzuzeigen.

6

Teilen

Refactoring to Java 8 (QCon New York)

Herunterladen, um offline zu lesen

While we’re drawing ever closer to Java 9, and even hearing about features in Java 10, it’s also true that many of us are still working with an older version. Even if your project has technically adopted Java 8, and even if you’re using it when coding new features, it’s likely the majority of your code base is still not making the most of what’s available in Java 8 - features like Lambda Expressions, the Streams API, and new Date/Time.

In this presentation, Trisha:
- Highlights some of the benefits of using Java 8 - after all, you’ll probably have to persuade The Management that tampering with existing code is worthwhile
- Demonstrates how to identify areas of code that can be updated to use Java 8 features
- Shows how to automatically refactor your code to make use of features like lambdas and streams.
- Covers some of the pros and cons of using the new features - including suggestions of when refactoring may NOT be the best idea.

Ä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

Refactoring to Java 8 (QCon New York)

  1. 1. Trisha Gee (@trisha_gee) Developer & Technical Advocate, JetBrains Refactoring to Java 8
  2. 2. Why Java 8?
  3. 3. It’s Faster •Performance Improvements in Common Data Structures •Fork/Join Speed Improvements •Changes to Support Concurrency •…and more http://bit.ly/refJ8
  4. 4. Easy to Parallelize
  5. 5. Fewer Lines of Code
  6. 6. New Solutions to Problems
  7. 7. Minimizes Errors
  8. 8. Safety Check
  9. 9. Test Coverage
  10. 10. Performance Tests
  11. 11. Decide on the Goals
  12. 12. Limit the Scope
  13. 13. Morphia https://github.com/mongodb/morphia
  14. 14. Refactoring!
  15. 15. Lambda Expressions
  16. 16. Automatic Refactoring •Predicate •Comparator •Runnable •etc…
  17. 17. Abstract classes
  18. 18. Advanced Search
  19. 19. Collections & Streams API
  20. 20. Automatic Refactoring •For loop to collect •For loop to forEach •…with and without Streams
  21. 21. Manual Refactoring
  22. 22. Optional
  23. 23. But what about performance?
  24. 24. Lambdas
  25. 25. 0 20 40 60 80 100 120 140 160 180Ops/ms Anonymous Inner Classes vs Lambdas Anonymous Inner Class Lambda  
  26. 26. http://www.oracle.com/technetwork/java/jvmls2013kuksen-2014088.pdf 0 2 4 6 8 10 12 14 16 18 20 single thread max threads nsec/op Performance of Capture anonymous(static) anonymous(non-static) lambda  
  27. 27.   0 50,000 100,000 150,000 200,000 250,000 300,000 350,000 400,000 450,000 500,000 Constant message Variable message Ops/ms Logging Performance Direct call Lambda
  28. 28. Streams vs Iteration
  29. 29. 0 1 2 3 4 5 6 7 8 9 Ops/ms Iterator vs Stream (1000 elements) for loop forEach()  
  30. 30. 0 1000 2000 3000 4000 5000 6000 7000 1 10 100 1000 Ops/ms IterHelper original simplified refactored  
  31. 31. 0 1 2 3 4 5 6 7 8 9 1000 10000 100000 Ops/ms IterHelper original simplified refactored  
  32. 32. BasicDAO – map & collect
  33. 33. 0 5000 10000 15000 20000 25000 1 10 100 1000 Ops/ms BasicDAO original simplified refactored  
  34. 34. DuplicatedAttributeNames – filter, map & collect
  35. 35. 0 200 400 600 800 1000 1200 Ops/ms DuplicatedAttributeNames original refactored  
  36. 36. EntityScanner– forEach
  37. 37.   0 0.01 0.02 0.03 0.04 0.05 0.06 Ops/ms EntityScanner original refactored
  38. 38. DatastoreImpl – filter & forEach
  39. 39. 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 Ops/ms DatastoreImpl original refactored  
  40. 40. MappingValidator – single stream operation
  41. 41.   0 100 200 300 400 500 600 700 800 900 EntityWithOneError EntityWith10Errors EntityWith20Errors Ops/ms MappingValidator original refactored
  42. 42. QueryImpl – multiple operations
  43. 43. 0 500 1000 1500 2000 2500 3000 3500 Ops/ms QueryImpl original simplified refactored  
  44. 44. Going parallel
  45. 45. 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 Serial Parallel TimeTaken(seconds) map() MacBook Surface  
  46. 46. 0 20 40 60 80 100 120 140 160 Serial Parallel TimeTaken(millis) findAny() MacBook Surface  
  47. 47. Optional
  48. 48. 0 50,000 100,000 150,000 200,000 250,000 300,000 350,000 Value not null Value null Ops/ms Compare Constant Field Value with Null Null check Optional  
  49. 49. 0 50,000 100,000 150,000 200,000 250,000 300,000 Value not null Value null Ops/ms Compare Variable Field Value with Null Null check Optional  
  50. 50. Summary
  51. 51. Sometimes new idioms decrease clutter
  52. 52. …but sometimes they don’t
  53. 53. Sometimes the new features improve performance
  54. 54. 0 20 40 60 80 100 120 140 160 180Ops/ms Anonymous Inner Classes vs Lambdas Anonymous Inner Class Lambda  
  55. 55. …and sometimes they don’t
  56. 56. 0 200 400 600 800 1000 1200 Ops/ms DuplicatedAttributeNames original refactored  
  57. 57. Sometimes a new feature makes life easier
  58. 58. …sometimes not so much
  59. 59. Some refactoring can safely be done automatically
  60. 60. …often you need a human brain
  61. 61. Conclusion
  62. 62. Should you migrate your code to Java 8?
  63. 63. It Depends
  64. 64. Always remember what your goal is And compare results to it
  65. 65. Understand what may impact performance And if in doubt, measure
  66. 66. Code may magically improve Or you may expose areas for improvement
  67. 67. Your tools can help you But you need to apply your brain too
  68. 68. http://bit.ly/refJ8 @trisha_gee
  • andrewsavill1

    Sep. 29, 2017
  • bhaveshpatidar29

    Jul. 27, 2017
  • kwangyongkim

    Sep. 5, 2016
  • LorenzoGallucci

    Aug. 4, 2016
  • VidyaBharath

    Jun. 27, 2016
  • NaveenRamaka

    Jun. 25, 2016

While we’re drawing ever closer to Java 9, and even hearing about features in Java 10, it’s also true that many of us are still working with an older version. Even if your project has technically adopted Java 8, and even if you’re using it when coding new features, it’s likely the majority of your code base is still not making the most of what’s available in Java 8 - features like Lambda Expressions, the Streams API, and new Date/Time. In this presentation, Trisha: - Highlights some of the benefits of using Java 8 - after all, you’ll probably have to persuade The Management that tampering with existing code is worthwhile - Demonstrates how to identify areas of code that can be updated to use Java 8 features - Shows how to automatically refactor your code to make use of features like lambdas and streams. - Covers some of the pros and cons of using the new features - including suggestions of when refactoring may NOT be the best idea.

Aufrufe

Aufrufe insgesamt

9.393

Auf Slideshare

0

Aus Einbettungen

0

Anzahl der Einbettungen

8.494

Befehle

Downloads

48

Geteilt

0

Kommentare

0

Likes

6

×