Why this matters
Prevents NullPointerException, documents preconditions, and fails fast near the source.
Validate method inputs up front with null checks or use Objects.requireNonNull and meaningful messages.
Prevents NullPointerException, documents preconditions, and fails fast near the source.
Side-by-side examples engineers can pattern-match during review.
String cityLower = city.toLowerCase();Objects.requireNonNull(city, "city required");
String cityLower = city.toLowerCase();user.getAddress().getCity().toLowerCase()String city = Optional.ofNullable(user)
.map(User::getAddress).map(Address::getCity)
.orElse("unknown");From the same buckets as this rule.
Check if loops use equality operators (== or !=) in termination conditions. These can lead to infinite loops if the condition is never met exactly. Instead, use relational operators like < or > for safer loop termination.