Back to all reviewers

Defensive null value handling

boto/boto3
Based on 7 comments
Python

Always handle potential null/None values defensively by: 1. Using `.get()` for dictionary access instead of direct key access 2. Checking for None/falsy values before accessing their properties

Null Handling Python

Reviewer Prompt

Always handle potential null/None values defensively by:

  1. Using .get() for dictionary access instead of direct key access
  2. Checking for None/falsy values before accessing their properties
  3. Initializing None defaults explicitly rather than using mutable defaults

Example:

# Bad:
def process_response(response):
    items = response['items']  # May raise KeyError
    return items['data']  # Nested access compounds risk

# Good:
def process_response(response, extra_params=None):
    if extra_params is None:
        extra_params = {}
    
    items = response.get('items')
    if not items:  # Handles None and empty dict
        return None
    
    return items.get('data')  # Safe nested access

This pattern:

  • Prevents KeyError exceptions from missing dictionary keys
  • Avoids NoneType attribute errors
  • Maintains consistent return types
  • Makes null cases explicit and intentional
  • Improves code robustness and maintainability
7
Comments Analyzed
Python
Primary Language
Null Handling
Category

Source Discussions