Zum Inhalt springen

Long Method (ARCH5180)

Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.

Key go.quality.long-method · Severity info · Category quality

Overly long functions do several things, are hard to read, and hard to test. Extract logical steps into separate functions.

func ProcessOrder(o Order) error {
// validation, pricing, stock, payment, notify... 80+ lines
if o.ID == "" { return errInvalid }
total := 0.0
for _, i := range o.Items { total += i.Price * float64(i.Qty) }
// ... dozens more lines
return nil
}
func ProcessOrder(o Order) error {
if err := validate(o); err != nil {
return err
}
total := priceOrder(o)
return finalize(o, total)
}

Break the function into meaningful steps and extract each into its own single-responsibility function.