說明
Methods must not exceed 60 lines (excluding blank lines and comments). Methods longer than this are a signal that they are doing too much.
為什麼重要
Long methods are hard to: - Read — the reader must hold too much context - Test — too many branches to cover - Reuse — embedded logic cannot be extracted
Enforcement
<module name="MethodLength">
<property name="max" value="60"/>
<property name="countEmpty" value="false"/>
</module>
Rule: java:S138 — Methods should not have too many lines
正例
public OrderResult processOrder(Order order) {
validateOrder(order);
Order saved = orderRepository.save(order);
notifyCustomer(saved);
return OrderResult.success(saved.getId());
}
private void validateOrder(Order order) { /* ... */ }
private void notifyCustomer(Order order) { /* ... */ }
反例
public OrderResult processOrder(Order order) {
// 80+ lines of validation, persistence, notification, logging...
// ❌ does too much, impossible to test individual steps
}