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
Always handle potential null/None values defensively by:
.get()
for dictionary access instead of direct key accessExample:
# 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:
Enter the URL of a public GitHub repository