Prioritize code simplification by removing unnecessary complexity, avoiding duplication, and leveraging existing utilities. This improves readability and maintainability while reducing potential bugs.
Key practices:
Example of simplification:
// Before: Unnecessary length check and duplication
if len(request.FirstAvailable) > 0 {
for _, subRequest := range request.FirstAvailable {
// process subRequest
}
}
// After: Direct ranging (safe for empty slices)
for _, subRequest := range request.FirstAvailable {
// process subRequest
}
// Before: Local variables for simple values
trueVal := true
spec.SetHostnameAsFQDN = &trueVal
// After: Use utility functions
spec.SetHostnameAsFQDN = ptr.To(true)
This approach reduces cognitive load, minimizes maintenance overhead, and makes code more self-documenting by removing unnecessary abstractions and leveraging well-tested standard patterns.
Enter the URL of a public GitHub repository