SlideShare a Scribd company logo
1 of 21
Download to read offline
@mirocupak
Miro Cupak
VP Engineering, DNAstack
01/06/2018
Writing clean code
with Java 9+
@mirocupak
Clarity, simplicity, brevity, humanity
!2
http://www.osnews.com/story/19266/WTFs_m https://xkcd.com/1695/
https://theamericanscholar.org/writing-english-as-a-second-language/
@mirocupak !3
https://www.amazon.ca/Clean-Code-Handbook-Software-
Craftsmanship/dp/0132350882/
https://www.amazon.ca/Effective-Java-3rd-Joshua-Bloch/dp/
0134685997/
@mirocupak
Features
!4
• Factory methods for collections.
• Improved try-with-resources.
• Private methods in interfaces.
• Stream API enhancements.
• Extensions to Optional.
• HTTP/2 client.
• Type inference.
@mirocupak !5
Factory methods for
collections
@mirocupak
Factory methods for collections
!6
• Obtain immutable collections via of/ofEntries methods.
• Create immutable copies of collections via copyOf (Java 10).
• 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 and can be shared freely (no need for defensive copies).
• Good performance.
• Don’t create mutable collections unless necessary.
@mirocupak !7
Improved try-with-resources
@mirocupak
Improved try-with-resources
!8
• 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.
@mirocupak !9
Private methods in
interfaces
@mirocupak
Private methods in interfaces
!10
• 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.
• Use default methods for providing standard implementations for new
interface code.
• Don’t use default methods to extend existing interfaces unless
necessary.
@mirocupak !11
Stream API enhancements
@mirocupak
Stream API enhancements
!12
• Be aware of new stream methods: takeWhile, dropWhile,
iterate.
• Prefer collecting into immutable collections using
toUnmodifiableList, toUnmodifiableSet,
toUnmodifiableMap.
• Check for convenience stream methods before converting to streams
manually (e.g. Matcher, LocalDate).
• 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.
@mirocupak !13
Extensions to Optional
@mirocupak
Extensions to Optional
!14
• Use ifPresentOrElse instead of if-isPresent construct.
• or provides a clean fluent way of chaining behaviour on Optionals.
• Be aware of orElse* methods, e.g. the new orElseThrow (Java 10).
• Use stream to take advantage of the lazy nature of streams and
handle streams of Optionals.
• Remember that isPresent is rarely the answer.
@mirocupak !15
HTTP/2 client
@mirocupak
HTTP/2 client
!16
• 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.
@mirocupak !17
Type inference
@mirocupak
Diamond operator with anonymous classes
!18
• Use <> for anonymous classes, like everywhere else.
• <> improves readability and consistency.
• Use anonymous classes only when needed (e.g. abstract classes,
interfaces with multiple abstract methods…), otherwise replace with
lambdas.
@mirocupak
Local variable type inference
!19
• Does not replace static typing.
• Generally good.
• Reduces boilerplate and improves readability.
• Helps with maintenance and refactoring.
• Use for local variables with initializers (especially constructors) and for
loops.
• Can’t use for method formals, constructor formals, method return types,
fields, catch formals, null or array initializers, lambdas, method references,
or any other kind of variable declaration.
• Consider whether to use when the generated type is not obvious.
• But use for complex types when breaking chained or nested expressions
with local variables.
• Primitive types might surprise you, be careful (e.g. byte, short, long all
inferred as int).
@mirocupak
Local variable type inference
!20
• Be very careful about combining with <> and generic methods (e.g. var
list = new ArrayList<>()).
• Probably not the best idea to use with anonymous classes.
• Use carefully chosen and expressive variable names.
• Don’t use Hungarian notation.
• Don’t rely on IDEs.
• Minimize the scope of local variables.
• Declare variable when it’s first used.
• Declaration not containing an initializer (i.e. you can’t use var) often
indicates the scope is not minimal.
• Prefer for loops to while loops.
• Keep methods small and focused.
• Code to the interface pattern does not work, but that’s kind of OK.
@mirocupak
Questions?
!21
More info:
Session notes on Twitter.
Blog: https://mirocupak.com/

More Related Content

