Strive to simplify code structure by eliminating unnecessary complexity. This includes: 1. **Move conditions higher in the code** when appropriate to avoid unnecessary operations. Check conditions as early as possible to fail fast or skip work.
Strive to simplify code structure by eliminating unnecessary complexity. This includes:
// Bad
if (someCondition) {
// do work
}
if err := r.Update(ctx, profileIns); err != nil {
return err
}
// Good
if (someCondition) {
// do work
if err := r.Update(ctx, profileIns); err != nil {
return err
}
}
// Bad
if !ok || existingValue != v {
ns.Labels[k] = v
}
// Good
ns.Labels[k] = v
// Bad
if pod.Status.ContainerStatuses[i].Name == instance.Name &&
pod.Status.ContainerStatuses[i].State != instance.Status.ContainerState {
// handle condition
}
// Good
if pod.Status.ContainerStatuses[i].Name != instance.Name {
continue
}
if pod.Status.ContainerStatuses[i].State == instance.Status.ContainerState {
continue
}
// handle condition
// Bad
if nodename != "" {
// lots of code here
}
// Good
if nodename == "" {
return nil
}
// lots of code here with less indentation
This makes your code more readable, maintainable, and less error-prone.
Enter the URL of a public GitHub repository