SlideShare a Scribd company logo
Clean code with Java 9
Miro Cupak

VP Engineering, DNAstack

10/05/2018
Kraków, 9-11 May 2018
Kraków, 9-11 May 2018@mirocupak
https://theamericanscholar.org/writing-english-as-a-second-language/, http://www.osnews.com/story/19266/WTFs_m, https://xkcd.com/1695/
Clean code: clarity, simplicity, brevity, humanity
Kraków, 9-11 May 2018@mirocupak
https://www.amazon.ca/Effective-Java-3rd-Joshua-
Bloch/dp/0134685997/
https://www.amazon.ca/Clean-Code-Handbook-
Software-Craftsmanship/dp/0132350882/
Features
• Factory methods for collections.

• Improved try-with-resources.

• Private methods in interfaces.

• Diamond operator with anonymous classes.

• Stream API enhancements. 

• Extensions to Optional.

• Stackwalker.

• HTTP/2 client.
Kraków, 9-11 May 2018@mirocupak
Factory methods for collections
Kraków, 9-11 May 2018@mirocupak
Factory methods for collections
• Obtain immutable collections via of/ofEntries methods.

• Static import java.util.Map.entry.

• Less verbose, no static initializer blocks.

• Don’t use Arrays.asList or Stream.of as shortcuts for creating collections.

• Don’t use external libraries if you only need immutable collections (Guava).

• No need to worry about leaving references to underlying collections.

• Thread-safe.

• Can be shared freely (no need for defensive copies).

• Good performance.

• Don’t create mutable collections unless necessary.

Kraków, 9-11 May 2018@mirocupak
Improved try-with-resources
Kraków, 9-11 May 2018@mirocupak
Improved try-with-resources
• Always prefer try-with-resources, don’t use try-finally and definitely don’t use finalizers to
close resources.

• Be aware of convenience methods, such as InputStream.transferTo.

• Don’t create unnecessary helper objects.

Kraków, 9-11 May 2018@mirocupak
Private methods in interfaces
Kraków, 9-11 May 2018@mirocupak
Private methods in interfaces
• Default methods, like all other methods, should be short.

• DRY.

• Use private methods to keep default methods short.

• Use private methods to extract shared core of default methods.

• Don’t use default methods to extend existing interfaces unless necessary.

• Use default methods for providing standard implementations for new interface code.

Kraków, 9-11 May 2018@mirocupak
Diamond operator with anonymous
classes
Kraków, 9-11 May 2018@mirocupak
Diamond operator with anonymous
classes
• Improved readability and consistency.

• Just use as everywhere else.

• Prefer lambdas to anonymous classes.

• Anonymous classes are not legacy (abstract classes, interfaces with multiple abstract
methods…).

Kraków, 9-11 May 2018@mirocupak
Stream API enhancements
Kraków, 9-11 May 2018@mirocupak
Stream API enhancements
• Be aware of new stream methods: takeWhile, dropWhile, iterate.

• Check for convenience stream methods before converting to streams manually (e.g.
LocalDate, Matcher).

• Avoid unnecessary null checks with ofNullable.

• Streams are suitable for more use cases now, but not all use cases.

• Don’t overuse streams as they can make code hard to read and difficult to maintain.

Kraków, 9-11 May 2018@mirocupak
Extensions to Optional
Kraków, 9-11 May 2018@mirocupak
Extensions to Optional
• Use ifPresentOrElse() instead of if-isPresent construct.

• or() provides a clean fluent way of chaining behaviour on Optionals.

• Use stream() to take advantage of the lazy nature of streams and handle streams of
Optionals.

• Remember that isPresent is rarely the answer.
Kraków, 9-11 May 2018@mirocupak
Stackwalker
Kraków, 9-11 May 2018@mirocupak
Stackwalker
• Prefer collections and streams to arrays.

• Access stacktraces lazily via walk().

• Take advantage of the Stream API to access only certain elements.

• Be aware of StackWalker.Option. Don’t resolve classes manually.

Kraków, 9-11 May 2018@mirocupak
HTTP/2 client
Kraków, 9-11 May 2018@mirocupak
HTTP/2 client
• Clean separation: HttpClient, HttpRequest, HttpResponse.

• HttpURLConnection is not pleasant to use.

• Avoid APIs with side effects.

• The new client API is versatile, flexible and clean.

• Prefer functionality in the JDK to external libraries.

• But aware it’s an incubator module.

