IfSQ
SP-1—Routine Too Long
Defect Indicators: A program (method, module, routine, subroutine, procedure, or any named block of code) consists of more than 150 lines (including comments and blank lines).
Risks: Programs over 150 lines are more error-prone and therefore more expensive to maintain.
Assessment:
- Mark all the lines following the 150th line of the program, including comments and blank lines.
Remedy:
- Restructure or refactor your code into smaller, easy-to-understand chunks, OR
- Write a comment justifying the length of the program.
- Do not:
- Remove comments or blank lines to make it shorter,
- Put multiple commands on one line to make the program shorter.
References:
- An Experimental Investigation of Software Metrics and Their Relationship to Software Development Effort (Randy K. Lind, and Kenneth G. Vairavan), 1989,
- Analyzing Error-Prone System Structure (Richard W. Selby, and Victor R. Basili), 1991,
- Programming Productivity (Capers Jones), 1986.
Research Findings:
-
Best size for routines is 100-150 lines:
A study showed that when routines averaged 100 to 150 lines each, code was more stable and required less changes, -
Better not to exceed 143 lines in a routine:
Routines with 143 lines or less are 2.4 times less expensive to fix than larger routines, -
Long routines are more error prone:
A study at IBM found that the most error-prone routines were those larger than 500 lines of code.