Go to the first, previous, next, last section, table of contents.


Good Factoring

Good code factoring is a concept well-understood in the Forth world, but perhaps not as widely appreciated elsewhere.

It is common to find, in code that merely works, function after function that differ only slightly, fragments of code that repeat over and over with minor variations.

Such code is easy to write, even for a novice: Simply copy an existing bit of code that does something similar to what is wanted, modify a couple of lines -- presto!

Such code is also wearisome to read -- the same code must be read over and over, flipping back and forth to figure out just what the differences are, and guessing what might be the point of those differences -- and even more wearisome to maintain and improve, as each fundamental transformation on the code must be repeated in endless little variations through forests of not-quite-identical code trees.

The ideal well-factored program says each thing exactly once.

There is no simple recipe for achieving this. Some languages make achieving it very difficult. Muf, with its economical function-call syntax and support for (for example) custom control structures, offers much better factoring support than most contemporary programming languages.

Will Strunk was not a programmer, but his prime dicti apply forcefully to programs and prose alike:

Eschew obfustication.
Omit needless words!
Omit needless words!
Omit needless words!

Learn to habitually watch your code as you write, wondering "Have I seen code like this before"? Look for ways to factor out the common element and write it but once, keeping separate only the quintessential differences.


Go to the first, previous, next, last section, table of contents.