Kraków, 9-11 May 2018@mirocupak
Q&A
Kraków, 9-11 May 2018@mirocupak
More info:

Session notes on Twitter.

Blog: https://mirocupak.com/

More Related Content

Similar to Clean code with Java 9

Voxxed Athens 2018 - Clean Code with Java9+
Voxxed Athens 2018 - Clean Code with Java9+Voxxed Athens 2018 - Clean Code with Java9+
Voxxed Athens 2018 - Clean Code with Java9+
Voxxed Athens
 
Writing clean code with Java 9+
Writing clean code with Java 9+Writing clean code with Java 9+
Writing clean code with Java 9+
Miro Cupak
 
Hivemall: Scalable machine learning library for Apache Hive/Spark/Pig
Hivemall: Scalable machine learning library for Apache Hive/Spark/PigHivemall: Scalable machine learning library for Apache Hive/Spark/Pig
Hivemall: Scalable machine learning library for Apache Hive/Spark/Pig
DataWorks Summit/Hadoop Summit
 
Writing clean code with Java in 2018
Writing clean code with Java in 2018Writing clean code with Java in 2018
Writing clean code with Java in 2018
Miro Cupak
 
Social connections14: Super charge your API’s with Reactive streams
Social connections14: Super charge your API’s with Reactive streamsSocial connections14: Super charge your API’s with Reactive streams
Social connections14: Super charge your API’s with Reactive streams
Frank van der Linden
 
Keeping Your Data Close and Your Caches Hotter (Ricardo Ferreira, Confluent) ...
Keeping Your Data Close and Your Caches Hotter (Ricardo Ferreira, Confluent) ...Keeping Your Data Close and Your Caches Hotter (Ricardo Ferreira, Confluent) ...
Keeping Your Data Close and Your Caches Hotter (Ricardo Ferreira, Confluent) ...
confluent
 
Cortex: Horizontally Scalable, Highly Available Prometheus
Cortex: Horizontally Scalable, Highly Available PrometheusCortex: Horizontally Scalable, Highly Available Prometheus
Cortex: Horizontally Scalable, Highly Available Prometheus
Grafana Labs
 
apidays Paris 2019 - How Do Async APIs Survive in a Rest World? by Luca Ferra...
apidays Paris 2019 - How Do Async APIs Survive in a Rest World? by Luca Ferra...apidays Paris 2019 - How Do Async APIs Survive in a Rest World? by Luca Ferra...
apidays Paris 2019 - How Do Async APIs Survive in a Rest World? by Luca Ferra...
apidays
 
How Master GraphQL by Francois de Campredon
How Master GraphQL by Francois de CampredonHow Master GraphQL by Francois de Campredon
How Master GraphQL by Francois de Campredon
TheFamily
 
High Performance Machine Learning in R with H2O
High Performance Machine Learning in R with H2OHigh Performance Machine Learning in R with H2O
High Performance Machine Learning in R with H2O
Sri Ambati
 
BigDataSpain 2016: Stream Processing Applications with Apache Apex
BigDataSpain 2016: Stream Processing Applications with Apache ApexBigDataSpain 2016: Stream Processing Applications with Apache Apex
BigDataSpain 2016: Stream Processing Applications with Apache Apex
Thomas Weise
 
How do async ap is survive in a rest world
How do async ap is survive in a rest world How do async ap is survive in a rest world
How do async ap is survive in a rest world
Red Hat
 
ArangoDB
ArangoDBArangoDB
Realizing the promise of portability with Apache Beam
Realizing the promise of portability with Apache BeamRealizing the promise of portability with Apache Beam
Realizing the promise of portability with Apache Beam
J On The Beach
 
Building cloud-enabled genomics workflows with Luigi and Docker
Building cloud-enabled genomics workflows with Luigi and DockerBuilding cloud-enabled genomics workflows with Luigi and Docker
Building cloud-enabled genomics workflows with Luigi and Docker
Jacob Feala
 
Portable Streaming Pipelines with Apache Beam
Portable Streaming Pipelines with Apache BeamPortable Streaming Pipelines with Apache Beam
Portable Streaming Pipelines with Apache Beam
confluent
 
Towards maintainable constraint validation and repair for taxonomies: The Poo...
Towards maintainable constraint validation and repair for taxonomies: The Poo...Towards maintainable constraint validation and repair for taxonomies: The Poo...
Towards maintainable constraint validation and repair for taxonomies: The Poo...
Monika Solanki
 