Similar to Voxxed Athens 2018 - Clean Code with Java9+

Working With Concurrency In Java 8
Working With Concurrency In Java 8Working With Concurrency In Java 8
Working With Concurrency In Java 8Heartin Jacob
 
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 designMiro 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 designMiro Cupak
 
Reading Notes : the practice of programming
Reading Notes : the practice of programmingReading Notes : the practice of programming
Reading Notes : the practice of programmingJuggernaut Liu
 
25 Real Life Tips In Ruby on Rails Development
25 Real Life Tips In Ruby on Rails Development25 Real Life Tips In Ruby on Rails Development
25 Real Life Tips In Ruby on Rails DevelopmentBelighted
 
The Good, the Bad and the Ugly things to do with android
The Good, the Bad and the Ugly things to do with androidThe Good, the Bad and the Ugly things to do with android
The Good, the Bad and the Ugly things to do with androidStanojko Markovik
 
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 designMiro 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 designMiro Cupak
 
Writing code for others
Writing code for othersWriting code for others
Writing code for othersAmol Pujari
 
openmp.New.intro-unc.edu.ppt
openmp.New.intro-unc.edu.pptopenmp.New.intro-unc.edu.ppt
openmp.New.intro-unc.edu.pptMALARMANNANA1
 
Writing Readable Code
Writing Readable CodeWriting Readable Code
Writing Readable Codeeddiehaber
 
10 Sets of Best Practices for Java 8
10 Sets of Best Practices for Java 810 Sets of Best Practices for Java 8
10 Sets of Best Practices for Java 8Garth Gilmour
 
Scala Bay Meetup - The state of Scala code style and quality
Scala Bay Meetup - The state of Scala code style and qualityScala Bay Meetup - The state of Scala code style and quality
Scala Bay Meetup - The state of Scala code style and qualityJaime Jorge
 
Learning from "Effective Scala"
Learning from "Effective Scala"Learning from "Effective Scala"
Learning from "Effective Scala"Kazuhiro Sera
 
The ES6 Conundrum - All Things Open 2015
The ES6 Conundrum - All Things Open 2015The ES6 Conundrum - All Things Open 2015
The ES6 Conundrum - All Things Open 2015Christian Heilmann
 
(6) c sharp introduction_advanced_features_part_i
(6) c sharp introduction_advanced_features_part_i(6) c sharp introduction_advanced_features_part_i
(6) c sharp introduction_advanced_features_part_iNico Ludwig
 
Augmenting Flow Operations and Feedback on the Model Driven MD_SAL Approach i...
Augmenting Flow Operations and Feedback on the Model Driven MD_SAL Approach i...Augmenting Flow Operations and Feedback on the Model Driven MD_SAL Approach i...
Augmenting Flow Operations and Feedback on the Model Driven MD_SAL Approach i...Brent Salisbury
 

Similar to Voxxed Athens 2018 - Clean Code with Java9+ (20)

Working With Concurrency In Java 8
Working With Concurrency In Java 8Working With Concurrency In Java 8
Working With Concurrency In Java 8
 
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
 
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
 
Reading Notes : the practice of programming
Reading Notes : the practice of programmingReading Notes : the practice of programming
Reading Notes : the practice of programming
 
Ajaxworld07
Ajaxworld07Ajaxworld07
Ajaxworld07
 
Ajaxworld07
Ajaxworld07Ajaxworld07
Ajaxworld07
 
25 Real Life Tips In Ruby on Rails Development
25 Real Life Tips In Ruby on Rails Development25 Real Life Tips In Ruby on Rails Development
25 Real Life Tips In Ruby on Rails Development
 
The Good, the Bad and the Ugly things to do with android
The Good, the Bad and the Ugly things to do with androidThe Good, the Bad and the Ugly things to do with android
The Good, the Bad and the Ugly things to do with android
 
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
 
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 code for others
Writing code for othersWriting code for others
Writing code for others
 
openmp.New.intro-unc.edu.ppt
openmp.New.intro-unc.edu.pptopenmp.New.intro-unc.edu.ppt
openmp.New.intro-unc.edu.ppt
 
Writing Readable Code
Writing Readable CodeWriting Readable Code
Writing Readable Code
 
