Software Design/Create "deep" functions

From Wikiversity
Jump to navigation Jump to search

Checklist questions:

  • If there are many small functions not making much sense in separation, could the logic be gathered into fewer larger functions?

This practice corresponds to the principle "Modules should be deep" from A Philosophy of Software Design.[1]

Why[edit | edit source]

When the logic is broken up in many small functions it ends up being arranged (across the boundaries of functions' bodies) not in the execution order but in some unnatural order. This, along with the need to jump between function calls and definitions makes it more difficult for readers to understand the overall logic.

Jumping between small function bodies and calls back and forth makes developers of the code to frequently lose the visual and semantic context. They have to keep more things in mind which is an extra cognitive load. With the frequent context loss, it may also be easier to make mistakes while developing such code.

Having defined many functions which are parameterless or have the same parameters makes it easier to make a programming mistake by calling a wrong function.

When some logic is split into many functions it takes more lines of code than when the same logic is organized in a single or a few functions.

Why not[edit | edit source]

References[edit | edit source]

  1. Ousterhout, John (2018). A Philosophy of Software Design. ISBN 978-1732102200.  Chapter 4 "Modules Should Be Deep"