Functions in Coding
Functions should be small, do one thing
All things in the function should be at the same abstraction level. You can describe a function with only one level of abstraction by using “TO” paragraphs. This allows readability like a story, “To do a, you must do b”
TO RenderPageWithSetupsAndTeardowns, we check to see whether the page is a test page and if so, we include the setups and teardowns. In either case we render the page in HTML. 1
“if you can extract another function from it with a name that is not merely a restatement of its implementation” 1
Nested structures like if, else, while, for, try, etc should ideally call another function in their code block. This makes the code easier to read and understand. 2
Example
References
Footnotes
-
FUNCTIONS SHOULD DO ONE THING. THEY SHOULD DO IT WELL. THEY SHOULD DO IT ONLY, CLEAN CODE: A HANDBOOK OF AGILE SOFTWARE CRAFTSMANSHIP Robert C. Martin ↩ ↩2
-
How to Design Good Functions and Classes | Writing Clean Code
↩