Java best practices summary
This document is an extension of the Software engineering best practices summary, dedicated to Java-specific issues.
Dates
- Use the LocalDateTime API (LocalDate, LocalTime, LocalDateTime, etc.) instead of other solutions (like JodaTime)
Lambdas and Streams
- Wherever it makes sense, use Java 8’s streams – they are usually more readable than classic for-loops
- Every function call after .stream() should start from a new line
- If an anonymous function has more than one line, consider extracting it into a separate method
- Use method references instead of lambdas if doing so will make the code shorter and more readable; Wherever using a method reference would make the code longer and less readable, use lambdas
- Read more:
Bloch, J. (2018). Effective Java. (Chapter 7, Topic 43)
- Read more:
Optionals
- Prefer .orElse() instead of .isPresent()