User Tools

Site Tools

Translations of this page:

en:various:forth_is_readable

Forth is readable

Answer to the typical “Forth is unreadable” lament on Usenet: http://groups.google.com/group/comp.lang.forth/msg/aa20040e2d763089

Sorry, that's because your problem is not my problem. I can read my code. I can read code written by other Forthers. And, as I told you again and again, it took me a few months to learn that — read code from other Forthers, tight, “undocumented” code. A programming language should be readable by itself, and comments just disturb you. When I read Shakespeare or Goethe, I don't read an annotated version (they exist), I read the unannotated original. That's no trouble for me.

I also often look into the Linux kernel. I can't follow your argument that C programmer document their code. It's simply not there (in the Linux kernel), the code is just as terse and uncommented as any other hacker's code I've looked at. The few comments I have read, e.g. in the horrible unreadable TCP/IP stack of Linux are what should be function boundaries, i.e. clues for better factoring. If the programmer had a sense for that, the comments would vanish.

If you use excessive comments from the start of your first programming language, you'll never learn how to read the programming language itself, because you always rely on the text on the right. That's wrong. It reminds me of my Chinese course, where through half of the book all glyphs where annotated with the transscript. I didn't start learning to easily recognize the glyphs until the transscripts vanished, and it was a tough time first.

Chinese glyphs are clearly unreadable. Yes, seriously, for us westerners with our long-time experience with linearly ordered alphabets, a two-dimensional arranged alphabet is difficult to learn (Chinese glyphs really are composed of around 500 different elements or ~200 “radicals”, of which most of them have variations). But on the other hand, this is a very successful language (in terms of total texts written in it, overall time of usage, and overall user number).

You can't write really long texts with it, because even the longest one is only one fourth of the same translated to Russian; and a lot of famous Chinese writers have the tendency to create four-glyph aphorisms instead of Dostoevsky-like novels ;-). That includes that Chinese authors, especially poets, favor terse ambiguous language (to the edge of unreadability ;-), while Russian authors favor wordy, precise, redundant language. And it's not just the Russians, it's the whole of Europe.

You don't win a Nobel prize for writing Chinese literature; the fact that the year 2000 laureate is of Chinese origin doesn't count, he writes thick wordy books in French. Note: Chinese writers can produce thick books. They just pack in an awful lot of content.

So it boils down to Forth not being accepted as a mainstream language, because it's so different. It takes some hard work to get into Forth (or Lisp, or any other unorthodox language - but if you think of it, it's hard work to get into any computer language, it just depends on which you come across first). This isn't helped by putting translations into the sources, because after all, the reader has to learn Forth, not the translated stuff.

If you leave the familiarity argument out of it, and just look at what are universal attributes that make a computer language readable, Forth offers a lot to it:

  • Words are written linearly, i.e. the flow of operations follows the reading flow. No need to rearrange the order in your head.
  • Programs are written linearly, too. New words only build on words previously defined. You don't have to seek in the text, you just read and remember. A Forth program should be written as consistent, bottom-up story.
  • Words are short, and are meant to do one thing. That's the design philosophy, but Forth allows to keep words shorter and do simpler things than many other languages, since the factoring overhead is smaller.
  • While the vocabulary of a Forth system typically is large, the frequently used words are out of a small set. It is important to steer novices towards those frequently used words, because they are efficient.
    Chuck Moore recently revised his preferred set of simple words to use, his “machine Forth”. It's also a cleaner separation of language and library (primitive and high-level functions).
  • The Forth semantic model is very simple. You can understand what a word does without much background. Nothing is complicated enough that it must be treated as a black box.

There are also things that make Forth difficult:

  • Implicit parameter passing takes away variable names. You must keep them in your head. Good Forth definitions don't have many variables floating around; my advice is if you have more than four variables active at a time, refactor; if you are in despair, use locals. The definition boundaries are clear, because of the stack effect comment.
  • Forth uses lots of @!>,? in words. The meanings of these glyphs (they are used as such, not as letters) seems to be a problem. People seem to accept all the strange signs in C more easily, because there they are punctuations or operators, not parts of a name.

And about obfuscated code

Answer to obfuscated code using cheese names from John Passaniti http://groups.google.com/group/comp.lang.forth/msg/ae42b916b5d4c1f4

You gave us some obfuscated code and you showed that obfuscated code is more difficult to understand in Forth than in other languages. I don't find that very conclusive, since other languages usually have a fixed syntax for things like assignments and procedure calls, and therefore, you can't so easily obfuscate that part.

Forth is just a language. Programmers use patterns, even in Forth. People have tried to describe nouns as “pushing something on the stack”, adjectives as “transforming something on the stack”, and verbs as “taking something away from the stack”. Actually, it looks like most people use verbs that combine several stack elements and still do leave a result or so, creating a syntax tree (more like Chinese, where every subject verb object combination is a subject of its own, so you can do subject verb object verb object verb object - in UPN of course you do subject object verb object verb …).

However, with a non-obfuscated Forth program and some experience in the language, reading other programs is not really that hard. You don't need that full mental model. There are enough clues around how the words will affect the stack. This even works with cheese:

: obatzter 
  camembert 250 gramm melt 
  limburger 250 gramm squash add 
  cream-cheese 125 gramm squash add 
  cream 50 ml add 
  mustard 1 spoons add 
  egg chop add 
  2 0 DO  onion chop chop chop add  LOOP 
  paprika 1 small-spoons add 
  beer 4 spoons add 
  salt dash add pepper dash add mince ; 

I'm sure you can write up all the stack effects including the effect of OBATZTER itself. I'm even sure that you'll be able to create an obatzter yourself. Warning: the program is not optimized for performance, as melting a camembert takes its time.

en/various/forth_is_readable.txt · Last modified: 2013-06-06 21:26 (external edit)