Ensure null checks follow consistent patterns and proper ordering for better readability and safety. Always place the variable being checked on the left side of null comparisons, use defensive programming with hasKey() checks before accessing map values, and maintain null-safe fallbacks when appropriate.
Ensure null checks follow consistent patterns and proper ordering for better readability and safety. Always place the variable being checked on the left side of null comparisons, use defensive programming with hasKey() checks before accessing map values, and maintain null-safe fallbacks when appropriate.
Key practices:
variable != null
instead of null != variable
for better readabilityaction.hasKey("key")
before accessing map valuesaction.hasKey("label") ? action.getString("label") : null
Example:
// Good: Consistent ordering and defensive programming
if (!action.hasKey("name") || !action.hasKey("label")) {
throw new IllegalArgumentException("Unknown accessibility action.");
}
String actionLabel = action.hasKey("label") ? action.getString("label") : null;
// Good: Proper null check ordering
if (!mSendMomentumEvents || mPostSmoothScrollRunnable != null) {
return;
}
// Avoid: Inconsistent null check ordering
if (!mSendMomentumEvents || null != mPostSmoothScrollRunnable) {
return;
}
Enter the URL of a public GitHub repository