-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathchapter2.tex
More file actions
1546 lines (1289 loc) · 61 KB
/
chapter2.tex
File metadata and controls
1546 lines (1289 loc) · 61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%% Thinking Forth
%% Copyright (C) 2004 Leo Brodie
%% Initial transcription by Andrew Nicholson, based on OCR output
%% provided by John Hogerhuis
%%
%Page 037 in first edition
%\chapternum{TWO}
\chapter{Analysis}\Chapmark{2}
%Page 038 in first edition
\initialb Anyone who tells you there is some definite number of phases to the
software development cycle is a fool.
Nevertheless \dots{}
\section{The Nine Phases of the Programming Cycle}%
As we've seen, \Forth{} integrates aspects of design with aspects of
implementation and maintenance. As a result, the notion of a ``typical
development cycle'' makes as much sense as a ``typical noise.''
But any approach is better than no approach, and indeed, some
approaches have worked out better than others. Here is a development
cycle that represents an ``average'' of the most successful approaches
used in software projects:%
\index{P!Programming cycle:!phases|(}
\begin{description}
\item[Analysis] \hfill\index{A!Analysis}
\begin{enumerate}
\item Discover the Requirements and Constraints
\item Build a Conceptual Model of the Solution
\item Estimate Cost/Schedule/Performance
\end{enumerate}
\item[Engineering] \hfill
\begin{enumerate}
\setcounter{enumi}{3}
\item Preliminary Design
\item Detailed Design
\item Implementation
\end{enumerate}
\item[Usage] \hfill
\begin{enumerate}
\setcounter{enumi}{6}
\item Optimization
\item Validation and Debugging
\item Maintenance
\end{enumerate}
\end{description}
In this book we'll treat the first six stages of the cycle,
focusing on analysis, design, and implementation.
In a \Forth{} project the phases occur on several levels. Looking at a
project from the widest perspective, each of these steps could take a
month or more. One step follows the next, like seasons.
%Page 039 in first edition
But \Forth{} programmers also apply these same phases toward
defining each word. The cycle then repeats on the order of minutes.%
\index{P!Programming cycle:!phases|)}
Developing an application with this rapid repetition of the
programming cycle is known as using the ``Iterative Approach.''
\section{The Iterative Approach}%
\index{I!Iterative approach|(}%
\index{P!Programming cycle:!iterative approach|(}
The iterative approach was explained eloquently by
\person{Kim Harris} \cite{harris81}. \index{H!Harris, Kim|(}
He begins by describing the scientific method:
\begin{tfquot}
\dots{} a never-ending cycle of discovery and refinement. It first
studies a natural system and gathers observations about its behavior.
Then the observations are modeled to produce a theory about the
natural system. Next, analysis tools are applied to the model, which
produces predictions about the real system's behavior. Experiments
are devised to compare actual behavior to the predicted behavior. The
natural system is again studied, and the model is revised.
\wepsfigb{fig2-1}{The iterative approach to the
software development cycle, from ``The \Forth{} Philosophy,''
by \person{Kim Harris}, \emph{Dr.\@ Dobb's Journal.}}%
\index{H!Harris, Kim|)}
The \emph{goal} of the method is to produce a model which accurately
predicts all observable behavior of the natural system.
\end{tfquot}
\person{Harris} then applies the scientific method to the software
development cycle, illustrated in \Fig{fig2-1}:
\begin{enumerate}
\item A problem is analyzed to determine what functions are required
in the solution.
\item Decisions are made about how to achieve those functions with
the available resources.
\item A program is written which attempts to implement the design.
\item The program is tested to determine if the functions were
implemented correctly.
\end{enumerate}
%Page 040 in first edition
Mr. \person{Harris} adds:
\begin{tfquot}
Software development in \Forth{} seeks first to find the simplest
solution to a given problem. This is done by implementing selected
parts of the problem separately and by ignoring as many constraints as
possible. Then one or a few constraints are imposed and the program is
modified.
\end{tfquot}
An excellent testimonial to the development/testing model of design is
evolution. From protozoa to tadpoles to people, each species along the
way has consisted of functional, living beings. The Creator does not
appear to be a top-down designer.
\begin{tip}
Start simple. Get it running. Learn what you're trying to do. Add
complexity gradually, as needed to fit the requirements and
constraints. Don't be afraid to restart from scratch.
\end{tip}%
\index{I!Iterative approach|)}%
\index{P!Programming cycle:!iterative approach|)}
\section{The Value of Planning}%
\index{P!Planning:!value of|(}%
\index{P!Programming cycle:!value of planning|(}
In the nine phases at the start of this chapter we listed five steps
\emph{before} ``implementation.'' Yet in \Chap{1} we saw that an
overindulgence in planning is both difficult and pointless.
Clearly you can't undertake a significant software
project---regardless of the language---without some degree of planning.
Exactly what degree is appropriate?%
\index{J!Johnson, Dave|(}
\begin{interview}
\noindent More than one \Forth{} programmer has expressed high regard for
\person{Dave Johnson}'s meticulous approach to planning. \person{Johnson}
is supervisor at Moore Products Co. in Springhouse, Pennsylvania. The firm
specializes in industrial instrumentation and process control
applications. Dave has been using \Forth{} since 1978.
He describes his approach:
\begin{tfquot}
Compared with many others that use \Forth{}, I suppose we take a more
formal approach. I learned this the hard way, though. My lack of
discipline in the early years has come back to haunt me.
We use two tools to come up with new products: a functional specification
and a design specification. Our department of Sales \& Applications comes
up with the functional specification, through customer contact.
Once we've agreed on what we're going to do, the functional
specification is turned over to our department. At that point we work
through a design, and come up with the design specification.
Up to this point our approach is no different from programming in any
language. But with \Forth{}, we go about designing somewhat
differently. With \Forth{} you don't have to work 95\% through your
design before you can start coding, but rather 60\% before you can get
into the iterative process.
%Page 041 in first edition
A typical project would be to add a functional enhancement to one of
our products. For example, we have an intelligent terminal with disk
drives, and we need certain protocols for communicating with another
device. The project to design the protocols, come up with displays,
provide the operator interfaces, etc. may take several months. The
functional specification takes a month; the design specification takes
a month; coding takes three months; integration and testing take
another month.
This is the typical cycle. One project took almost two years, but six
or seven months is reasonable.
When we started with \Forth{} five years ago, it wasn't like that. When I
received a functional specification, I just started coding. I used a
cross between top-down and bottom-up, generally defining a structure,
and as I needed it, some of the lower level, and then returning with
more structure.
The reason for that approach was the tremendous pressure to show
something to management. We wound up never writing down what we were
doing. Three years later we would go back and try to modify the code,
without any documentation. \Forth{} became a disadvantage because it
allowed us to go in too early. It was fun to make the lights flash and
disk drives hum. But we didn't go through the nitty-gritty design
work. As I said, our ``free spirits'' have come back to haunt us.
Now for the new programmers, we have an established requirement: a
thorough design spec that defines in detail all the high-level \Forth{}
words---the tasks that your project is going to do. No more reading a
few pages of the functional specification, answering that, reading a
few more, answering that, etc.
No living programmer likes to document. By ensuring the design ahead
of time, we're able to look back several years later and remember what
we did.
I should mention that during the design phase there is some amount of
coding done to test out certain ideas. But this code may not be part
of the finished product. The idea is to map out your design.
\end{tfquot}%
\index{J!Johnson, Dave|)}
\end{interview}
\person{Johnson} advises us to complete the design specification
before starting to code, with the exception of needed preliminary
tests. The next interview backs up this point, and adds some
additional reasons.%
\begin{interview}
\index{T!Teleska, John|(}
\noindent \person{John Teleska} has been an independent software
consultant since 1976, specializing in custom applications for
academic research environments. He enjoys providing research tools
``right at the edge of what technology is able to do.''
\person{Teleska} works in Rochester, New York:
\begin{tfquot}
I see the software development process as having two phases. The first is
making sure I understand what the problem is. The second is
implementation, including debugging, verification, etc.
My goal in Phase One is an operational specification. I start with a
problem description, and as I proceed it becomes the operational
specification. My understanding of the problem metamorphoses into a
solution. The better the understanding, the more complete the
solution. I look for closure; a sense of having no more questions that
aren't answered in print.
I've found that on each project I've been putting more time into Phase
One, much to the initial dismay of many of my clients. The limiting
factor is how
%Page 042 in first edition
much I can convince the client it's necessary to spend that time up
front. Customers generally don't know the specifications for the job
they want done. And they don't have the capital---or don't feel they
do---to spend on good specs. Part of my job is to convince them it
will end up costing more time and money not to.
Some of Phase One is spent on feasibility studies. Writing the spec
unearths uncertainties. I try to be as uncertain about uncertainties
as possible. For instance, they may want to collect 200,000 samples a
second to a certain accuracy. I first need to find out if it's even
possible with the equipment they've got. In this case I've got to test
its feasibility by writing a patch of code.
Another reason for the spec is to cover myself. In case the
application performs to the spec but doesn't fully satisfy the
customer, it's the customer's responsibility. If the customer wants
more, we'll have to renegotiate. But I see it as the designer's
responsibility to do whatever is necessary to generate an operational
specification that will do the job to the customer's satisfaction.
I think there are consultants who bow to client pressure and limit the
time they spend on specs, for fear of losing the job. But in these
situations nobody ends up happy.
\end{tfquot}
\index{T!Teleska, John|)}
\end{interview}%
We'll return to the \person{Teleska} interview momentarily.%
\index{P!Planning:!value of|)}%
\index{P!Programming cycle:!value of planning|)}
\section{The Limitations of Planning}%
\index{P!Planning:!limitations of|(}%
\index{P!Programming cycle:!limitations of planning|(}
Experience has taught us to map out where we're going before we begin
coding. But planning has certain limitations. The following interviews
give different perspectives to the value of planning.%
\begin{interview}
\index{T!Teleska, John|(}
\noindent Despite \person{Teleska}'s preference for a well-planned
project, he suggests that the choice between a top-down and bottom-up
approach may depend on the situation:
\begin{tfquot}
On two recent projects involving a lot of technical interface work, I
did the whole thing bottom-up. I milled around in a bunch of
data-sheets and technical descriptions of little crannies of the
operating system I was dealing with. I felt lost most of the time,
wondering why I ever took the job on. Then finally I reached a
critical mass of some sort and began putting small programs together
that made small things happen. I continued, bottom-up, until I matched
the target application.
My top-down sense was appalled at this procedure. But I've seen me go
through this process successfully too many times to discount it for
any pedagogical reasons. And there is always this difficult phase
which it seems no amount of linear thinking will penetrate.
Programming seems a lot more intuitive than we, in this business, tell
each other it ought to be.
I think if the application elicits this sense of being lost, I proceed
bottom-up. If the application is in familiar territory then I'll
probably use a more traditional by-the-book approach.
\end{tfquot}
\index{T!Teleska, John|)}
\end{interview}
%Page 043 in first edition
\noindent And here's another view:%
\index{S!Starling, Michael|(}
\begin{interview}
\noindent At the time I interviewed him, \person{Michael Starling} of Union
Carbide was putting the final touches on two applications involving
user-configurable laboratory automation and process control automation
systems. For the pilot plant system, \person{Starling} designed both the
hardware and software to known requirements; on the laboratory
automation system he also defined the requirements himself.
His efforts were extremely successful. On one project, the new system
typically costs only 20\% as much as the equivalent system and
requires days, instead of months, to install and configure.
I asked him what techniques of project management he employed.
\begin{tfquot}
On both of these projects much design was needed. I did not follow the
traditional analysis methods, however. I did employ these steps:
First, I clearly defined the boundaries of the problem.
Second, I determined what the smaller functional pieces, the software
subsystems, had to be.
Third, I did each piece, put them together, and the system ran.
Next, I asked the users ``Does this meet your requirements?''
Sometimes it didn't, and in ways that neither the users nor the
specification designers could have anticipated.
For instance, the designers didn't realize that the original
specification wouldn't produce pleasing, human-oriented graphics
displays. After working with the interactive graphics on the first
version, users were applying arbitrary scales and coming up with
oddball displays.
So even after the basic plot algorithm was designed, we realized we
needed auto-scaling. We went back in and analyzed how human beings
plot data and wrote a first level plot function that evaluates the x
and y data and how much will fit on the graph.
After that, we realized that not all the data taken will be of
interest to experimenters. So we added a zoom capability.
This iterative approach resulted in cleaner code and better thought
out code. We established a baseline set of goals and built a minimal
system to the users' known requirements. Then we'd crank in the
programmer's experience to improve it and determine what the users
forgot they needed when they generated the specs.
The users did not invent most of the new ideas. The programmers did,
and they would bounce these ideas off the users. The problem
definition was a two-way street. In some cases they got things they
didn't know they could do on such a small computer, such as applying
digital filters and signal processing to the data.
One of the things about \Forth{} that makes this approach possible is
that primitives are easily testable. It takes some experience with
\Forth{} to learn how to take advantage of this. Guys from traditional
environments want to write ten pages of code at their desk, then sit
down to type it in and expect it to work.
To summarize my approach: I try to find out from the users what they
need, but at the same time recognizing its incompleteness. Then I keep
%Page 044 in first edition
them involved in the design during the implementation, since they have
the expertise in the application. When they see the result, they feel
good because they know their ideas were involved.
The iterative approach places highest value on producing a good
solution to the real problem. It may not always give you the most
predictable software costs. The route to a solution may depend upon
your priorities. Remember:
\begin{list}{}{}
\item Good
\item Fast
\item Cheap
\end{list}
Pick any two!
\end{tfquot}
\end{interview}
\noindent As \person{Starling} observes, you don't know completely what
you're doing till you've done it once. In my own experience, the best way
to write an application is to write it twice. Throw away the first
version and chalk it up to experience.%
\index{S!Starling, Michael|)}%
\index{K!Kogge, Peter|(}
\begin{interview}
\noindent \person{Peter Kogge} is Senior Technical Staff in the IBM
Federal Systems Division, Oswego, New York:
\begin{tfquot}
One of the key advantages I find in \Forth{} is that it allows me to
very quickly prototype an application without all the bells and
whistles, and often with significant limitations, but enough to wring
out the ``human interface'' by hands-on trial runs.
When I build such a prototype, I do so with the firm constraint that I
will use not a single line of code from the prototype in the final
program. This enforced ``do-over'' almost always results in far
simpler and more elegant final programs, even when those programs are
written in something other than \Forth{}.
\end{tfquot}
\end{interview}%
\index{K!Kogge, Peter|)}
\noindent Our conclusions? In the \Forth{} environment planning is
necessary. But it should be kept short. Testing and prototyping are
the best ways to discover what is really needed.
A word of caution to project managers: If you're supervising any
experienced \Forth{} programmers, you won't have to worry about them
spending too much time on planning. Thus the following tip has two
versions:
\begin{tip}
For newcomers to \Forth{} (with ``traditional'' backgrounds):
Keep the analysis phase to a minimum.
\medskip
For \Forth{} addicts (without a ``traditional'' background):
Hold off on coding as long as you can possibly stand it.
\end{tip}
%Page 045 in first edition
Or, as we observed in \Chap{1}:
\begin{tip}
Plan for change (by designing components that can be changed).
\end{tip}
Or, simply:
\begin{tip}
Prototype.
\end{tip}%
\index{P!Planning:!limitations of|)}%
\index{P!Programming cycle:!limitations of planning|)}
\section{The Analysis Phase}%
\index{A!Analysis|(}%
In the remainder of this chapter we'll discuss the analysis phase.
Analysis is an organized way of understanding and documenting what
the program should do.%
\index{A!Analysis!defined}
With a simple program that you write for yourself in less than an
hour, the analysis phase may take about 250 microseconds. At the other
extreme, some projects will take many man-years to build. On such a
project, the analysis phase is critical to the success of the entire
project.
We've indicated three parts to the analysis phase:
\begin{enumerate}\parsep=0pt\itemsep=0pt
\item Discovering the requirements and constraints
\item Building a conceptual model of the solution
\item Estimating cost, scheduling, and performance
\end{enumerate}
\noindent Let's briefly describe each part:
\subsection{Discovering the Requirements}%
\index{A!Analysis!requirements|(}%
\index{R!Requirements|(}
The first step is to determine what the application should do. The
customer, or whoever wants the system, should supply a ``requirements
specification.'' This is a modest document that lists the minimum
capabilities for the finished product.
The analyst may also probe further by conducting interviews and
sending out questionnaires to the users.%
\index{A!Analysis!requirements|)}%
\index{R!Requirements|)}
\subsection{Discovering the Constraints}%
\index{A!Analysis!constraints|(}%
\index{C!Constraints|(}
The next step is to discover any limiting factors. How important is
speed? How much memory is available? How soon do you need it?
No matter how sophisticated our technology becomes, programmers will
always be bucking limitations. System capacities inexplicably
%Page 046 in first edition
diminish over time. The double-density disk drives that once were the
answer to my storage prayers no longer fill the bill. The
double-sided, double-density drives I'll get next will seem like a
vast frontier---for a while. I've heard guys with 10-megabyte hard
disks complain of feeling cramped.
Whenever there's a shortage of something---and there always will
be---tradeoffs have to be made. It's best to use the analysis phase to
anticipate most limitations and decide which tradeoffs to make.
On the other hand, you should \emph{not} consider other types of
constraints during analysis, but should instead impose them gradually
during implementation, the way one stirs flour into gravy.
The type of constraint to consider during analysis includes those that
might affect the overall approach. The type to defer includes those
that can be handled by making iterative refinements to the planned
software design.
As we heard in our earlier interviews, finding out about \emph{hardware}
constraints\index{H!Hardware constraints} often requires writing
some test code and trying things out.
Finding out about the \emph{customer's} constraints%
\index{C!Customer constraints}
is usually a matter of asking the customer, or of taking written
surveys. ``How fast do you need such-and-such, on a scale of one to
ten?'', etc.%
\index{A!Analysis!constraints|)}%
\index{C!Constraints|)}
\subsection{Building a Conceptual Model of the Solution}%
\index{A!Analysis!conceptual model|(}%
\index{C!Conceptual model|(}
A conceptual model is an imaginary solution to the problem. It is a
view of how the system \emph{appears} to work. It is an answer to all
the requirements and constraints.%
\index{C!Conceptual model!defined}
\wepsfigp{img2-047}{Refining the conceptual model to meet
requirements and constraints.}
If the requirements definition is for ``something to stand on to paint
the ceiling,'' then a description of the conceptual model is ``a
device that is free-standing (so you can paint the center of the
room), with several steps spaced at convenient intervals (so you can
climb up and down), and having a small shelf near the top (to hold
your paint can).''
A conceptual model is not quite a design, however. A design begins to
describe how the system \emph{really} works. In design, the image of a
step ladder would begin to emerge.
\Forth{} blurs the distinction a little, because all definitions are
written in conceptual terms, using the lexicons of lower level
components. In fact, later in this chapter we'll use \Forth{}
``pseudocode'' to describe conceptual model solutions.
Nevertheless, it's useful to make the distinction. A conceptual model
is more flexible than a design. It's easier to fit the requirements
and constraints into the model than into a design.
\begin{tip}
Strive to build a solid conceptual model before beginning the design.
\end{tip}
%Page 047 in first edition
%img2-47 moved forward
%Page 048 in first edition
\noindent Analysis consists of expanding the requirements definition
into a conceptual model. The technique involves two-way communication
with the customer in successive attempts to describe the model.
Like the entire development cycle, the analysis phase is best approached
iteratively. Each new requirement will tend to suggest something in
your mental model. Your job is to juggle all the requirements and
constraints until you can weave a pattern that fits the bill.
\wepsfigb{fig2-2}{An iterative approach to analysis.}
\Fig{fig2-2} illustrates the iterative approach to the analysis phase.
The final step is one of the most important: show the documented model
to the customer. Use whatever means of communication are
necessary---diagrams, tables, or cartoons---to convey your
understanding to the customer and get the needed feedback. Even if you
cycle through this loop a hundred times, it's worth the effort.
In the next three sections we'll explore three techniques for defining
and documenting the conceptual model:
\begin{enumerate}
\item defining the interfaces
\item defining the rules
\item defining the data structures.
\end{enumerate}%
\index{A!Analysis|)}
\section{Defining the Interfaces}%
\index{A!Analysis!interface definition|(}%
\index{C!Conceptual model!interface definition|(}%
\index{I!Interface definition|(}
\begin{tip}
First, and most importantly, the conceptual model should describe the
system's interfaces.\index{I!Interface definition}
\end{tip}%
\begin{interview}
\index{T!Teleska, John|(}
\person{Teleska}:
\begin{tfquot}
The ``spec'' basically deals with WHAT. In its most glorious form, it
describes what the system would look like to the user---you might call it
the user's manual. I find I write more notes on the human
interaction---what it
%Page 049 in first edition
will look like on the outside---than on the part that gets the job
done. For instance, I'll include a whole error-action listing to show
what happens when a particular error occurs. Oddly, this is the part
that takes the most time to implement anyway.
I'm currently working on a solid-state industrial washing-machine
timer. In this case, the user interface is not that complex. What is
complex is the interface to the washing machine, for which I must
depend on the customer and the documentation they can provide.
The significant interface is whatever is the arms and legs of the
product. I don't make the distinction between hardware and software at
this early stage. They can be interchanged in the implementation.
The process of designing hardware and the process of designing
software are analogous. The way I design hardware is to treat it as a
black box. The front panel is input and output. You can do the same
with software.
I use any techniques, diagrams, etc., to show the customer what the
inputs and outputs look like, using his description of what the
product has to do. But in parallel, in my own mind, I'm imagining how
it will be implemented. I'm evaluating whether I can do this
efficiently. So to me it's not a black box, it's a gray box. The
designer must be able to see inside the black boxes.
When I design a system that's got different modules, I try to make the
coupling as rational and as little as possible. But there's always
give and take, since you're compromising the ideal.
\index{D!Data-flow diagrams|(}
For the document itself, I use DFDs {[}data-flow diagrams, which we'll
discuss later{]}, and any other kind of representation that I can show
to my client. I show them as many diagrams as I can to clarify my
understanding. I don't generally use these once it comes to
implementation. The prose must be complete, even without reference to
the diagrams.
\end{tfquot}%
\index{T!Teleska, John|)}
\end{interview}%
\index{E!Error handling|(}
\begin{tip}
Decide on error- and exception-handling early as part of defining the
interface.
\end{tip}
\noindent It's true that when coding for oneself, a programmer can
often concentrate first on making the code run correctly under
\emph{normal} conditions, then worry about error-handling later. When
working for someone else, however, error-handling should be worked out
ahead of time. This is an area often overlooked by the beginning
programmer.
The reason it's so important to decide on error-handling at this stage
is the wide divergence in how errors can be treated. An error might be:
\begin{itemize}
\item ignored
\item made to set a flag indicating that an error occurred, while
processing continues
\item made to halt the application immediately
\item designed to initiate procedures to correct the problem and keep
the program running.
\end{itemize}
%
%Page 050 in first edition
%
There's room for a serious communications gap if the degree of
complexity required in the error-handling is not nailed down early.
Obviously, the choice bears tremendous impact on the design and
implementation of the application.%
\index{E!Error handling|)}
\begin{tip}
Develop the conceptual model by imagining the data traveling through and
being acted upon by the parts of the model.
\end{tip}%
\index{S!Structured analysis|(}
A discipline called \emph{structured analysis} \cite{weinberg80}
offers some techniques for describing interfaces in ways that your
clients will easily understand. One of these techniques is called the
``data-flow diagram'' (DFD), which \person{Teleska} mentioned.
\wepsfiga{fig2-3}{A data-flow diagram.}
A data-flow diagram, such as the one depicted in \Fig{fig2-3},
emphasizes what happens to items of data as they travel through the
system. The circles represent ``transforms,'' functions that act upon
information. The arrows represent the inputs and outputs of the
transforms.
The diagram depicts a frozen moment of the system in action. It
ignores initialization, looping structures, and other details of
programming that relate to time.
Three benefits are claimed for using DFDs:
First, they speak in simple, direct terms to the customer. If your
%Page 051 in first edition
customer agrees with the contents of your data-flow diagram, you know
you understand the problem.
Second, they let you think in terms of the logical ``whats,'' without
getting caught up in the procedural ``hows,'' which is consistent with
the philosophy of hiding information as we discussed in the last chapter.
Third, they focus your attention on the interfaces to the system and
between modules.
\Forth{} programmers, however, rarely use DFDs except for the customer's
benefit. \Forth{} encourages you to think in terms of the conceptual
model, and \Forth{}'s implicit use of a data stack makes the passing of
data among modules so simple it can usually be taken for granted.
This is because \Forth{}, used properly, approaches a functional language.%
\index{D!Data-flow diagrams|)}%
\index{S!Structured analysis|)}%
\index{P!Pseudocode|(}
For anyone with a few days' familiarity with \Forth{}, simple definitions
convey at least as much meaning as the diagrams:
\begin{Code}
: request ( quantity part# -- )
on-hand? IF transfer ELSE reorder THEN ;
: reorder authorization? IF p.o. THEN ;
: p.o. bookkeeping copy receiving copy
vendor mail-copy ;
\end{Code}
This is \Forth{} pseudocode. No effort has been made to determine what
values are actually passed on the stack, because that is an
implementation detail. The stack comment for \forth{REQUEST} is used
only to indicate the two items of data needed to initiate the process.
(If I were designing this application, I'd suggest that the user
interface be a word called \forth{NEED}, which has this syntax:
\begin{Code}
need 50 axles
\end{Code}
\noindent \forth{NEED} converts the quantity into a numeric value on
the stack, translates the string \forth{AXLES} into a part number,
also on the stack, then calls \forth{REQUEST}. Such a command should
be defined only at the outer-most level.)%
\begin{interview}
\index{J!Johnson, Dave|(}
\person{Johnson} of Moore Products Co. has a few words on \Forth{}
pseudocode:
\begin{tfquot}
IBM uses a rigorously documented PDL (program design language). We use
a PDL here as well, although we call it FDL, for \Forth{} design
language. It's probably worthwhile having all those standards, but
once you're familiar with \Forth{}, \Forth{} itself can be a design
language. You just have to leave out the so-called ``noise'' words:
\forth{C@}, \forth{DUP}, \forth{OVER}, etc., and show only the basic
flow. Most \Forth{} people probably do that informally. We do it
purposefully.
\end{tfquot}%
\index{J!Johnson, Dave|)}%
\end{interview}%
\index{P!Pseudocode|)}
%Page 052 in first edition
\begin{interview*}
During one of our interviews I asked \person{Moore}\index{M!Moore,
Charles|(} if he used diagrams of any sort to plan out the conceptual
model, or did he code straight into \Forth{}? His reply:
\begin{tfquot}
The conceptual model \emph{is} \Forth{}. Over the years I've learned
to think that way.
\end{tfquot}
Can everyone learn to think that way?
\begin{tfquot}
I've got an unfair advantage. I codified my programming style and other
people have adopted it. I was surprised that this happened. And I feel at a
lovely advantage because it is my style that others are learning to emulate.
Can they learn to think like I think? I imagine so. It's just a matter of
practice, and I've had more practice.
\end{tfquot}\index{M!Moore, Charles|)}
\end{interview*}%
\index{A!Analysis!interface definition|)}%
\index{C!Conceptual model!interface definition|)}%
\index{I!Interface definition|)}
\section{Defining the Rules}%
\index{A!Analysis!rule definition|(}%
\index{C!Conceptual model!rule definition|(}%
\index{R!Rule definition|(}%
\program{phone1}
Most of your efforts at defining a problem will center on describing
the interface.%
\index{I!Interface definition}
Some applications will also require that you define the set of
application rules.
All programming involves rules. Usually these rules are so simple it
hardly matters how you express them: ``If someone pushes the button,
ring the bell.''
Some applications, however, involve rules so complicated that they
can't be expressed in a few sentences of English. A few formal techniques
can come in handy to help you understand and document these more
complicated rules.
Here's an example. Our requirements call for a system to compute the
charges on long-distance phone calls. Here's the customer's
explanation of its rate structure. (I made this up; I have no idea how
the phone company actually computes their rates except that they
overcharge.)
\begin{tfquot}
All charges are computed by the minute, according to distance in
hundreds of miles, plus a flat charge. The flat charge for direct dial
calls during weekdays between 8 A.M. and 5 P.M. is .30 for the first
minute, and .20 for each additional minute; in addition, each minute
is charged .12 per 100 miles. The flat charge for direct calls during
weekdays between 5 P.M. and 11 P.M. is .22 for the first minute, and
.15 for each additional minute; the distance rate per minute is .10
per 100 miles. The flat charge for direct calls late during weekdays
between 11 P.M. or anytime on Saturday, Sundays, or holidays is .12
for the first minute, and .09 for each additional minute; the distance
rate per minute is .06 per 100 miles. If the call requires assistance
from the operator, the flat charge increases by .90, regardless of the hour.
\end{tfquot}
This description is written in plain old English, and it's quite a
mouthful. It's hard to follow and, like an attic cluttered with
accumulated belongings, it may even hide a few bugs.
%Page 053 in first edition
In building a conceptual model for this system, we must describe the
rate structure in an unambiguous, useful way. The first step towards
cleaning up the clutter involves factoring out irrelevant pieces of
information---that is, applying the rules of limited redundancy. We
can improve this statement a lot by splitting it into two statements.
First there's the time-of-day rule:%
\index{A!Analysis!conceptual model|)}
\begin{tfquot}
Calls during weekdays between 8 A.M. and 5 P.M. are charged at ``full'' rate.
Calls during weekdays between 5 P.M. and 11 P.M. are charged at ``lower''
rate. Calls placed during weekdays between 11 P.M. or anytime on Saturday,
Sundays, or holidays are charged at the ``lowest'' rate.
\end{tfquot}
Then there's the rate structure itself, which should be described in
terms of ``first-minute rate,'' ``additional minute rate,'' ``distance
rate,'' and ``operator-assistance rate.''
\begin{tip}
Factor the fruit. (Don't confuse apples with oranges.)
\end{tip}
These prose statements are still difficult to read, however. System
analysts use several techniques to simplify these statements:
structured English, decision trees, and decision tables. Let's study
each of these techniques and evaluate their usefulness in the \Forth{}
environment.
\subsection{Structured English}%
\index{A!Analysis!Structured English|(}%
\index{S!Structured English|(}
Structured English is a sort of structured pseudocode in which our rate
statement would read something like this:
%Page 053/054 in first edition
\begin{Code}[baselinestretch=0.95]
IF full rate
IF direct-dial
IF first-minute
.30 + .12/100miles
ELSE ( add'l- minute)
.20 + .12/100miles
ENDIF
ELSE ( operator )
IF first-minute
1.20 + .12/100miles
ELSE ( add'l- minute)
.20 + .12/100miles
ENDIF
ENDIF
ELSE ( not-full-rate)
IF lower-rate
IF direct-dial
IF first-minute
.22 + .10/100miles
ELSE ( add'l- minute)
.15 + .10/100miles
ENDIF
ELSE ( operator)
IF first-minute
1.12 + .10/100miles
ELSE ( add'l- minute)
.15 + .10/100miles
ENDIF
ENDIF
ELSE ( lowest-rate)
IF direct-dial
IF first-minute
.12 + .06/100miles
ELSE ( add'l- minute)
.09 + .O6/100miles
ENDIF
ELSE ( operator)
IF first-minute
1.02 + .O6/100miles
ELSE ( add'l- minute)
.09 + .06/100miles
ENDIF
ENDIF
ENDIF
ENDIF
\end{Code}
This is just plain awkward. It's hard to read, harder to maintain, and
hardest to write. And for all that, it's worthless at implementation
time. I don't even want to talk about it anymore.%
\index{A!Analysis!Structured English|)}%
\index{S!Structured English|)}
\subsection{The Decision Tree}%
\index{A!Analysis!decision tree|(}%
\index{D!Decision tree|(}
\wepsfigt{fig2-4}{Example of a decision tree.}
\Fig{fig2-4} illustrates the telephone rate rules by means of a
decision tree. The decision tree is the easiest method of any to
``follow down'' to determine the result of certain conditions. For
this reason, it may be the best representation to show the customer.
Unfortunately, the decision tree is difficult to ``follow up,'' to
determine which conditions produce certain results. This difficulty
inhibits seeing ways to simplify the problem. The tree obscures the
fact that additional minutes cost the same, whether the operator
assists or not. You can't see the facts for the tree.%
\index{A!Analysis!decision tree|)}%
\index{D!Decision tree|)}
%Page 055 in first edition
% Figure 2-4 was here
\subsection{The Decision Table}%
\index{A!Analysis!decision table|(}%
\index{D!Decision table|(}
The decision table, described next, provides the most usable graphic
representation of compound rules for the programmer, and possibly for
the customer as well. \Fig{fig2-5} shows our rate structure rules in
decision-table form.
\wepsfigt{fig2-5}{The decision table.}
In \Fig{fig2-5} there are three dimensions: the rate discount, whether
an operator intervenes, and initial minute vs. additional minute.
Drawing problems with more than two dimensions gets a little tricky.
As you can see, these additional dimensions can be depicted on
%Page 056 in first edition
paper as subdimensions within an outer dimension. All of the
subdimension's conditions appear within every condition of the outer
dimension. In software, any number of dimensions can be easily
handled, as we'll see.
All the techniques we've described force you to analyze which
conditions apply to which dimensions. In factoring these dimensions,
two rules apply:
First, all the elements of each dimension must be mutually exclusive.
You don't put ``first minute'' in the same dimension as ``direct
dial,'' because they are not mutually exclusive.
Second, all possibilities must be accounted for within each dimension.
If there were another rate for calls made between 2 A.M. to 2:05 A.M.,
the table would have to be enlarged.
But our decision tables have other advantages all to themselves. The
decision table not only reads well to the client but actually benefits
the implementor in several ways:
\begin{description}
\item[Transferability to actual code.] This is particularly true in
\Forth{}, where decision tables are easy to implement in a form very
similar to the drawing.
\item[Ability to trace the logic upwards.] Find a condition and see what
factors produced it.
\item[Clearer graphic representation.] Decision tables serve as a better