Back to all reviewers

Defensive null checking

aws/aws-sdk-js
Based on 7 comments
JavaScript

Always perform explicit null/undefined checks before accessing properties or using values that could be null or undefined. Use strict equality checks rather than relying on JavaScript's truthy/falsy evaluation which can lead to subtle bugs.

Null Handling JavaScript

Reviewer Prompt

Always perform explicit null/undefined checks before accessing properties or using values that could be null or undefined. Use strict equality checks rather than relying on JavaScript’s truthy/falsy evaluation which can lead to subtle bugs.

Common patterns to adopt:

  1. Check objects before accessing their properties:
    // Bad:
    if (error.code === 'AuthorizationHeaderMalformed') { /* ... */ }
       
    // Good:
    if (error && error.code === 'AuthorizationHeaderMalformed') { /* ... */ }
    
  2. Be careful with array index checks:
    // Bad - indexOf returns 0 for first item which is falsy:
    if (list.indexOf(member)) { return true; }
       
    // Good:
    if (list.indexOf(member) >= 0) { return true; }
    
  3. Use typeof checks before accessing browser/environment-specific objects:
    // Bad:
    return window.localStorage !== null;
       
    // Good:
    return AWS.util.isBrowser() && window.localStorage !== null && typeof window.localStorage === 'object';
    
  4. Provide defaults for parameters that could be undefined:
    // Bad:
    function handleParams(params) {
      params.property = 'value'; // Fails if params is null/undefined
    }
       
    // Good:
    function handleParams(params) {
      params = params || {};
      params.property = 'value';
    }
    
  5. Use type checks for numeric values instead of truthy checks:
    // Bad - fails for zero values:
    if (params.$waiter.delay) {
      this.config.delay = params.$waiter.delay;
    }
       
    // Good:
    if (typeof params.$waiter.delay === 'number') {
      this.config.delay = params.$waiter.delay;
    }
    

Consistent null checking prevents the most common class of runtime errors and produces more predictable code.

7
Comments Analyzed
JavaScript
Primary Language
Null Handling
Category

Source Discussions