Subject: Re: avoiding global vars.
From: (Rob Warnock)
Date: 13 Apr 2001 10:17:09 GMT
Newsgroups: comp.lang.lisp
Message-ID: <9b6jn5$2hqo$>
<> wrote:
| Kent M Pitman <> writes:
| > Can you say why are global variables to be avoided?  I think there
| > are soemtimes reasons, but often I think the reasons are imagined.
| As for trying to avoid globals, I'd think the "evil" to be that having
| vast quantities of them not sitting in structures is rather like using
| lots of GOTOs.  Obviously code's going to contain them, but you
| certainly want to keep it structured...


	Wulf, W. A., and Shaw, M., "Global Variable Considered Harmful",
	SIGPLAN Notices Vol. 8, No. 2, February 1973. 

which is one of many replies to:

	Edsger W. Dijkstra, "Go To Statement Considered Harmful",
	Letters to the Editor, CACM, Vol. 11, No. 3, March 1968.

Wulf & Shaw showed that one can trivially map arbitrary "GOTO"-filled
spagetti code to GOTO-less "pure" structured programming style using
a single LOOP containing a single CASE, and a single global variable.
Simply label each basic block of the spagetti code with a number, then:

	(defvar *pc* 0)

	  (case *pc*
	    (0 ...block 0 goes here... (setf *pc* (if (some_test) 3 17)))
	    (1 ...block 1 goes here... (setf *pc* (if (other_test) 22 96)))
	    (2 ...block 2 goes here... (setf *pc* (if (y_a_test) 53 35)))
	    ...and so on...))

However, if you're of the functional programming persuasion, you might
say that what they *should* have called the paper was "Assignment To
Global Variable Considered Harmful", assignment being the real villain,


Rob Warnock, 31-2-510
SGI Network Engineering		<URL:>
1600 Amphitheatre Pkwy.		Phone: 650-933-1673
Mountain View, CA  94043	PP-ASEL-IA