Bad Code in Forth

BASIC, C, and Pascal are rigid and constrain your ability to write bad code. Forth is more permissive. and you can be as awful as you please.

It has been said that Forth is an amplifier; it makes bad code very bad and good code very good. Here are some examples of the power of the dark side of the Forth.

This comes from an article published by an experienced programmer.

... if true else false then ...

Why not use 0<> which would be correct even in older Forths in which 1 was TRUE and 0 was FALSE?

How Forth represents TRUE and FALSE

The next also comes from a published article. A little factoring will improve it.

... if true foo ! else false foo ! then ...

Why not:

... if true else false then foo ! ...

or better yet:

0<> foo !

making 3 Forth words do what 9 did before.

One day, I saw this gem in my own code.

... dup swap ...

It resulted from a double lapse, for which I take full blame. I started with:

... dup 1+ swap ...

Later, when the 1+ was found to be incorrect, I deleted it, forgetting to remove the swap.

Some programmers like to add a word +under that increments the second stack item. It should be written in assembler for maximum efficiency, but here it is in Forth:

: +under swap 1+ swap ;

Finally, a classic, thanks to Mike Perry:

: decompose ( -- )   rot rot rot ;


Updated: June 23, 1996