10 Sets of Best Practices for Java 8
10 Sets of Best Practices for Java 810 Sets of Best Practices for Java 8
10 Sets of Best Practices for Java 8
 
Scala Bay Meetup - The state of Scala code style and quality
Scala Bay Meetup - The state of Scala code style and qualityScala Bay Meetup - The state of Scala code style and quality
Scala Bay Meetup - The state of Scala code style and quality
 
Javascript best practices
Javascript best practicesJavascript best practices
Javascript best practices
 
Learning from "Effective Scala"
Learning from "Effective Scala"Learning from "Effective Scala"
Learning from "Effective Scala"
 
The ES6 Conundrum - All Things Open 2015
The ES6 Conundrum - All Things Open 2015The ES6 Conundrum - All Things Open 2015
The ES6 Conundrum - All Things Open 2015
 
(6) c sharp introduction_advanced_features_part_i
(6) c sharp introduction_advanced_features_part_i(6) c sharp introduction_advanced_features_part_i
(6) c sharp introduction_advanced_features_part_i
 
Augmenting Flow Operations and Feedback on the Model Driven MD_SAL Approach i...
Augmenting Flow Operations and Feedback on the Model Driven MD_SAL Approach i...Augmenting Flow Operations and Feedback on the Model Driven MD_SAL Approach i...
Augmenting Flow Operations and Feedback on the Model Driven MD_SAL Approach i...
 

More from Voxxed Athens

Voxxed Athens 2018 - Eventing, Serverless, and the Extensible Enterprise
Voxxed Athens 2018 - Eventing, Serverless, and the Extensible EnterpriseVoxxed Athens 2018 - Eventing, Serverless, and the Extensible Enterprise
Voxxed Athens 2018 - Eventing, Serverless, and the Extensible EnterpriseVoxxed Athens
 
Voxxed Athens 2018 - Let’s Get Chatty with Conversational Interface with Java...
Voxxed Athens 2018 - Let’s Get Chatty with Conversational Interface with Java...Voxxed Athens 2018 - Let’s Get Chatty with Conversational Interface with Java...
Voxxed Athens 2018 - Let’s Get Chatty with Conversational Interface with Java...Voxxed Athens
 
Voxxed Athens 2018 - IBM Watson Machine Learning – Build and train AI models ...
Voxxed Athens 2018 - IBM Watson Machine Learning – Build and train AI models ...Voxxed Athens 2018 - IBM Watson Machine Learning – Build and train AI models ...
Voxxed Athens 2018 - IBM Watson Machine Learning – Build and train AI models ...Voxxed Athens
 
Voxxed Athens 2018 - We're going to talk about no sql, you can't join
Voxxed Athens 2018 - We're going to talk about no sql, you can't joinVoxxed Athens 2018 - We're going to talk about no sql, you can't join
Voxxed Athens 2018 - We're going to talk about no sql, you can't joinVoxxed Athens
 
Voxxed Athens 2018 - Methods and Practices for Guaranteed Failure in Big Data
Voxxed Athens 2018 - Methods and Practices for Guaranteed Failure in Big DataVoxxed Athens 2018 - Methods and Practices for Guaranteed Failure in Big Data
Voxxed Athens 2018 - Methods and Practices for Guaranteed Failure in Big DataVoxxed Athens
 
Voxxed Athens 2018 - The secret for high quality software: Listen to your people
Voxxed Athens 2018 - The secret for high quality software: Listen to your peopleVoxxed Athens 2018 - The secret for high quality software: Listen to your people
Voxxed Athens 2018 - The secret for high quality software: Listen to your peopleVoxxed Athens
 
Voxxed Athens 2018 - A scalable maritime platform providing services through...
Voxxed Athens 2018 -  A scalable maritime platform providing services through...Voxxed Athens 2018 -  A scalable maritime platform providing services through...
Voxxed Athens 2018 - A scalable maritime platform providing services through...Voxxed Athens
 
Voxxed Athens 2018 - UX design and back-ends: When the back-end meets the user
Voxxed Athens 2018 - UX design and back-ends: When the back-end meets the userVoxxed Athens 2018 - UX design and back-ends: When the back-end meets the user
Voxxed Athens 2018 - UX design and back-ends: When the back-end meets the userVoxxed Athens
 
