C# – Coding Guideline : Methods shouldn’t contain more than 7 statements

ccoding-standardsmaintainability

I was looking through the AvSol Coding Guidelines for C# and I agree with nearly everything but I'm really curious to see what other think of one specific rule.

AV1500

Methods should not exceed 7 statements A method that requires more
than 7 statements is doing too much, or has too many responsibilities.
It also requires the human mind to analyze the exact statements to
understand what the code is doing. Break it down in multiple small and
focused methods with self-explaining names.

Do most of you follow this rule? Even if there's little to be saved from creating a new method (Your code is still DRY) aside from greatly increasing readability? And is your number still as low as 7? I would tend more toward 10.

I'm not saying I violate this rule all over the place–on the contrary, my methods are 95% small and focused but saying you should never violate this rule really blew me away.

I really just want to know what everyone thinks of NEVER violating this rule (It's a '1' on the coding standard–meaning NEVER do this). But I think you'd have trouble finding a codebase that doesn't.

Best Answer

This is a "standards smell" to me. Whenever I see coding standards with specific limits in them, I worry. You almost always run into a case where a method needs to be bigger than the standard allows (whether it's line length/count, number of variables, number of exit points, etc). Standards should be more like guidelines, and allow sufficient leeway for exercising good judgement. Don't get me wrong, it's good to have standards, but they shouldn't become "micromanagement by proxy".