HOW TO WRITE GOOD FORTH
FORTH suffers from a combination of bad press arid bad programmers. It is actually easier to write good FORTH than good anything else, since it is flexible, imposes few restrictions, and extracts no penalty for short programs, (Short programs, incidentally, are where it's at - breaking things up into small, sensible, lucid pieces is a key to good programming).
Why write good FORTH?
Good code is faster to write, if you consider total time, since it comes from clear thinking and good organization, arid it is much easier to debug. You can understand and modify good code in amazingly shorter time*
Finally, you're much more employable if you write good code* Should a prospective employee tell me that he or she prides him or herself in writing good, clear, maintainable, well-documented FORTH I would, after picking myself up off the floor, endeavor to hire the person or. the spot.
How to write good FORTH:
You need to (1) know what good FORTH is, (2) discipline yourself to write it. Item (1) will be addresed[
sic] next. For (2), I've observed that a few thoughtful rewritings of a reasonably sized piece of code will not only help a decent programmer understand what good code is, but will also establish the necessary habits so that writing passable code becomes almost automatic, and good code a distinct possibility.
What is good FORTH?
Good FORTH consists of short, well thought out pieces. A word should rarely take up more than half a screen. If you use more than that, see whether you can break up the word into smaller, more coherent pieces* A good FORTH word is one which is easy to understand, and which you might be tempted to use again elsewhere.
Good FORTH is vertical, not horizontal. That is, there are few words per line and the lines are left-indented in an intelligent fashion. DOs and LOOPs, BEGINs and UNTILs, IFs, TH£Ns, and ELSEs, etc. are all left justified, with inner groupings to the right of outer ones. Take a look, at the Swarthmore source code to see what this means.
Each line should contain no more than a single idea.
A comment accompanying each word should show its effect on the stack, preferably with helpful mnemonic symbols for the stack, elements.
Words should be liberally sprinkled with comments. More lengthy comments can be put after the "-->" or ";S". The best code is almost self-documented.
The first line of a screen should consist of a comment which describes the contents of the screen. You should rarely start with more than half a screen full of code.
Will you go broke buying the number of disks necessary to write good FORTH? Hardly. Even in this expansive forn, FORTH gets you a lot of mileage. Moreover, few items connected with computers are as cheap as disk space. Finally, truth to tell, revisions and reworking usually result in screens filling up more than the minimal recommended (starting minimally gives you room for such expansion).
FORTH is supposedly a "stack oriented" language, but the programmer should not be stack oriented. Juggling several elements or. the stack can cause a surprising number of errors. In almost all situations, it's much clearer and leads to many fewer errors if you introduce sensibly named variables. It is hard to find a situation when such a practice will measurably slow down a program. It is possible to juggle stack elements by sending them to and from the return stack (it's possible to do lots of wild things in FORTH), but it's usually just not worth it.
If you simply must play with several items or. the stack, you're probably best off making up special stack manipulation words. If a word deals with several items on a stack, I've also found it very helpful to put in full comment lines within the word which show the stack, changes in clear mnemonics[sic],
Well-thought-out names can contribute immensely to good code. It's worth spending tine making up useful names (self-documenting code, again). One helpful approach is to try to name what the word does, not how it does it. Don't use an unidentified nunber in a word. Either identify it in a content or define a constant (with a good name) arid use it in the code.
Avoid abbreviations when you're writing code. The extra typing is trivial and the clarity introduced is considerable. It's fair and reasonable to introduce abbreviations in a testing/debugging session, however.
Is our code good?
It varies. It's getting better. At first we committed the negation of the above ideas. Look over our source code and see what you like and what is clear. There are probably 8 different prograMMers represented on the SwarthMore disks, so there are lots of different styles, ideas, arid approachs.
Further thoughts or. writing good FORTH are to be found in Leo Brodie's Starting FORTH.