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.4Provide immutable decorators for sensitive mutable classes
- 1.5Use pattern matching for instanceof
- 1.6Consider using sealed classes and interfaces
- 1.7Use record patterns for destructuring
- 1.8Avoid constant interfaces
- 1.9Avoid global constant classes
- 1.10Favour composition over inheritance
- 1.11Consider using lambdas
- 1.12Avoiding Primitive Obsession
- 1.13Use the
@OverrideAnnotation - 1.14Use the
@FunctionalInterfaceAnnotation - 1.15Prefer returning Null-Objects over
null - 1.16Avoid
nullas a method parameter - 1.17Prefer enhanced loops over for loops
- 1.18Code against interfaces not implementations
- 1.19Use existing exceptions
- 1.20Validate method parameter
- 1.21Minimize method parameters for improved readability
- 1.22Prevent
NullPointerExceptionforStringcomparison - 1.23Safely cast
longtoint - 1.24Convert integers to floating-point for floating-point math operations
- 1.25Use generics in favor of raw types
- 1.26Prefer enums over
intconstants - 1.27Prefer the
==operator overequalsfor comparing enum values - 1.28Be aware of the contract between
equalsandhashCode - 1.29Use text blocks for multi-line strings
- 1.30Use always braces for the body of all statements
- 1.31Simplifying boolean expressions
- 1.32Pre-calculate the length in loops
- 1.33Avoid constructor instantiation of
String - 1.34Use
StringBuilderfor concatenation - 1.35Reduce lookups in collection containers
- 1.36Instantiate wrapper objects with
valueOf - 1.37Use
entrySetfor iterating - 1.38Use
isEmpty()forStringlength - 1.39Use switch expressions for concise control flow
- 1.40Avoid using the default case in switch statements for sealed classes
- 1.41Prefer method references over lambdas for conciseness
- 1.42Use the
orElseandorElseGetmethods for optional values - 1.43Enhance code clarity with
EnumMap - 1.44Use local variable type inference with
varjudiciously - 1.45Practice defensive programming
- 1.46Implement effective logging practices