Voxxed Athens 2018 - Your Local Meet-up: Your Path to Crafts(wo)manship
Voxxed Athens 2018 - Your Local Meet-up: Your Path to Crafts(wo)manshipVoxxed Athens 2018 - Your Local Meet-up: Your Path to Crafts(wo)manship
Voxxed Athens 2018 - Your Local Meet-up: Your Path to Crafts(wo)manshipVoxxed Athens
 
Voxxed Athens 2018 - The quantum computers are coming
Voxxed Athens 2018 - The quantum computers are comingVoxxed Athens 2018 - The quantum computers are coming
Voxxed Athens 2018 - The quantum computers are comingVoxxed Athens
 
Voxxed Athens 2018 - Serverless by Design
Voxxed Athens 2018 - Serverless by DesignVoxxed Athens 2018 - Serverless by Design
Voxxed Athens 2018 - Serverless by DesignVoxxed Athens
 
Voxxed Athens 2018 - Getting real with progressive web apps in 2018
Voxxed Athens 2018 - Getting real with progressive web apps in 2018Voxxed Athens 2018 - Getting real with progressive web apps in 2018
Voxxed Athens 2018 - Getting real with progressive web apps in 2018Voxxed Athens
 
Voxxed Athens 2018 - Why Kotlin?
Voxxed Athens 2018 - Why Kotlin?Voxxed Athens 2018 - Why Kotlin?
Voxxed Athens 2018 - Why Kotlin?Voxxed Athens
 
Voxxed Athens 2018 - Java EE is dead Long live jakarta EE!
Voxxed Athens 2018 - Java EE is dead Long live jakarta EE!Voxxed Athens 2018 - Java EE is dead Long live jakarta EE!
Voxxed Athens 2018 - Java EE is dead Long live jakarta EE!Voxxed Athens
 
Voxxed Athens 2018 - How WebAssembly is changing the Web and what it means to...
Voxxed Athens 2018 - How WebAssembly is changing the Web and what it means to...Voxxed Athens 2018 - How WebAssembly is changing the Web and what it means to...
Voxxed Athens 2018 - How WebAssembly is changing the Web and what it means to...Voxxed Athens
 
Voxxed Athens 2018 - Going agile with kanban
Voxxed Athens 2018 - Going agile with kanbanVoxxed Athens 2018 - Going agile with kanban
Voxxed Athens 2018 - Going agile with kanbanVoxxed Athens
 
Voxxed Athens 2018 - Elasticsearch (R)Evolution — You Know, for Search...
Voxxed Athens 2018 - Elasticsearch (R)Evolution — You Know, for Search...Voxxed Athens 2018 - Elasticsearch (R)Evolution — You Know, for Search...
Voxxed Athens 2018 - Elasticsearch (R)Evolution — You Know, for Search...Voxxed Athens
 
Voxxed Athens 2018 - Graph databases & data integration
Voxxed Athens 2018 - Graph databases & data integrationVoxxed Athens 2018 - Graph databases & data integration
Voxxed Athens 2018 - Graph databases & data integrationVoxxed Athens
 

More from Voxxed Athens (18)

Voxxed Athens 2018 - Eventing, Serverless, and the Extensible Enterprise
Voxxed Athens 2018 - Eventing, Serverless, and the Extensible EnterpriseVoxxed Athens 2018 - Eventing, Serverless, and the Extensible Enterprise
Voxxed Athens 2018 - Eventing, Serverless, and the Extensible Enterprise
 
Voxxed Athens 2018 - Let’s Get Chatty with Conversational Interface with Java...
Voxxed Athens 2018 - Let’s Get Chatty with Conversational Interface with Java...Voxxed Athens 2018 - Let’s Get Chatty with Conversational Interface with Java...
Voxxed Athens 2018 - Let’s Get Chatty with Conversational Interface with Java...
 
Voxxed Athens 2018 - IBM Watson Machine Learning – Build and train AI models ...
Voxxed Athens 2018 - IBM Watson Machine Learning – Build and train AI models ...Voxxed Athens 2018 - IBM Watson Machine Learning – Build and train AI models ...
Voxxed Athens 2018 - IBM Watson Machine Learning – Build and train AI models ...
 
