Back to all reviewers

Validate and document nulls

deeplearning4j/deeplearning4j
Based on 6 comments
Java

Always handle null values explicitly and consistently throughout your codebase to prevent null pointer exceptions. Follow these guidelines: 1. **Validate nulls early** using Preconditions to provide clear error messages:

Null Handling Java

Reviewer Prompt

Always handle null values explicitly and consistently throughout your codebase to prevent null pointer exceptions. Follow these guidelines:

  1. Validate nulls early using Preconditions to provide clear error messages:
// Use explicit null checks with informative messages
Preconditions.checkNotNull(input, "Input cannot be null");
Preconditions.checkArgument(shape != null && shape.length > 0, "Invalid shape: %s", shape);
  1. Document null behavior clearly in Javadoc comments:
/**
 * Creates a buffer in the specified workspace.
 * @param workspace Optional memory workspace to define the buffer in, may be null 
 *                 (buffer not created in workspace when null)
 */
public DataBuffer createBuffer(DataType dataType, long length, boolean initialize, MemoryWorkspace workspace) {
    // Implementation
}
  1. Handle nulls at the source rather than requiring downstream methods to handle them, preventing defensive null checks throughout the codebase.

  2. Use appropriate types for potentially null values from collections:
    // Instead of:
    int numWords = (int) tokenizerConfig.get("num_words");
    // Use:
    Integer numWords = (Integer) tokenizerConfig.get("num_words");
    
  3. Consider using annotations like @NonNull or @Nullable to clearly indicate intent and enable static analysis tools to catch potential null issues.

Following these practices helps prevent null pointer exceptions and makes code more robust and maintainable.

6
Comments Analyzed
Java
Primary Language
Null Handling
Category

Source Discussions