Long method (ARCH702)
Ce contenu n’est pas encore disponible dans votre langue.
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.
Incorrect
Section titled “Incorrect”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)}Correct
Section titled “Correct”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.