Voxxed Athens 2018 - We're going to talk about no sql, you can't join
Voxxed Athens 2018 - We're going to talk about no sql, you can't joinVoxxed Athens 2018 - We're going to talk about no sql, you can't join
Voxxed Athens 2018 - We're going to talk about no sql, you can't join
 
Voxxed Athens 2018 - Methods and Practices for Guaranteed Failure in Big Data
Voxxed Athens 2018 - Methods and Practices for Guaranteed Failure in Big DataVoxxed Athens 2018 - Methods and Practices for Guaranteed Failure in Big Data
Voxxed Athens 2018 - Methods and Practices for Guaranteed Failure in Big Data
 
Voxxed Athens 2018 - The secret for high quality software: Listen to your people
Voxxed Athens 2018 - The secret for high quality software: Listen to your peopleVoxxed Athens 2018 - The secret for high quality software: Listen to your people
Voxxed Athens 2018 - The secret for high quality software: Listen to your people
 
Voxxed Athens 2018 - A scalable maritime platform providing services through...
Voxxed Athens 2018 -  A scalable maritime platform providing services through...Voxxed Athens 2018 -  A scalable maritime platform providing services through...
Voxxed Athens 2018 - A scalable maritime platform providing services through...
 
Voxxed Athens 2018 - UX design and back-ends: When the back-end meets the user
Voxxed Athens 2018 - UX design and back-ends: When the back-end meets the userVoxxed Athens 2018 - UX design and back-ends: When the back-end meets the user
Voxxed Athens 2018 - UX design and back-ends: When the back-end meets the user
 
Voxxed Athens 2018 - Your Local Meet-up: Your Path to Crafts(wo)manship
Voxxed Athens 2018 - Your Local Meet-up: Your Path to Crafts(wo)manshipVoxxed Athens 2018 - Your Local Meet-up: Your Path to Crafts(wo)manship
Voxxed Athens 2018 - Your Local Meet-up: Your Path to Crafts(wo)manship
 
Voxxed Athens 2018 - The quantum computers are coming
Voxxed Athens 2018 - The quantum computers are comingVoxxed Athens 2018 - The quantum computers are coming
Voxxed Athens 2018 - The quantum computers are coming
 
Voxxed Athens 2018 - Serverless by Design
Voxxed Athens 2018 - Serverless by DesignVoxxed Athens 2018 - Serverless by Design
Voxxed Athens 2018 - Serverless by Design
 
Voxxed Athens 2018 - Getting real with progressive web apps in 2018
Voxxed Athens 2018 - Getting real with progressive web apps in 2018Voxxed Athens 2018 - Getting real with progressive web apps in 2018
Voxxed Athens 2018 - Getting real with progressive web apps in 2018
 
Voxxed Athens 2018 - Why Kotlin?
Voxxed Athens 2018 - Why Kotlin?Voxxed Athens 2018 - Why Kotlin?
Voxxed Athens 2018 - Why Kotlin?
 
Voxxed Athens 2018 - Java EE is dead Long live jakarta EE!
Voxxed Athens 2018 - Java EE is dead Long live jakarta EE!Voxxed Athens 2018 - Java EE is dead Long live jakarta EE!
Voxxed Athens 2018 - Java EE is dead Long live jakarta EE!
 
Voxxed Athens 2018 - How WebAssembly is changing the Web and what it means to...
Voxxed Athens 2018 - How WebAssembly is changing the Web and what it means to...Voxxed Athens 2018 - How WebAssembly is changing the Web and what it means to...
Voxxed Athens 2018 - How WebAssembly is changing the Web and what it means to...
 
Voxxed Athens 2018 - Going agile with kanban
Voxxed Athens 2018 - Going agile with kanbanVoxxed Athens 2018 - Going agile with kanban
Voxxed Athens 2018 - Going agile with kanban
 
