Zum Inhalt springen

Long Method (ARCH4180)

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

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

Overly long methods take on more than one responsibility and become hard to read and test. Split the logic into well-named helper methods.

void processOrder(Order o) {
validate(o);
// 40+ lines: pricing, tax, discount,
// inventory deduction, invoice, email, logging...
double tax = o.total() * 0.18;
double discount = o.total() > 1000 ? 0.1 : 0;
inventory.deduct(o.items());
invoices.create(o, tax, discount);
mailer.send(o.customerEmail(), buildBody(o));
audit.log("order " + o.id());
}
void processOrder(Order o) {
validate(o);
Pricing p = price(o);
inventory.deduct(o.items());
invoices.create(o, p);
notify(o);
}

Break the method into small, named helpers that each do one thing.