Back to all reviewers

consistent null checking

facebook/react-native
Based on 3 comments
Java

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.

Null Handling Java

Reviewer Prompt

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:

  • Use variable != null instead of null != variable for better readability
  • Implement defensive checks like action.hasKey("key") before accessing map values
  • Consider null-safe fallbacks: action.hasKey("label") ? action.getString("label") : null
  • Add explicit null checks for potentially nullable return values, even when parent objects are non-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;
}
3
Comments Analyzed
Java
Primary Language
Null Handling
Category

Source Discussions