Why Five Lines of Code

#56 - Refactoring–The Discipline for Writing Good Code - Christian Clausen

Topic 11 of 18

Published Sep 20, 2021 · Clip 25:57–30:01 (4:05)

Christian explains why five lines is the rule: one pass through a 2D array takes exactly five lines, so a lower limit would break the intent of the loop structure; a study he later learned of found methods averaging five to six lines have the fewest bugs, though that was luck. He addresses the criticism that splitting methods hurts readability by explaining his technique: let blank lines and structure show where methods should be, then let methods guide class creation (e.g., via common affixes), producing a fine-grained hierarchy that isolates invariants. He also advises using an editor with go-to-definition, or, with primitive editors, bubbling frequently-searched methods to the top of the file — an idea from the book 'Algorithms to Live By'.