Voxxed Athens 2018 - Elasticsearch (R)Evolution — You Know, for Search...
Voxxed Athens 2018 - Elasticsearch (R)Evolution — You Know, for Search...Voxxed Athens 2018 - Elasticsearch (R)Evolution — You Know, for Search...
Voxxed Athens 2018 - Elasticsearch (R)Evolution — You Know, for Search...
 
Voxxed Athens 2018 - Graph databases & data integration
Voxxed Athens 2018 - Graph databases & data integrationVoxxed Athens 2018 - Graph databases & data integration
Voxxed Athens 2018 - Graph databases & data integration
 

Recently uploaded

Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsHyundai Motor Group
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Unlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsUnlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsPrecisely
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 

Recently uploaded (20)

Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort ServiceHot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Unlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsUnlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power Systems
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 

Voxxed Athens 2018 - Clean Code with Java9+

  • 1. @mirocupak Miro Cupak VP Engineering, DNAstack 01/06/2018 Writing clean code with Java 9+
  • 2. @mirocupak Clarity, simplicity, brevity, humanity !2 http://www.osnews.com/story/19266/WTFs_m https://xkcd.com/1695/ https://theamericanscholar.org/writing-english-as-a-second-language/
  • 4. @mirocupak Features !4 • Factory methods for collections. • Improved try-with-resources. • Private methods in interfaces. • Stream API enhancements. • Extensions to Optional. • HTTP/2 client. • Type inference.
  • 6. @mirocupak Factory methods for collections !6 • Obtain immutable collections via of/ofEntries methods. • Create immutable copies of collections via copyOf (Java 10). • 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 and can be shared freely (no need for defensive copies). • Good performance. • Don’t create mutable collections unless necessary.
  • 8. @mirocupak Improved try-with-resources !8 • 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.
  • 10. @mirocupak Private methods in interfaces !10 • 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. • Use default methods for providing standard implementations for new interface code. • Don’t use default methods to extend existing interfaces unless necessary.
  • 12. @mirocupak Stream API enhancements !12 • Be aware of new stream methods: takeWhile, dropWhile, iterate. • Prefer collecting into immutable collections using toUnmodifiableList, toUnmodifiableSet, toUnmodifiableMap. • Check for convenience stream methods before converting to streams manually (e.g. Matcher, LocalDate). • 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.
  • 14. @mirocupak Extensions to Optional !14 • Use ifPresentOrElse instead of if-isPresent construct. • or provides a clean fluent way of chaining behaviour on Optionals. • Be aware of orElse* methods, e.g. the new orElseThrow (Java 10). • Use stream to take advantage of the lazy nature of streams and handle streams of Optionals. • Remember that isPresent is rarely the answer.
  • 16. @mirocupak HTTP/2 client !16 • 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.
  • 18. @mirocupak Diamond operator with anonymous classes !18 • Use <> for anonymous classes, like everywhere else. • <> improves readability and consistency. • Use anonymous classes only when needed (e.g. abstract classes, interfaces with multiple abstract methods…), otherwise replace with lambdas.
  • 19. @mirocupak Local variable type inference !19 • Does not replace static typing. • Generally good. • Reduces boilerplate and improves readability. • Helps with maintenance and refactoring. • Use for local variables with initializers (especially constructors) and for loops. • Can’t use for method formals, constructor formals, method return types, fields, catch formals, null or array initializers, lambdas, method references, or any other kind of variable declaration. • Consider whether to use when the generated type is not obvious. • But use for complex types when breaking chained or nested expressions with local variables. • Primitive types might surprise you, be careful (e.g. byte, short, long all inferred as int).
  • 20. @mirocupak Local variable type inference !20 • Be very careful about combining with <> and generic methods (e.g. var list = new ArrayList<>()). • Probably not the best idea to use with anonymous classes. • Use carefully chosen and expressive variable names. • Don’t use Hungarian notation. • Don’t rely on IDEs. • Minimize the scope of local variables. • Declare variable when it’s first used. • Declaration not containing an initializer (i.e. you can’t use var) often indicates the scope is not minimal. • Prefer for loops to while loops. • Keep methods small and focused. • Code to the interface pattern does not work, but that’s kind of OK.
  • 21. @mirocupak Questions? !21 More info: Session notes on Twitter. Blog: https://mirocupak.com/