Safe Navigation Patterns
Overview
Null dereference bugs are common in Salesforce codebases with deep relationship traversal and optional fields.
Use explicit null-safety patterns consistently so code is both safe and readable.
Consensus Best Practices
- Use safe navigation operator (
?.) where available and clear. - Use guard clauses for method entry validation.
- Keep deep property traversal in helper methods when reused.
- Prefer early return over nested
ifpyramids. - Write tests for null and populated paths.
Core Patterns
Pattern 1: Guard Clause Entry
public static String getAccountName(Contact c) {
if (c == null || c.Account == null) return null;
return c.Account.Name;
}
Pattern 2: Safe Navigation Operator
String accountName = contact?.Account?.Name;
String ownerEmail = contact?.Account?.Owner?.Email;
Pattern 3: Null-Safe Map Access
Contact oldRow = oldMap?.get(row.Id);
if (oldRow == null) return;
Pattern 4: Fallback Defaults
String label = contact?.Preferred_Name__c;
if (String.isBlank(label)) {
label = contact?.FirstName != null ? contact.FirstName : 'Customer';
}
When to Avoid Overuse
- Don’t hide required-data contracts behind silent null defaults.
- Don’t use null-safe chains when missing data should fail fast.
Common Failure Modes
- Null-safe chains returning null silently for mandatory values.
- Deep traversal repeated in multiple classes.
- Tests only cover happy path.