Reduce Nesting

Code should reduce nesting where possible by handling error cases/specialconditions first and returning early or continuing the loop. Reduce the amountof code that is nested multiple levels.

BadGood
  1. for , v := range data {
  2. if v.F1 == 1 {
  3. v = process(v)
  4. if err := v.Call(); err == nil {
  5. v.Send()
  6. } else {
  7. return err
  8. }
  9. } else {
  10. log.Printf("Invalid v: %v", v)
  11. }
  12. }
  1. for , v := range data {
  2. if v.F1 != 1 {
  3. log.Printf("Invalid v: %v", v)
  4. continue
  5. }
  6.  
  7. v = process(v)
  8. if err := v.Call(); err != nil {
  9. return err
  10. }
  11. v.Send()
  12. }