Automatic Query-Centric API for Routine Access to Linked Data
Automatic Query-Centric API for Routine Access to Linked DataAutomatic Query-Centric API for Routine Access to Linked Data
Automatic Query-Centric API for Routine Access to Linked Data
Albert Meroño-Peñuela
 
Docker Networking with Container Orchestration Engines [Docker Meetup Santa C...
Docker Networking with Container Orchestration Engines [Docker Meetup Santa C...Docker Networking with Container Orchestration Engines [Docker Meetup Santa C...
Docker Networking with Container Orchestration Engines [Docker Meetup Santa C...
Debra Robertson
 
Docker Networking in Swarm, Mesos and Kubernetes [Docker Meetup Santa Clara |...
Docker Networking in Swarm, Mesos and Kubernetes [Docker Meetup Santa Clara |...Docker Networking in Swarm, Mesos and Kubernetes [Docker Meetup Santa Clara |...
Docker Networking in Swarm, Mesos and Kubernetes [Docker Meetup Santa Clara |...
PLUMgrid
 

Similar to Clean code with Java 9 (20)

Voxxed Athens 2018 - Clean Code with Java9+
Voxxed Athens 2018 - Clean Code with Java9+Voxxed Athens 2018 - Clean Code with Java9+
Voxxed Athens 2018 - Clean Code with Java9+
 
Writing clean code with Java 9+
Writing clean code with Java 9+Writing clean code with Java 9+
Writing clean code with Java 9+
 
Hivemall: Scalable machine learning library for Apache Hive/Spark/Pig
Hivemall: Scalable machine learning library for Apache Hive/Spark/PigHivemall: Scalable machine learning library for Apache Hive/Spark/Pig
Hivemall: Scalable machine learning library for Apache Hive/Spark/Pig
 
Writing clean code with Java in 2018
Writing clean code with Java in 2018Writing clean code with Java in 2018
Writing clean code with Java in 2018
 
Social connections14: Super charge your API’s with Reactive streams
Social connections14: Super charge your API’s with Reactive streamsSocial connections14: Super charge your API’s with Reactive streams
Social connections14: Super charge your API’s with Reactive streams
 
Keeping Your Data Close and Your Caches Hotter (Ricardo Ferreira, Confluent) ...
Keeping Your Data Close and Your Caches Hotter (Ricardo Ferreira, Confluent) ...Keeping Your Data Close and Your Caches Hotter (Ricardo Ferreira, Confluent) ...
Keeping Your Data Close and Your Caches Hotter (Ricardo Ferreira, Confluent) ...
 
Cortex: Horizontally Scalable, Highly Available Prometheus
Cortex: Horizontally Scalable, Highly Available PrometheusCortex: Horizontally Scalable, Highly Available Prometheus
Cortex: Horizontally Scalable, Highly Available Prometheus
 
apidays Paris 2019 - How Do Async APIs Survive in a Rest World? by Luca Ferra...
apidays Paris 2019 - How Do Async APIs Survive in a Rest World? by Luca Ferra...apidays Paris 2019 - How Do Async APIs Survive in a Rest World? by Luca Ferra...
apidays Paris 2019 - How Do Async APIs Survive in a Rest World? by Luca Ferra...
 
How Master GraphQL by Francois de Campredon
How Master GraphQL by Francois de CampredonHow Master GraphQL by Francois de Campredon
How Master GraphQL by Francois de Campredon
 
High Performance Machine Learning in R with H2O
High Performance Machine Learning in R with H2OHigh Performance Machine Learning in R with H2O
High Performance Machine Learning in R with H2O
 
BigDataSpain 2016: Stream Processing Applications with Apache Apex
BigDataSpain 2016: Stream Processing Applications with Apache ApexBigDataSpain 2016: Stream Processing Applications with Apache Apex
BigDataSpain 2016: Stream Processing Applications with Apache Apex
 
How do async ap is survive in a rest world
How do async ap is survive in a rest world How do async ap is survive in a rest world
How do async ap is survive in a rest world
 
ArangoDB
ArangoDBArangoDB
ArangoDB
 
Realizing the promise of portability with Apache Beam
Realizing the promise of portability with Apache BeamRealizing the promise of portability with Apache Beam
Realizing the promise of portability with Apache Beam
 
Building cloud-enabled genomics workflows with Luigi and Docker
Building cloud-enabled genomics workflows with Luigi and DockerBuilding cloud-enabled genomics workflows with Luigi and Docker
Building cloud-enabled genomics workflows with Luigi and Docker
 
Portable Streaming Pipelines with Apache Beam
Portable Streaming Pipelines with Apache BeamPortable Streaming Pipelines with Apache Beam
Portable Streaming Pipelines with Apache Beam
 
Towards maintainable constraint validation and repair for taxonomies: The Poo...
Towards maintainable constraint validation and repair for taxonomies: The Poo...Towards maintainable constraint validation and repair for taxonomies: The Poo...
Towards maintainable constraint validation and repair for taxonomies: The Poo...
 
Automatic Query-Centric API for Routine Access to Linked Data
Automatic Query-Centric API for Routine Access to Linked DataAutomatic Query-Centric API for Routine Access to Linked Data
Automatic Query-Centric API for Routine Access to Linked Data
 
Docker Networking with Container Orchestration Engines [Docker Meetup Santa C...
Docker Networking with Container Orchestration Engines [Docker Meetup Santa C...Docker Networking with Container Orchestration Engines [Docker Meetup Santa C...
Docker Networking with Container Orchestration Engines [Docker Meetup Santa C...
 
Docker Networking in Swarm, Mesos and Kubernetes [Docker Meetup Santa Clara |...
Docker Networking in Swarm, Mesos and Kubernetes [Docker Meetup Santa Clara |...Docker Networking in Swarm, Mesos and Kubernetes [Docker Meetup Santa Clara |...
Docker Networking in Swarm, Mesos and Kubernetes [Docker Meetup Santa Clara |...
 

More from Miro Cupak

Exploring the latest and greatest from Java 14
Exploring the latest and greatest from Java 14Exploring the latest and greatest from Java 14
Exploring the latest and greatest from Java 14
Miro Cupak
 
Exploring reactive programming in Java
Exploring reactive programming in JavaExploring reactive programming in Java
Exploring reactive programming in Java
Miro Cupak
 
Exploring the last year of Java
Exploring the last year of JavaExploring the last year of Java
Exploring the last year of Java
Miro Cupak
 
Local variable type inference - Will it compile?
Local variable type inference - Will it compile?Local variable type inference - Will it compile?
Local variable type inference - Will it compile?
Miro Cupak
 
The Good, the Bad and the Ugly of Java API design
The Good, the Bad and the Ugly of Java API designThe Good, the Bad and the Ugly of Java API design
The Good, the Bad and the Ugly of Java API design
Miro Cupak
 
Local variable type inference - Will it compile?
Local variable type inference - Will it compile?Local variable type inference - Will it compile?
Local variable type inference - Will it compile?
Miro Cupak
 
Exploring reactive programming in Java
Exploring reactive programming in JavaExploring reactive programming in Java
Exploring reactive programming in Java
Miro Cupak
 
The good, the bad, and the ugly of Java API design
The good, the bad, and the ugly of Java API designThe good, the bad, and the ugly of Java API design
The good, the bad, and the ugly of Java API design
Miro Cupak
 
Master class in modern Java
Master class in modern JavaMaster class in modern Java
Master class in modern Java
Miro Cupak
 
The good, the bad, and the ugly of Java API design
The good, the bad, and the ugly of Java API designThe good, the bad, and the ugly of Java API design
The good, the bad, and the ugly of Java API design
Miro Cupak
 
Exploring reactive programming in Java
Exploring reactive programming in JavaExploring reactive programming in Java
Exploring reactive programming in Java
Miro Cupak
 
The good, the bad, and the ugly of Java API design
The good, the bad, and the ugly of Java API designThe good, the bad, and the ugly of Java API design
The good, the bad, and the ugly of Java API design
Miro Cupak
 
Writing clean code with modern Java
Writing clean code with modern JavaWriting clean code with modern Java
Writing clean code with modern Java
Miro Cupak
 
The good, the bad, and the ugly of Java API design
The good, the bad, and the ugly of Java API designThe good, the bad, and the ugly of Java API design
The good, the bad, and the ugly of Java API design
Miro Cupak
 
Master class in modern Java
Master class in modern JavaMaster class in modern Java
Master class in modern Java
Miro Cupak
 
Exploring reactive programming in Java
Exploring reactive programming in JavaExploring reactive programming in Java
Exploring reactive programming in Java
Miro Cupak
 
Writing clean code with modern Java
Writing clean code with modern JavaWriting clean code with modern Java
Writing clean code with modern Java
Miro Cupak
 
Exploring what's new in Java 10 and 11 (and 12)
Exploring what's new in Java 10 and 11 (and 12)Exploring what's new in Java 10 and 11 (and 12)
Exploring what's new in Java 10 and 11 (and 12)
Miro Cupak
 
Exploring what's new in Java 10 and 11
Exploring what's new in Java 10 and 11Exploring what's new in Java 10 and 11
Exploring what's new in Java 10 and 11
Miro Cupak
 
Exploring what's new in Java in 2018
Exploring what's new in Java in 2018Exploring what's new in Java in 2018
Exploring what's new in Java in 2018
Miro Cupak
 

More from Miro Cupak (20)

Exploring the latest and greatest from Java 14
Exploring the latest and greatest from Java 14Exploring the latest and greatest from Java 14
Exploring the latest and greatest from Java 14
 
Exploring reactive programming in Java
Exploring reactive programming in JavaExploring reactive programming in Java
Exploring reactive programming in Java
 
Exploring the last year of Java
Exploring the last year of JavaExploring the last year of Java
Exploring the last year of Java
 
Local variable type inference - Will it compile?
Local variable type inference - Will it compile?Local variable type inference - Will it compile?
Local variable type inference - Will it compile?
 
The Good, the Bad and the Ugly of Java API design
The Good, the Bad and the Ugly of Java API designThe Good, the Bad and the Ugly of Java API design
The Good, the Bad and the Ugly of Java API design
 
Local variable type inference - Will it compile?
Local variable type inference - Will it compile?Local variable type inference - Will it compile?
Local variable type inference - Will it compile?
 
Exploring reactive programming in Java
Exploring reactive programming in JavaExploring reactive programming in Java
Exploring reactive programming in Java
 
The good, the bad, and the ugly of Java API design
The good, the bad, and the ugly of Java API designThe good, the bad, and the ugly of Java API design
The good, the bad, and the ugly of Java API design
 
Master class in modern Java
Master class in modern JavaMaster class in modern Java
Master class in modern Java
 
The good, the bad, and the ugly of Java API design
The good, the bad, and the ugly of Java API designThe good, the bad, and the ugly of Java API design
The good, the bad, and the ugly of Java API design
 
Exploring reactive programming in Java
Exploring reactive programming in JavaExploring reactive programming in Java
Exploring reactive programming in Java
 
The good, the bad, and the ugly of Java API design
The good, the bad, and the ugly of Java API designThe good, the bad, and the ugly of Java API design
The good, the bad, and the ugly of Java API design
 
Writing clean code with modern Java
Writing clean code with modern JavaWriting clean code with modern Java
Writing clean code with modern Java
 
The good, the bad, and the ugly of Java API design
The good, the bad, and the ugly of Java API designThe good, the bad, and the ugly of Java API design
The good, the bad, and the ugly of Java API design
 
Master class in modern Java
Master class in modern JavaMaster class in modern Java
Master class in modern Java
 
Exploring reactive programming in Java
Exploring reactive programming in JavaExploring reactive programming in Java
Exploring reactive programming in Java
 
Writing clean code with modern Java
Writing clean code with modern JavaWriting clean code with modern Java
Writing clean code with modern Java
 
Exploring what's new in Java 10 and 11 (and 12)
Exploring what's new in Java 10 and 11 (and 12)Exploring what's new in Java 10 and 11 (and 12)
Exploring what's new in Java 10 and 11 (and 12)
 
Exploring what's new in Java 10 and 11
Exploring what's new in Java 10 and 11Exploring what's new in Java 10 and 11
Exploring what's new in Java 10 and 11
 
Exploring what's new in Java in 2018
Exploring what's new in Java in 2018Exploring what's new in Java in 2018
Exploring what's new in Java in 2018
 

Recently uploaded

Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
informapgpstrackings
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
Globus
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
Max Andersen
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
wottaspaceseo
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
Philip Schwarz
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
AMB-Review
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Mind IT Systems
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
Globus
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
abdulrafaychaudhry
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
Cyanic lab
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
vrstrong314
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
takuyayamamoto1800
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Globus
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Anthony Dahanne
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
Matt Welsh
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
Adele Miller
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
Paco van Beckhoven
 

Recently uploaded (20)

Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
 

Clean code with Java 9

  • 1. Clean code with Java 9 Miro Cupak VP Engineering, DNAstack 10/05/2018 Kraków, 9-11 May 2018
  • 2. Kraków, 9-11 May 2018@mirocupak https://theamericanscholar.org/writing-english-as-a-second-language/, http://www.osnews.com/story/19266/WTFs_m, https://xkcd.com/1695/ Clean code: clarity, simplicity, brevity, humanity
  • 3. Kraków, 9-11 May 2018@mirocupak https://www.amazon.ca/Effective-Java-3rd-Joshua- Bloch/dp/0134685997/ https://www.amazon.ca/Clean-Code-Handbook- Software-Craftsmanship/dp/0132350882/
  • 4. Features • Factory methods for collections. • Improved try-with-resources. • Private methods in interfaces. • Diamond operator with anonymous classes. • Stream API enhancements. • Extensions to Optional. • Stackwalker. • HTTP/2 client. Kraków, 9-11 May 2018@mirocupak
  • 5. Factory methods for collections Kraków, 9-11 May 2018@mirocupak
  • 6. Factory methods for collections • Obtain immutable collections via of/ofEntries methods. • Static import java.util.Map.entry. • Less verbose, no static initializer blocks. • Don’t use Arrays.asList or Stream.of as shortcuts for creating collections. • Don’t use external libraries if you only need immutable collections (Guava). • No need to worry about leaving references to underlying collections. • Thread-safe. • Can be shared freely (no need for defensive copies). • Good performance. • Don’t create mutable collections unless necessary. Kraków, 9-11 May 2018@mirocupak
  • 8. Improved try-with-resources • Always prefer try-with-resources, don’t use try-finally and definitely don’t use finalizers to close resources. • Be aware of convenience methods, such as InputStream.transferTo. • Don’t create unnecessary helper objects. Kraków, 9-11 May 2018@mirocupak
  • 9. Private methods in interfaces Kraków, 9-11 May 2018@mirocupak
  • 10. Private methods in interfaces • Default methods, like all other methods, should be short. • DRY. • Use private methods to keep default methods short. • Use private methods to extract shared core of default methods. • Don’t use default methods to extend existing interfaces unless necessary. • Use default methods for providing standard implementations for new interface code. Kraków, 9-11 May 2018@mirocupak
  • 11. Diamond operator with anonymous classes Kraków, 9-11 May 2018@mirocupak
  • 12. Diamond operator with anonymous classes • Improved readability and consistency. • Just use as everywhere else. • Prefer lambdas to anonymous classes. • Anonymous classes are not legacy (abstract classes, interfaces with multiple abstract methods…). Kraków, 9-11 May 2018@mirocupak
  • 13. Stream API enhancements Kraków, 9-11 May 2018@mirocupak
  • 14. Stream API enhancements • Be aware of new stream methods: takeWhile, dropWhile, iterate. • Check for convenience stream methods before converting to streams manually (e.g. LocalDate, Matcher). • Avoid unnecessary null checks with ofNullable. • Streams are suitable for more use cases now, but not all use cases. • Don’t overuse streams as they can make code hard to read and difficult to maintain. Kraków, 9-11 May 2018@mirocupak
  • 15. Extensions to Optional Kraków, 9-11 May 2018@mirocupak
  • 16. Extensions to Optional • Use ifPresentOrElse() instead of if-isPresent construct. • or() provides a clean fluent way of chaining behaviour on Optionals. • Use stream() to take advantage of the lazy nature of streams and handle streams of Optionals. • Remember that isPresent is rarely the answer. Kraków, 9-11 May 2018@mirocupak
  • 18. Stackwalker • Prefer collections and streams to arrays. • Access stacktraces lazily via walk(). • Take advantage of the Stream API to access only certain elements. • Be aware of StackWalker.Option. Don’t resolve classes manually. Kraków, 9-11 May 2018@mirocupak
  • 19. HTTP/2 client Kraków, 9-11 May 2018@mirocupak
  • 20. HTTP/2 client • Clean separation: HttpClient, HttpRequest, HttpResponse. • HttpURLConnection is not pleasant to use. • Avoid APIs with side effects. • The new client API is versatile, flexible and clean. • Prefer functionality in the JDK to external libraries. • But aware it’s an incubator module. Kraków, 9-11 May 2018@mirocupak
  • 21. Q&A Kraków, 9-11 May 2018@mirocupak More info: Session notes on Twitter. Blog: https://mirocupak.com/