Software Design/Naturality
Naturality is a code and interface quality which is the degree of either how much the code reads like prose in a natural language (usually, English), or how directly the code describes the processes and events in the problem domain rather obscures the processes and events behind abstractions or language forms which don't exist in the problem domain.
Naturality as an interface quality is the naturality of the code which uses the interface.
Examples
[edit | edit source]Double negation in boolean expressions: !notSomething
reads as "not not something" which doesn't happen in natural communication.
dog.bark()
expresses the process in the problem domain (the dog is barking) more directly than code like barking.do(dog)
or dogActionExecutor.submit(Barking(dog))
.
Relations to other software qualities
[edit | edit source]It may be easier to learn an interface that produces natural code and to use such an interface from memory because people learn and recall phrases in natural language throughout their lives outside of computer programming.
It may be easier to perceive code which reads like natural prose, thus the cognitive load of reading natural code may be lower.
In typical code, naturality closely correlates with clarity. However, there are exceptions: for example, Yoda conditions are exactly as clear as equivalent "normal" conditions (because the only thing different is the order of terms, not the number or complexity of programming language constructs or operators used), but less natural.
Relevant practices
[edit | edit source]- Avoid double negation in boolean expressions
- Choose programming language or framework similar to the language of the problem domain
- Transform data with pipeline operator
Sources
[edit | edit source]- Boswell, Dustin; Foucher, Trevor (2011). The Art of Readable Code. ISBN 978-0596802295. Chapter 12. "Turning Thoughts into Code"
- Bloch, Joshua (September 22, 2008). "Bumper-Sticker API Design".
Every API is a little language, and people must learn to read and write it. If you get an API right, code will read like prose.