| 1 |
|
| 2 |
<HTML> |
| 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 |
<HEAD> |
| 29 |
<TITLE>5.11 The Dangers Of FunnelWeb</TITLE> |
| 30 |
<STYLE TYPE="text/css"> <!-- A {text-decoration: none} // --> </STYLE> |
| 31 |
</HEAD> |
| 32 |
<BODY BACKGROUND="binary/background.gif" |
| 33 |
BGCOLOR="#FFFFFF" |
| 34 |
TEXT="#000000" |
| 35 |
VLINK="#660000" |
| 36 |
LINK="#FF0000" |
| 37 |
ALINK="#CC0000"> |
| 38 |
|
| 39 |
<TABLE WIDTH="490"> |
| 40 |
<TR> |
| 41 |
<TD WIDTH="130" VALIGN="top"> |
| 42 |
<IMG SRC="binary/d_clear.gif" ALT="" WIDTH="130" HEIGHT="1"><BR> |
| 43 |
|
| 44 |
<FONT SIZE="2"> |
| 45 |
<BR> |
| 46 |
|
| 47 |
<A HREF="http://www.ross.net/" |
| 48 |
TARGET="rosshome" |
| 49 |
onClick="window.open('','rosshome','location,status,menubar,scrollbars,resizable',false).focus(); return true;" |
| 50 |
> |
| 51 |
<IMG SRC="binary/rossnet_logo.gif" |
| 52 |
WIDTH="64" HEIGHT="32" |
| 53 |
BORDER="0" ALT="RossNet" |
| 54 |
HSPACE="0" VSPACE="1"></A><BR> |
| 55 |
<BR> |
| 56 |
|
| 57 |
<A HREF="../index.shtml" |
| 58 |
TARGET="funnelweb" |
| 59 |
onClick="window.open('','funnelweb','location,status,menubar,scrollbars,resizable',false).focus(); return true;" |
| 60 |
> |
| 61 |
<IMG SRC="binary/linklogo.gif" |
| 62 |
WIDTH="64" HEIGHT="32" |
| 63 |
BORDER="0" ALT="FunnelWeb" |
| 64 |
HSPACE="0" VSPACE="1"></A><BR> |
| 65 |
<BR> |
| 66 |
<TABLE CELLSPACING=0 CELLPADDING=0 BORDER=0><TR><TD BGCOLOR="#000000"> |
| 67 |
<A HREF="../reference/index.html" |
| 68 |
TARGET="funnelwebreference" |
| 69 |
onClick="window.open('','funnelwebreference','location,status,menubar,scrollbars,resizable',false).focus(); return true;" |
| 70 |
><FONT COLOR="#FFFFFF"><B>Reference</B></FONT></A><BR> |
| 71 |
<BR> |
| 72 |
<A HREF="../developer/index.html" |
| 73 |
TARGET="funnelwebdeveloper" |
| 74 |
onClick="window.open('','funnelwebdeveloper','location,status,menubar,scrollbars,resizable',false).focus(); return true;" |
| 75 |
><FONT COLOR="#FFFFFF"><B>Developer</B></FONT></A><BR> |
| 76 |
<BR> |
| 77 |
<A HREF="index.html"><FONT COLOR="#FFFFFF"><B>Tutorial</B></FONT></A><BR> |
| 78 |
<A HREF="intro.html"><FONT COLOR="#FFFFFF">1 Introduction</FONT></A><BR> |
| 79 |
<A HREF="macro.html"><FONT COLOR="#FFFFFF">2 Macros</FONT></A><BR> |
| 80 |
<A HREF="type.html"><FONT COLOR="#FFFFFF">3 Typesetting</FONT></A><BR> |
| 81 |
<A HREF="example.html"><FONT COLOR="#FFFFFF">4 Example</FONT></A><BR> |
| 82 |
<A HREF="hints.html"><FONT COLOR="#FFFFFF">5 Hints</FONT></A><BR> |
| 83 |
<A HREF="examples.html"><FONT COLOR="#FFFFFF">6 Examples</FONT></A><BR> |
| 84 |
<A HREF="web.html"><FONT COLOR="#FFFFFF">7 Webmaking</FONT></A><BR> |
| 85 |
|
| 86 |
<BR> |
| 87 |
<A HREF="search.html"><FONT COLOR="#FFFFFF"><B>SEARCH</B></FONT></A><BR> |
| 88 |
</FONT> |
| 89 |
</TD></TR></TABLE> |
| 90 |
|
| 91 |
|
| 92 |
</TD> |
| 93 |
<TD WIDTH="360" VALIGN="top"> |
| 94 |
<FONT SIZE="3"> |
| 95 |
|
| 96 |
|
| 97 |
<A HREF="../reference/index.html"><IMG SRC="binary/title.gif" |
| 98 |
WIDTH="302" HEIGHT="24" |
| 99 |
BORDER="0" ALT="FunnelWeb Tutorial Manual" |
| 100 |
HSPACE="0" VSPACE="0"></A> |
| 101 |
<P><FONT SIZE="5">5.11 The Dangers Of FunnelWeb</FONT><BR> |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
<P>Like many tools that are general and flexible, |
| 106 |
FunnelWeb can be used in a variety of ways, both good and |
| 107 |
bad. One of the original appeals of the literate approach to |
| 108 |
programming for Knuth, the inventor |
| 109 |
of literate programming, was |
| 110 |
that it allows the programmer to describe the target program |
| 111 |
bottom up, top down, size to side, or chaotically if |
| 112 |
desired. |
| 113 |
|
| 114 |
<P>The flexibility that the literate style of |
| 115 |
programming leaves much room for bad documentation |
| 116 |
as well as good documentation. Years of experience |
| 117 |
with FunnelWeb has revealed the following |
| 118 |
stylistic pitfalls which the experienced FunnelWeb |
| 119 |
user should take care to avoid. (Note: The fact that |
| 120 |
these faults are listed here does not mean that |
| 121 |
the author has eliminated them in his own work. |
| 122 |
Rather, it is mainly the author's own mistakes |
| 123 |
that have resulted in this list being compiled. |
| 124 |
The author immediately confesses to several of the |
| 125 |
faults listed here, most notably that of Pavlov |
| 126 |
documentation). |
| 127 |
|
| 128 |
<P><BLOCKQUOTE><B>Spaghetti |
| 129 |
organization:</B> By far the |
| 130 |
worst problem that arises in connection with the literate |
| 131 |
style occurs where the programmer has used the literate tool |
| 132 |
to completely scramble the program so that the program is |
| 133 |
described and layed out in an unordered, undisciplined |
| 134 |
"stream of consciousness". In such cases the programmer may be using |
| 135 |
the literate style as a crutch to avoid having to think |
| 136 |
about structuring the presentation.</BLOCKQUOTE> |
| 137 |
|
| 138 |
<P><BLOCKQUOTE><B>Boring |
| 139 |
organization:</B> At the other |
| 140 |
extreme, a program may be organized in such a strict way |
| 141 |
that it is essentially laid out in the order most |
| 142 |
"desired" by the target programming language. For |
| 143 |
example, each macro might contain a single procedure, with |
| 144 |
all the macros being called by a macro connected to a file |
| 145 |
at the top. In many cases a boring structure may be entirely |
| 146 |
appropriate, but the programmer should be warned that it is |
| 147 |
easy to slip into such a normative style, largely forgetting |
| 148 |
the descriptive structural power that FunnelWeb provides.</BLOCKQUOTE> |
| 149 |
|
| 150 |
<P><BLOCKQUOTE><B>Poor random |
| 151 |
access:</B> Using FunnelWeb, it is |
| 152 |
quite possible to write programs like novels |
| 153 |
--- to be read from cover to cover. Sometimes the story is |
| 154 |
very exciting, with data structures making dashing triumphs |
| 155 |
and optimized code bringing the story to a satisfying |
| 156 |
conclusion. These programs can be works of art. |
| 157 |
Unfortunately, without careful construction, such |
| 158 |
"novel-programs" can become very hard to access |
| 159 |
randomly by (say) a maintenance |
| 160 |
programmer who wishes only |
| 161 |
to dive in and fix a specific problem. If the entire program |
| 162 |
is scrambled for sequential exposition, it can be hard to |
| 163 |
find the parts relating to a single function. Somehow a |
| 164 |
balance must be struck in the document between the needs of |
| 165 |
the sequential and of the random-access reader. This balance |
| 166 |
will depend on the intended use of the program.</BLOCKQUOTE> |
| 167 |
|
| 168 |
<P><BLOCKQUOTE><B>Too-interdependent |
| 169 |
documentation:</B> Sometimes, |
| 170 |
when editing a |
| 171 |
program written using FunnelWeb, one knows how to modify the |
| 172 |
program, but one is unsure of how to update the surrounding |
| 173 |
documentation! The documentation may be woven into such a |
| 174 |
network of facts that it seems that changing a small piece |
| 175 |
of code could invalidate many pieces of documentation |
| 176 |
scattered throughout the document. The documentation becomes |
| 177 |
a big tar pit in which movement is impossible. For example, |
| 178 |
if you have talked about a particular data structure |
| 179 |
invariant throughout a document, changing that invariant in |
| 180 |
a small way could mean having to update all the |
| 181 |
documentation without touching much code. In such cases, the |
| 182 |
documentation is too interdependent. This could be |
| 183 |
symptomatic of an excessibly interconnected program, or of |
| 184 |
an excessively verbose or redundant documenting style. In |
| 185 |
any case, a balance must be struck between the |
| 186 |
conversational style that encourages redundancy (by |
| 187 |
mentioning things many times) and the normalized database |
| 188 |
approach where each fact is given at only one point, and the |
| 189 |
reader is left to figure out the implications throughout the |
| 190 |
document.</BLOCKQUOTE> |
| 191 |
|
| 192 |
<P><BLOCKQUOTE><B>Pavlov |
| 193 |
documentation:</B> By placing so |
| 194 |
much emphasis on the documentation, FunnelWeb naturally |
| 195 |
provides slots where documentation "should" go. For |
| 196 |
example, a FunnelWeb user may feel that there may be a |
| 197 |
rather unpleasant gap between a <SAMP>@C</SAMP> marker and the |
| 198 |
following macro. In many cases <I>no</I> commentary is |
| 199 |
needed, and the zone is better left blank rather than being |
| 200 |
filled with the kind of uninformative waffle one often finds |
| 201 |
filling the slots of structured documentation written |
| 202 |
according to a military standards |
| 203 |
(e.g. MIL-STD-2167A). (Note: |
| 204 |
This is not a criticism of 2167A, only of the way it is |
| 205 |
sometimes used). The lesson is to add documentation only |
| 206 |
when it adds something. The lesson in Strunk and |
| 207 |
White<STRONG>[Strunk79]</STRONG> (p. 23) holds for program |
| 208 |
documentation as it does for other writing: "Vigorous |
| 209 |
writing is concise. A sentence should contain no unnecessary |
| 210 |
words, a paragraph no unnecessary sentences, for the same |
| 211 |
reason that a drawing should have no unnecessary lines and a |
| 212 |
machine no unnecessary parts. This requires not that the |
| 213 |
writer make all his sentences short, or that he avoid all |
| 214 |
detail and treat his subjects only in outline, but that |
| 215 |
every word tell.".</BLOCKQUOTE> |
| 216 |
|
| 217 |
<P><BLOCKQUOTE><B>Duplicate |
| 218 |
documentation:</B> Where the |
| 219 |
programmer is generating product files that must exist on |
| 220 |
their own within the entire programming environment |
| 221 |
(e.g. the case of a programmer in a team who is using |
| 222 |
FunnelWeb for his own benefit but must generate (say) |
| 223 |
commented Ada specification package files) there |
| 224 |
is a tendency for the comments in the target code to |
| 225 |
duplicate the commentary in the FunnelWeb text. This may or |
| 226 |
may not be a problem, depending on the situation. |
| 227 |
However, if this is happening, it is certainly worth the |
| 228 |
programmer spending some time deciding if one or other of |
| 229 |
the FunnelWeb or inline-comment documentation should be |
| 230 |
discarded. In many cases, a mixture can be used, with the |
| 231 |
FunnelWeb documentation referring the reader to the inline |
| 232 |
comments where they are present. For example:</BLOCKQUOTE> |
| 233 |
|
| 234 |
<P> |
| 235 |
<PRE> |
| 236 |
@A Here is the header comment for the list package |
| 237 |
specification. The reader should read these comments |
| 238 |
carefully as they define a list. There is no need to |
| 239 |
duplicate the comments in this text. |
| 240 |
|
| 241 |
@$@<Specification package comments@>==@{@- |
| 242 |
-- LIST PACKAGE |
| 243 |
-- ============ |
| 244 |
-- * A LIST consists of zero or more ITEMS. |
| 245 |
-- * The items are numbered 1 to N where N |
| 246 |
-- is the number of items in the list. |
| 247 |
-- * If the list is non-empty, item 1 |
| 248 |
-- is called the HEAD of the list. |
| 249 |
-- * If the list is non-empty, item N |
| 250 |
-- is called the TAIL of the list. |
| 251 |
-- ... |
| 252 |
@} |
| 253 |
</PRE> |
| 254 |
|
| 255 |
<P><BLOCKQUOTE><B>Overdocumenting:</B> Another |
| 256 |
evil that can arise when using FunnelWeb is to over-document |
| 257 |
the target program. In some of Knuth's earlier (e.g. 1984) |
| 258 |
examples of literate programming, each variable is given its |
| 259 |
own description and each piece of code has a detailed |
| 260 |
explanation. This level of analysis, while justified for |
| 261 |
tricky tracts of code, is probably not warranted for most of |
| 262 |
the code that constitutes most programs. Such |
| 263 |
over-commenting can even have the detrimental affect of |
| 264 |
obscuring the code, making it hard to understand because it |
| 265 |
is so scattered (see "spaghetti organization" |
| 266 |
earlier). It is up to the user to decide when a stretch of |
| 267 |
just a few lines of code should be pulled to bits and |
| 268 |
analysed and when it is clearer to leave it alone.</BLOCKQUOTE> |
| 269 |
|
| 270 |
<P><BLOCKQUOTE><B></B> In the case where there are a few rather tricky |
| 271 |
lines of code, a detailed explanation may be appropriate. |
| 272 |
The following example contains a solution to a problem |
| 273 |
outlined in section 16.3 of the book "The Science of |
| 274 |
Programming" by David Gries<STRONG>[Gries81]</STRONG>.</BLOCKQUOTE> |
| 275 |
|
| 276 |
<P> |
| 277 |
<PRE> |
| 278 |
@C@<Calculation of the longest plateau in b@> |
| 279 |
|
| 280 |
This section contains a solution to a problem |
| 281 |
outlined in section 16.3 of the book @/The |
| 282 |
Science of Programming@/ by David Gries[Gries81]. |
| 283 |
|
| 284 |
@D Given a sorted array @{b[1..N]@} of |
| 285 |
integers, we wish to determine the @/length@/ of |
| 286 |
the longest run of identically valued elements in |
| 287 |
the array. This problem is defined by the |
| 288 |
following precondition and postcondition. |
| 289 |
|
| 290 |
@$@<Precondition@>==@{/* Pre: sorted(b). */@} |
| 291 |
@$@<Postcondition@>==@{@- |
| 292 |
/* Post: sorted(b) and p is the length */ |
| 293 |
/* of the longest run in b[1..N]. */@} |
| 294 |
|
| 295 |
@D We approach a solution to the problem by |
| 296 |
deciding to try the approach of scanning through |
| 297 |
the array one element at a time maintaining a |
| 298 |
useful invariant through each iteration. A loop |
| 299 |
variable array index @{i@} is created for this |
| 300 |
purpose. The bound function is @{N-i@}. Here is |
| 301 |
the invariant. |
| 302 |
|
| 303 |
@$@<Invariant@>==@{@- |
| 304 |
/* Invariant: sorted(b) and 1<=i<=N and */ |
| 305 |
/* p is len of longest run in b[1..i]. */@} |
| 306 |
|
| 307 |
@D Establishing the invariant above in the |
| 308 |
initial, degenerate case is easy. |
| 309 |
|
| 310 |
@$@<Establish plateau loop invariant@>@{i=1; p=1;@} |
| 311 |
|
| 312 |
@D At this stage, we have the following loop |
| 313 |
structure. Note that when both the invariant and |
| 314 |
@{i == N@} are true, the postcondition holds and |
| 315 |
the loop can terminate. |
| 316 |
|
| 317 |
@$@<p=len(longest run in sorted b[1..N])@>@{@- |
| 318 |
@<Precondition@> |
| 319 |
@<Establish plateau loop invariant@> |
| 320 |
while (i != N) |
| 321 |
{ |
| 322 |
@<Invariant@> |
| 323 |
@<Loop body@> |
| 324 |
} |
| 325 |
@<Postcondition@> |
| 326 |
@} |
| 327 |
|
| 328 |
@D Now there remains only the loop body whose |
| 329 |
sole task is to increase @{i@} (and so decrease |
| 330 |
the value of the bound function) while maintaining |
| 331 |
the invariant. If @{p@} is the length of the |
| 332 |
longest run seen so far (i.e. in b[1..i]), then, |
| 333 |
because the array is sorted, the extension of our |
| 334 |
array range to @{b[1..i+1]@} can only result in |
| 335 |
an increase in @{p@} if the new element |
| 336 |
terminates a run of length @{p+1@}. The increase |
| 337 |
can be at most 1. Because the array is sorted, we |
| 338 |
need only compare the endpoints of this possible |
| 339 |
run to see if it exists. This is performed as |
| 340 |
shown below. |
| 341 |
|
| 342 |
@$@<Loop body@>==@{i++; if (b[i] != b[i-p]) p++;@} |
| 343 |
</PRE> |
| 344 |
|
| 345 |
<P>Where the code is more obvious, |
| 346 |
it is often better to let the code speak for itself. |
| 347 |
|
| 348 |
<P> |
| 349 |
<PRE> |
| 350 |
@C The following function compares two C |
| 351 |
strings and returns TRUE iff they are identical. |
| 352 |
|
| 353 |
@$@<Function comp@>==@{@- |
| 354 |
bool comp(p,q) |
| 355 |
char *p,*q; |
| 356 |
{ |
| 357 |
while (TRUE) |
| 358 |
{ |
| 359 |
if (*p != *q ) return FALSE; |
| 360 |
if (*p == '\0') return TRUE; |
| 361 |
p++; q++; |
| 362 |
} |
| 363 |
} |
| 364 |
@} |
| 365 |
</PRE> |
| 366 |
|
| 367 |
|
| 368 |
<P> |
| 369 |
<TABLE WIDTH="100%"> |
| 370 |
<TR> |
| 371 |
<TD ALIGN="left" VALIGN="bottom"><A HREF="hints_make.html"><IMG SRC="binary/fw_left.gif" HEIGHT="32" WIDTH="32" BORDER="0" ALT="Prev"></A></TD> |
| 372 |
<TD ALIGN="center" VALIGN="bottom"><A HREF="hints.html"><IMG SRC="binary/fw_up.gif" HEIGHT="32" WIDTH="32" BORDER="0" ALT="Up"></A></TD> |
| 373 |
<TD ALIGN="right" VALIGN="bottom"><A HREF="hints_debugging.html"><IMG SRC="binary/fw_right.gif" HEIGHT="32" WIDTH="32" BORDER="0" ALT="Next"></A></TD> |
| 374 |
</TR> |
| 375 |
</TABLE> |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
<HR> |
| 380 |
<FONT SIZE="2"> |
| 381 |
<A HREF="mailto:[email protected]">Webmaster</A> |
| 382 |
<A HREF="copyright.html">Copyright © Ross N. Williams 1992,1999. All rights reserved.</A><BR> |
| 383 |
</FONT> |
| 384 |
|
| 385 |
</FONT> |
| 386 |
</TD> |
| 387 |
</TR> |
| 388 |
</TABLE> |
| 389 |
|
| 390 |
|
| 391 |
</BODY> |
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
</HTML> |