[comp.lang.forth] Forth stack comments

saari@robotman.Sun.COM (Michael Saari x63609) (11/10/89)

Forth has been accused of being an unsuitable programming language,
because "Forth code is unreadable (by humans)".  I do not feel that
this is an inherent aspect of the language.  However, I think that
many of the *examples* of Forth code that people look see *are* in
fact unreadable.

I suspect that much of this is due to that fact that since Forth
allows such close control of the machine, that it is largely preferred
by hardware hackers, who never learned a basic law of programming:

"Thou shalt *always* write code which can be easily understood by people
other than thyself."

At a recent Forth conference, I conducted a straw poll which greatly
reinforced this opinion.  At the question "How many people here feel
comfortable dealing with *hardware* issues?" about 80% of the 60 or so
people raised their hands.  At the question "How many people here
studied programming in school?", about 20% raised their hands... And
this is at a *software* conference, mind you.

Anyway, here's my main point.  The lowly Forth Stack Comment
( inputs -- outputs ) should be considered an absolutely *vital*
component of EACH AND EVERY DEFINITION.  Every time I see some
published code, without stack comments, I want to go shoot someone!!

Consider that you *need* to know the stack effect to use *any* word.

Consider trying to infer the stack effect of a word by tracing through
the code; when it then calls a previous (unfamiliar) word, you have a
nasty nesting problem with comprehension.

Consider that parameter passing on the stack is an integral part of
Forth - if you can't understand the stack effect you can't understand
the code.  Never mind that the stack effect is obvious to *you*, the
programmer.  That's not the point!

Consider a large C program, where every subroutine was written as:
calculate-residue ( x, y, z, a, b, c )   /* Only some are used */
print-results ( x, y, z, a, b, c )   /* Only some are used */
get-modifiers ( x, y, z, a, b, c )   /* Only some are used */
You can see the challenge to comprehension.  At least here you can
still easily tell which parameters are used with which sub-calls!

Even if a word has no overall stack effect, a simple ( -- ) makes
this clear to the reader, who is left wondering otherwise.

So when you're writing some Forth code, ask yourself "Am I truly the *only*
person who will *ever* read this code?"  If the answer is "No", then
*please* include a stack comment with the start of EVERY definition.
And if you publish code to the net or elsewhere, this is doubly
important.  

Thank you.

Mike Saari
saari@robotman@sun.COM
Mike Saari
saari@robotman.sun.COM

peter@ficc.uu.net (Peter da Silva) (11/11/89)

In article <127679@sun.Eng.Sun.COM> saari@robotman.Sun.COM (Michael Saari x63609) writes:
> So when you're writing some Forth code, ask yourself "Am I truly the *only*
> person who will *ever* read this code?"  If the answer is "No", then
> *please* include a stack comment with the start of EVERY definition.

And if the answer is "Yes", go back and think about it some more.
-- 
`-_-' Peter da Silva <peter@ficc.uu.net> <peter@sugar.hackercorp.com>.
 'U`  --------------  +1 713 274 5180.
"*Real* wizards don't whine about how they paid their dues"
	-- Quentin Johnson quent@atanasoff.cs.iastate.edu