Ir al contenido

Long method (ARCH702)

Esta página aún no está disponible en tu idioma.

Key dotnet.codequality.long-method · Severity warning · Category codequality · Principle Kiss

Very long methods carry multiple responsibilities and become hard to read and test. Logical steps should be extracted into separate methods.

public void ProcessOrder(Order order)
{
// validation
if (order is null) throw new ArgumentNullException();
if (order.Items.Count == 0) throw new InvalidOperationException();
foreach (var i in order.Items) { /* ... */ }
// pricing
decimal total = 0;
foreach (var i in order.Items) { total += i.Price * i.Qty; }
// tax, shipping, discount, persist, email... (dozens more lines)
}
public void ProcessOrder(Order order)
{
Validate(order);
var total = CalculateTotal(order);
Persist(order, total);
NotifyCustomer(order);
}

Break the method into smaller helpers that each do one thing, so every step expresses its intent through its name.