Avoid long functions. Avoid deep nesting. Summary Short is better than long, flat is better than deep: Excessively long functions and nested code blocks are often caused by failing to give one function one cohesive responsibility, and both are usually solved by better refactoring. Discussion Every function should be a coherent unit of work bearing a suggestive name. When a function instead tries to merge such small conceptual elements inside a long function body, it ends up doing too much. Excessive straight-line function length and excessive block nesting depth (e.g., if, for, while, and try blocks) are twin culprits that make functions more difficult to understand and maintain, and often needlessly so. Each level of nesting adds intellectual overhead when reading code because you need to maintain a mental stack (e.g., enter conditional, enter loop, enter try, enter conditional, ...). ... Prefer better functional decomposition to help avoid forcing readers to keep as much context in mind at a time. Exercise common sense and reasonableness: Limit the length and depth of your functions |
|
|
Programming is usually taught by examples. |