3 ms·
The most important idea in Forth is factoring. Breaking complex tasks into small pieces. In an ideal Forth codebase, most definitions are about a line long, con
by RodgerTheGreat 11y ago
The most important idea in Forth is factoring. Breaking complex tasks into small pieces. In an ideal Forth codebase, most definitions are about a line long, contain at most one control structure and reference no named variables.
Forth's stack-oriented semantics make function calls very cheap- a function call and return is literally two instructions, whereas languages which use activation records will turn a function call into dozens of instructions- copying arguments around, backing up registers, etc. Since it's computationally inexpensive, you don't need to sweat over whether your optimizing compiler will manage to inline a function.
Short, simple and mostly pure functions are easy to test in isolation- there aren't deeply nested code paths to exercise.
Lots of short functions allow your codebase to reuse more of those functions- any redundancy can be collapsed together, both shrinking the codebase and improving test coverage. This effect is magnified in more complex programs.
You can do this sort of thing in other languages, but forth's syntax and semantics for function definition and invocation are both very lightweight, and this makes it much easier to apply.
- tehwalrus 11y agoIt sounds like what Clean Code[1] really wanted. I still think it's a great shame that all his examples are in Java (although, in fairness, they are very pseudocode-ish). [1] https://books.google.co.uk/books/about/Clean_Code.html?id=dwSfGQAACAAJ https://books.google.co.uk/books/about/Clean_Code.html?id=dw...