

   Stirring the Programming Pot:


                    Computer Programming Without Spaghetti

                     Copyright 1995 by Peter Neuendorffer


 A good computer program must meet a specified need(s), and perform in a
 predictable and dependable fashion. As a programmer starting a new
 project, I spend several days thinking about the problem at hand.  I
 think much less about how I am going to construct the software, as
 opposed to what I want it to accomplish with it. Slowly, I try to
 envision the outlines of my finished project in my head, often scribbling
 notes on a napkin in a coffee shop.

 There are many design tools which play out for making lists or word
 games, but if I don't have an overall vision of the project, I do not
 proceed. There are times when I will jump into a programming project by
 furiously typing from start to finish, pulling an all-nighter and ending
 up with a conglomeration of incantations that purport to do a task. This
 style of writing can work as long as the author -me- doesn't drop dead,
 quit the business, or have a sudden memory loss.

 Such a tangled mass of programming  is affectionately called spaghetti
 code.  It is analogous to an electrician stringing wires every which way
 in a dangerous and tangled heap. All is well until changes are needed, or
 someone else must maintain the system. Side-effects, where one section
 impinges upon or trashes another region, crop up, sometimes not until
 weeks, months, or years after completion.

 When coding programs, programmers use a higher level language that
 shields them from the nuts and bolts of the machine. The machine is
 merely the medium, and must not be the reason for the program. Higher
 level languages are an outside layer, where the Basic Input Output layer
 that does the work is not addressed directly.

 The BIOS is machine dependent - different for different makes of
 computers. DOS and Windows are designed to provide services to the
 programmer to accomplish small tasks. The tasks, such as sorting,
 searching, counting, comparing of words are combined in a logical fashion
 to accomplish the purpose of the program.

 Computer languages are called higher level not because they are better, but because they are closer to normal English than machine code. No one in their right mind would code a large program in machine code, which is barely removed from long strings of ones and zeros.

 Two popular languages were developed for teaching purposes, BASIC, and
 PASCAL and they have stayed on as popular programming languages. C, the
 language of choice among professionals (I use Pascal) allows the
 programmer to do fancy low-level manipulation, and still have the
 structure of the statements in an English-like fashion, or syntax.

 A computer program is a series of data processing cycles. Some operation
 or processing is done on Input data, producing processed Output data.
 Because the instructions may be stored in a program, the processing can
 be performed without the person present who wrote the program. When
 designing a program I often ask "If I fall down a hole and drop dead,
 will the program still be able to do x,y,z?" By presenting the decisions,
 in effect bringing forward some of the processing to the user of my
 program,  I make my program more flexible. The way I present these
 choices, as well as the gateway to put data into the program (Input) and
 the results (Output) is of central importance.

 The presentation of the data is the interface of the program. Windows programming provides a common interface. Many things about a Windows program, such as the style of the menus, are conventions, or things that everybody has agreed upon. We have the convention in social relations of saying "Have a nice day", upon leaving someone, for example.

 It has been said that a well-designed program works the first time.
 This is wishful thinking. One of the benefits of the faster PC is that
 programs can be designed by running them and making changes. Although the
 program may have been freed of syntax -grammar- errors, a program can
 compile into machine language and still not accomplish it's intended
 purpose.

 These logical errors can be devilishly hard to find. Imagine giving the
 instruction to someone "Take the New York train to Maine." This is a
 logically consistent and valid grammar. But the action it commands is
 impossible, and therefore nonsensical.

 Programs may have a variety of differing routines - collections of
 statements to process information at a sub level. They might be
 instructions executed one after another -procedurally. For example, "Get
 up! Turn off the Alarm Clock! Smoke cigarette! Get dressed." Each of
 these verbs "get up", "turn off" could invoke lengthy procedures that
 have their own code. To us, "smoke cigarette" seems self-evident. But try
 writing a list of all the things you do to accomplish smoking a
 cigarette. This is what the programmer is up against. The computer does
 what you tell it to do, if anything at all, and has no problem accessing
 the wrong data if that is what you tell it to do.

 A second structure could be branching. This is performing different
 activities based upon the result of a condition. The computer, when
 making a comparison to see IF something is true, is actually merely
 comparing the values of two numbers to see if they are the same or
 different. An example of IF would be "If it is raining, then wear a
 raincoat, otherwise (it is not raining) wear a sweater."

 A third type of structure could be Looping. Doing something over and
 over until the situation changes. "Skip rope 2000 times." For a computer, this is doing something until a certain number equals, or exceeds a another number. If you are counting the loops, a common disastrous error is to forget to add one to the counter each time the loop iterates. This leads to the common "freeze" where the user is locked out of doing anything. The loop is endless.

 Still another type of structure for instructions is the recursion. This is
 a curious method where half-way through a procedure, the procedure
 invokes itself again. This is used for problems which may be halved and
 where each half of the problem is done again and again.

 These types of structures are used to make algorithms. Algorithms are
 a finite set of steps used to accomplish a particular task. When
 designing your program, you can describe these using Pseudocode.
 Pseudocode is your own English-like language. In effect, a recipe for
 making a cake could be written in Pseudocode to see how it works. The
 ingredients are the data, the preparation represents the Initialization,
 and the steps for making the cake are the instructions.

 You break the project into small detachable modules, such as "make
 batter", "make icing", "cook cake", and "ice the cake." You could put
 this overall structure into a drawing of boxes, that are set up in a
 hierarchy like a family true. This is a program structure chart.

 To get organized, you can set up Input/Process/Output sheets that lists
 the tasks involved in accomplishing a module. Several tasks per sheet are
 used. Then you list the data that goes into each task, the Input, and
 what is produced, and then Output. You don't worry too much about how you
 will accomplish these, just what the tasks are. Each task you break down
 further, having a separate sheet for each. This is called top-down
 design.

 When working with Windows programming, it is possible to set this up
 directly so you see the sections of your program in their design. This is
 called stubbing. The program has all the Windows ready but each doesnt
 actually do anything. They could have dummy labels saying "This is where
 we make the batter" or "This is where we ice the cake." You can then
 flesh out the code so that each task is in it's proper place in relation
 to all the others.

 It is very tempting in programming to have a set of switches that the
 whole program "sees" called globals. That way the entire program can use
 them. When they are true-false switches, they are called control
 variables, or Booleans. But the tradeoff is that you may forget what you
 have set them to in subsection II.A.i somewhere. As this group grows and
 grows, the logic of your program is handed over to these switches, and
 the English-like look of the program is subverted. I like to use these
 globals a lot, but unexpected results can crop up, called side effects.

 Planning a project is very important. An architect does not build a house
 without making plans. But programmers, myself included, eager to hack
 their way into the symbolic world, are often too eager to dispense with
 the design work. Some programs seem to write themselves. But these turn
 out to be ones that have been carefully planned, with self-documenting
 data with meaningful names, and a logical structure that logically
 follows the problem at hand.

 Halfway through writing this article, I realized that I do not spend much
 time filling out design charts and forms. However, I do a lot of this
 unconsciously when I think about my new project, or when I set up the
 names and structures for my data. Since you can name your data variables
 anything you want, it is a good idea to give them meaningful names.  Cake
 ingredients is a much better name than "x".

 Although the compiler program doesn't care about the names you use for
 your data or routines,  -and if it works, it works! However, you will
 certainly care when you go back to the source six months later. If you
 can't understand what on earth the program that you wrote yourself is
 doing, you might as well throw out the project entirely.

 Much of programming involves passing along "magic" routines and even
 whole systems from one application to another, but this must be done in
 an orderly fashion. The programmer should insert copious comments in
 English reminding him/her what the program does. The extra trouble is
 well worth it. Today's brilliant solution can easily be tomorrow's
 antiquated curiosity.


 Peter is a Windows programmer. His home page URL is
 http://www.channel1.com/users/petern. His latest program is a text
 searcher abool20.zip which can be downloaded from his homepage.


                                  ww



