Use explicit and clear patterns when handling null/None values to improve code readability and maintainability. Avoid complex conditional expressions, unnecessary type casting, and ambiguous nullable types.
Use explicit and clear patterns when handling null/None values to improve code readability and maintainability. Avoid complex conditional expressions, unnecessary type casting, and ambiguous nullable types.
Key principles:
if version is None: version = packages[0] if packages else None
version = version or packages[0]
2. **Use None to mean "unknown" and empty collections for "known empty":**
```python
# Use None when data availability is uncertain
requires_dist = None # "I don't know what the requirements are"
# Use empty list when you know there are no requirements
requires_dist = [] # "I know there are no requirements"
name = poetry.local_config.get(“name”, “”) headers = kwargs.get(“headers”, {}) or {}
if “name” in distribution.metadata: name = distribution.metadata[“name”]
4. **Avoid unnecessary nullable types:**
```python
# Don't make parameters nullable if they're never actually None
def configure_options(self, io: IO) -> None: # Not IO | None
# io is always provided by caller
# Put the default/expected case first
if output is None:
sys.stdout.write(content)
else:
with open(output, 'w') as f:
f.write(content)
if self._cache_control is None: return None
if self._disable_cache: return None
```
These patterns make null handling intentions clear, reduce the risk of runtime errors, and improve code maintainability by making the expected behavior explicit.
Enter the URL of a public GitHub repository