Functions and APIs should employ explicit strategies for handling null/None values rather than leaving null handling ambiguous. Choose one of two approaches:
Functions and APIs should employ explicit strategies for handling null/None values rather than leaving null handling ambiguous. Choose one of two approaches:
Avoid: Returning None from functions without clear handling expectations, or requiring frequent isinstance/null checks due to inconsistent data formats.
Example of good null handling:
# Option 1: Design out nulls with defaults
def get_config(key: str) -> Config:
return config_dict.get(key, DEFAULT_CONFIG) # Never returns None
# Option 2: Explicit null contract
def get_node(node_id: str) -> Node:
node = self._find_node(node_id)
if node is None:
raise NodeNotFoundError(f"Node {node_id} not found")
return node
Example of problematic null handling:
# Ambiguous - callers don't know if they need to handle None
def get_node(node_id: str) -> Node | None:
# Returns None sometimes, unclear when
return self.nodes.get(node_id)
This approach prevents attribute errors, reduces defensive programming overhead, and makes null-handling expectations explicit in the codebase.
Enter the URL of a public GitHub repository