<cbbrowne@hex.net> wrote:
+---------------
| Kent M Pitman <pitman@world.std.com> 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...
+---------------
Indeed:
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)
(loop
(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,
spaghetti-wise.
-Rob
-----
Rob Warnock, 31-2-510 rpw3@sgi.com
SGI Network Engineering <URL:http://reality.sgi.com/rpw3/>
1600 Amphitheatre Pkwy. Phone: 650-933-1673
Mountain View, CA 94043 PP-ASEL-IA