Preface
- What about the code examples and typographic conventions?
- Which literature is this book based on?
- Giving feedback?
1Java Best Practices
- 1.1Avoid instantiation for utility classes
- 1.2Use immutable objects
- 1.3Prefer records for immutability
- 1.4Consider static factory methods for expressive construction
- 1.5Provide immutable decorators for sensitive mutable classes
- 1.6Use pattern matching for instanceof
- 1.7Consider using sealed classes and interfaces
- 1.8Use record patterns for destructuring
- 1.9Use unnamed variables and patterns
- 1.10Avoid constant interfaces
- 1.11Avoid global constant classes
- 1.12Favour composition over inheritance
- 1.13Consider using lambdas
- 1.14Avoiding Primitive Obsession
- 1.15Use the
@OverrideAnnotation - 1.16Use the
@FunctionalInterfaceAnnotation - 1.17Prefer returning Null-Objects over
null - 1.18Avoid
nullas a method parameter - 1.19Prefer enhanced loops over for loops
- 1.20Code against interfaces not implementations
- 1.21Use existing exceptions
- 1.22Use try-with-resources for closeable resources
- 1.23Validate method parameter
- 1.24Minimize method parameters for improved readability
- 1.25Prevent
NullPointerExceptionforStringcomparison - 1.26Safely cast
longtoint - 1.27Convert integers to floating-point for floating-point math operations
- 1.28Prefer
BigDecimalfor money and exact decimal calculations - 1.29Use generics in favor of raw types
- 1.30Prefer enums over
intconstants - 1.31Prefer the
==operator overequalsfor comparing enum values - 1.32Be aware of the contract between
equalsandhashCode - 1.33Use text blocks for multi-line strings
- 1.34Use always braces for the body of all statements
- 1.35Simplifying boolean expressions
- 1.36Pre-calculate the length in loops
- 1.37Avoid constructor instantiation of
String - 1.38Use
StringBuilderfor concatenation - 1.39Reduce lookups in collection containers
- 1.40Instantiate wrapper objects with
valueOf - 1.41Use
entrySetfor iterating - 1.42Prefer the sequenced collection accessors
- 1.43Use
isEmpty()forStringlength - 1.44Use switch expressions for concise control flow
- 1.45Avoid using the default case in switch statements for sealed classes
- 1.46Prefer method references over lambdas for conciseness
- 1.47Use the
orElseandorElseGetmethods for optional values - 1.48Use
Optionalas a return type, not as a general-purpose value - 1.49Enhance code clarity with
EnumMap - 1.50Prefer
java.timetypes and injectClock - 1.51Use local variable type inference with
varjudiciously - 1.52Practice defensive programming
- 1.53Prefer higher-level concurrency utilities over raw threads
- 1.54Implement effective logging practices
- 1.55Write tests as executable documentation
