The key advantages are: factoring, interactive development and Forth's extensible compiler.

Factoring

For example, to convert a byte from packed binary-coded decimal to hexadecimal you might write a macro in ANS C:

#define BCDtoHEX(b) ((b)-6*((b)>>4))

The equivalent in ANS Forth would be a word:

: BCD>Hex ( nibble-nibble -- byte )
    DUP 4 RSHIFT 6 * - ;

Here ( nibble-nibble -- byte ) is a comment indicating the changes to the topmost values on Forth's Data Stack and 4 RSHIFT is equivalent to >>4. Note that Forth needs a minimum of variables like b, preferring instead to manipulate the values on the Stack with words like DUP which duplicates the top value.

The advantages from factoring will now become clear. BCD>Hex is a first-class word in Forth and can be tested as soon as it has been written with the command

1 BCD>HEX .

which uses the . word to print the result of the conversion. Testing all the values from 0 to 99 can be done in a single line too.

To test the C macro BCDtoHEX , you must write, compile, link and test a small program or possibly use a debugger to poke values into a register, step through the macro code and check the result. Neither solution is of the same order of convenience as Forth.

Forth makes it so easy to write and test short routines (known as factoring) that it leads to a very different and reliable style of programming.

Interactive Development

Embedded systems are usually developed on a PC host using a cross-compiler that delivers a program which is downloaded to the target and tested by running under a debugger. This is a lengthy and intrusive procedure which is quite unnecessary for Forth users.

Instead, because the Forth compiler is incremental, each word can be written on the host and immediately compiled and tested on the target (with no need for debugger or emulator) before proceeding to the next word. In a field where testing may consume more than 50% of the project timescale, this approach (known as "umbilical Forth") brings major benefits.

Extensible Compiler

The Forth compiler is open and uniquely extensible. Programmers are encouraged to provide their own extensions to match the application they are building. The array construct appears in most languages - sometimes the size of the array is fixed, sometimes dynamic, the first element may be addressed as element no. 1 or no. 0 or the array may be associative. The data may be stored in RAM or on disk and bounds-checking may or may not be available.

In contrast, Forth has no array construct but instead provides simple tools to build whatever type of array is appropriate for your current application - providing an outstanding degree of freedom for the programmer.

This approach is equally apparent in object-oriented programming where a number of high-performance extensions have been published, the simplest providing efficient inheritance and polymorphism in just 12 short lines of code .

This openness and flexibility has intrigued a generation of programmers.


Footnotes:

12 line Object-Oriented Extension for Forth by Bernd Paysan.