Always validate objects for null/nil before accessing their properties, and establish consistent patterns for handling null values throughout the codebase. This prevents nil pointer dereferences and ensures predictable behavior.
Always validate objects for null/nil before accessing their properties, and establish consistent patterns for handling null values throughout the codebase. This prevents nil pointer dereferences and ensures predictable behavior.
Three key practices:
// Good: Check for nil first if returnedObj == nil { return nil, errors.New(“cannot enrich nil object”) } accessorReturned, err := meta.Accessor(returnedObj)
2. **Use proper objects instead of nil values:**
```go
// Bad: Using nil objects that may cause dereference errors
return newRecordingRule(context.Background(), models.AlertRuleKeyWithGroup{}, 0, nil, nil, st, log.NewNopLogger(), nil, nil, writer.FakeWriter{}, nil, nil)
// Good: Use proper initialized objects
mockClock := clock.NewMock()
retryConfig := RetryConfig{
MaxRetries: 3,
RetryInterval: time.Second,
}
return newRecordingRule(context.Background(), models.AlertRuleKeyWithGroup{}, retryConfig, mockClock, nil, st, log.NewNopLogger(), nil, nil, writer.FakeWriter{}, nil, nil)
When handling special cases like empty strings or missing values, document the expected behavior and ensure all developers follow the same patterns.
Enter the URL of a public GitHub repository