Prompt
Always handle potential null/None values defensively by:
- Using
.get()for dictionary access instead of direct key access - Checking for None/falsy values before accessing their properties
- 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