Rob Cozens wrote:

> What is spaghetti code and how should one avoid it?

Historically, spaghetti code dates back to the early days of BASIC, when
subroutines weren't available and the only way to reuse code was to use
GOTO to jump around a single program. For a program of any complexity
(say, anything over about 100 lines) the source code rapidly became a
jumble of jumps, a veritable tangle of spaghetti-like paths.

The use of procedures and sub-routines makes it easier to reuse code,
and event driven code reduces the tangle even more so. Still, one thing
I believe the various xTalk languages (Hypercard, Metacard, etc) miss is
information hiding: the ability to write subroutines that are only
accessable from within the parent handler (something like the Pascal
rules for procedural scope).

BUT (and this is an important point!) the best way to avoid spaghetti
code, regardless of the language, platform or development system,
whether you are using GOTOs, procedures or handlers, is to carefully and
in detail plan your program on paper before even sitting down in front
of a computer.

I don't mean you need to write out every handler on paper before
starting, but you must know where the program flow is going to go, what
routines will be called in what order, what routines they depend on, and
so forth, in enough detail to allow you to plan your strategy, without
getting bogged down in the specifics of implementation.

As my old computer science tutor used to tell me, you must know exactly
what your program is going to do before you write a single line of code.



-- 
Steven D'Aprano

==========================================
M.B. Sales Pty Ltd    Ph:  +61 3 9460-5244
A.C.N. 005-964-796    Fax: +61 3 9462-1161

Reply via email to