clean-code-functions
Installation
SKILL.md
Functions
Functions are the first unit of organization in a program. They're the verbs of the small language a program defines for its domain. These rules make that language read clearly.
1. Small, then smaller
- Functions should be short: a handful of lines, rarely more than about 20. There's no magic number, but if a function doesn't fit comfortably in your head, it's too long.
- Blocks inside
if,elseandwhileshould ideally be one line, usually a call to a well-named function. This keeps the outer function small and lets the inner name document the intent. - Indentation shouldn't exceed one or two levels. Nested structures are a sign to extract.
2. Do one thing
A function does one thing if all its steps are one level of abstraction below the function's name. Two practical tests:
- Can you extract another function from it with a name that isn't just a restatement of the implementation? If yes, it does more than one thing.
- Does it have "sections" (setup, validation, computation, output), perhaps separated by blank lines or comments? Functions that do one thing can't be reasonably divided into sections.