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.9Avoid constant interfaces
- 1.10Avoid global constant classes
- 1.11Favour composition over inheritance
- 1.12Consider using lambdas
- 1.13Avoiding Primitive Obsession
- 1.14Use the
@OverrideAnnotation - 1.15Use the
@FunctionalInterfaceAnnotation - 1.16Prefer returning Null-Objects over
null - 1.17Avoid
nullas a method parameter - 1.18Prefer enhanced loops over for loops
- 1.19Code against interfaces not implementations
- 1.20Use existing exceptions
- 1.21Use try-with-resources for closeable resources
- 1.22Validate method parameter
- 1.23Minimize method parameters for improved readability
- 1.24Prevent
NullPointerExceptionforStringcomparison - 1.25Safely cast
longtoint - 1.26Convert integers to floating-point for floating-point math operations
- 1.27Prefer
BigDecimalfor money and exact decimal calculations - 1.28Use generics in favor of raw types
- 1.29Prefer enums over
intconstants - 1.30Prefer the
==operator overequalsfor comparing enum values - 1.31Be aware of the contract between
equalsandhashCode - 1.32Use text blocks for multi-line strings
- 1.33Use always braces for the body of all statements
- 1.34Simplifying boolean expressions
- 1.35Pre-calculate the length in loops
- 1.36Avoid constructor instantiation of
String - 1.37Use
StringBuilderfor concatenation - 1.38Reduce lookups in collection containers
- 1.39Instantiate wrapper objects with
valueOf - 1.40Use
entrySetfor iterating - 1.41Use
isEmpty()forStringlength - 1.42Use switch expressions for concise control flow
- 1.43Avoid using the default case in switch statements for sealed classes
- 1.44Prefer method references over lambdas for conciseness
- 1.45Use the
orElseandorElseGetmethods for optional values - 1.46Use
Optionalas a return type, not as a general-purpose value - 1.47Enhance code clarity with
EnumMap - 1.48Prefer
java.timetypes and injectClock - 1.49Use local variable type inference with
varjudiciously - 1.50Practice defensive programming
- 1.51Prefer higher-level concurrency utilities over raw threads
- 1.52Implement effective logging practices
- 1.53Write tests as executable documentation
