Tag Archives: codestandards

Only 7 lines of code please

I ran across this question at Programmers.StackExchange discussing a coding guideline that states “Methods should not exceed 7 statements”.  I find these types of guidelines and the reactions to them quite amusing.

Let’s not forget the SOLID modeling principles before I begin to ramble.  Go ahead and review them for a minute.  I’ll wait…

Got it?  ok…  Programming Guidelines are written by Alpha Geeks for more intermediate and junior coders.  A rule like this one is set forth to force the uninitiated to think about how they can break down their objects and methods into much smaller, re-usable chunks.

 Consider this:  if the junior developers on your team are writing methods with 7 lines of code or less, you’re reducing the chances they’ll publish code with high cyclomatic complexity.  You’re project should end up with much simpler, less complex code just because of this rule.

Senior developers and architects who write boiler-plate code may violate this rule.  If you know WHY this rule is in place, then you know the hows, whens, and wheres about violating the 7 lines of code rule.  Those individuals are thinking about keeping complexity metrics down, and code coverage high.

Should you be writing 7 line methods?  A quick glance through one of my personal projects and the only ‘large’ methods I find are in unit tests…  and I’m okay with those.

Try using this rule for a week and see how it forces you to craft your code.  You might be surprised what you get.