309x Filetype PDF File size 0.53 MB Source: moss.cs.iit.edu
programming
pearls
BUMPER-STICKER COMPUTER SCIENCE
Every now and then, programmers have to convert rule is usually the person who sent me the rule, even if
units of time. If a program processes 100 records per they in fact attributed it to their Cousin Ralph (sorry,
second, for instance, how long will it take to process Ralph). In a few cases 1 have listed an earlier reference,
one million records? Dividing shows that the task takes together with the author’s current affiliation (to the
10,000 seconds, and there are 3600 seconds per hour, so best of my knowledge]. I’m sure that 1 have slighted
the answer is about three hours. many people by denying them proper attribution, and
But how many seconds are there in a year? If I tell to them I offer the condolence that
you there are 3.155 X 107, you won’t even try to re- Plagiarism is the sincerest form of flattery.
member it. On the other hand, who could forget that, to Anon.
within half a percent, Without further ado, here’s the advice, grouped into
?r seconds is a nanocentury. a few major categories.
Tom Duff
Bell Labs
So if your program takes lo7 seconds, be prepared to Coding
wait four months. When in doubt, use brute force.
February’s column solicited bumper-sticker-sized ad- Ken Thompson
vice on computing. Some of the contributions aren’t Bell Labs
debatable: Duff’s rule is a memorable statement of a Avoid arc-sine and arc-cosine functions-you can usu-
handy constant. This rule about a program testing ally do better by applying a trig identity or computing a
method (regression tests save old inputs and outputs to vector dot-product.
make sure the new outputs are the same) contains a Jim Conyngham
number that isn’t as ironclad. Arvin/Cnlspan Advanced Technology Center
Regression testing cuts test intervals in half.
Larry Bernstein Allocate four digits for the year part of a date: a new
Bell Communications Research millenium is coming.
Bernstein’s point remains whether the constant is 30 or David Martin
70 percent: these tests save development time. Norristown, Petmsylvania
There’s a problem with advice that is even less quan- Avoid asymmetry.
titative. Everyone agrees that Andy Huber
Absence makes the heart grow fonder. Data General Corporation
Anorl.
and The sooner you start to code, the longer the program
Out of sight, out of mind. will take.
Roy Carlson
Anon. University of Wisconsin
Everyone, that is, except the sayings themselves-they If you can’t write it down in English, you can’t code it
are contradictory. There are similar contradictions in Peter Halpern
the slogans in this column. Although there is some Brooklyn, New York
truth in each saying in this column, all should be taken
with a grain of salt. Details count.
A word about credit. The name associated with a Peter Wrinberger
Q1985 ACMOOOl-0782/85/0900-0896 750 Bell Labs
a96 Communications of the ACM September 1985 Volume 28 Number 9
Programming Pearls
If the code and the comments disagree, then both are It takes three times the effort to find and fix bugs in
probably wrong. system test than when done by the developer. It takes
Norm Sch yer ten times the effort to find and fix bugs in the field than
Belt Labs when done in system test. Therefore, insist on unit tests
by the developer.
A procedure should fit on a page. Larry Bernstein
David Tribble Bell Communications Research
Arlington, Texas
If you have too many special cases, you are doing it Don’t debug standing up. It cuts your patience in half,
and you need all you can muster.
wrong. Dave Storer
Craig Zerouni Cedar Rapids, Iowa
Computer FX Ltd.
London, England Don’t get suckered in by the comments-they can be
Get your data structures correct first, and the rest of terribly misleading. Debug only the code.
Dave Storer
the program will write itself. Cedar Rapids, Iowa
David Iones
Assert, The Netherlands Testing can show the presence of bugs, but not their
absence.
User Interfaces Edsger W. Dijkstra
[The Principle of Least Astonishment] Make a user in- University of Texas
terface as consistent and as predictable as possible. Each new user of a new system uncovers a new class of
Contributed by several readers bugs.
A program designed for inputs from people is usually Brian Kernighan
Bell Labs
stressed beyond the breaking point by computer-
generated inputs. If it ain’t broke, don’t fix it.
Dennis Ritchie Ronald Reagan
Bell Labs Santa Barbara, California
Twenty percent of all input forms filled out by people [The Maintainer’s Motto] If we can’t fix it, it ain’t
contain bad data. broke.
Vie Vyssotsky Lieutenant Colonel Walt Weir
Bell Labs United States Army
Eighty percent of all input forms ask questions they The first step in fixing a broken program is getting it to
have no business asking. fail repeatably.
Mike Garey Tom Duff
Bell Labs Bell Labs
Don’t make the user provide information that the sys-
tem already knows. Performance
Rick Lemons [The First Rule of Program Optimization] Don’t do it.
Cardinal Data Systems [The Second Rule of Program Optimization-For ex-
For 80 percent of all data sets, 95 percent of the infor- perts only] Don’t do it yet.
mation can be seen in a good graph. Michael jackson
William S. Cleveland Michael lackson Systems Ltd.
Bell Labs The fastest algorithm can frequently be replaced by one
that is almost as fast and much easier to understand.
Debugging Douglas W. Iones
Of all my programming bugs, 80 percent are syntax University of lowa
errors. Of the remaining 20 percent, 80 percent are triv- On some machines indirection is slower with displace-
ial logical errors. Of the remaining 4 percent, 80 per- ment, so the most-used member of a structure or a
cent are pointer errors. And the remaining 0.8 percent
are hard. record should be first.
Marc Donner Mike Morton
IBM T. 1. Watson Research Center Boston, Massachusetts
September 1985 Volume 28 Number 9 Communications of the ACM 097
Programming Pearls
In non-I/O-bound programs, a few percent of the Documentation
source code typically accounts for over half the run [The Test of Negation] Don’t include a sentence in doc-
time. umentation if its negation is obviously false.
Don Knuth Bob Martin
Stanford University AT&T Technologies
Before optimizing, use a profiler to locate the “hot When explaining a command, or language feature, or
spots” of the program. hardware widget, first describe the problem it is de-
Mike Morton signed to solve.
Boston, Massachusetts David Martin
Norristown, Pennsylvania
[Conservation of Code Size] When you turn an ordinary [One Page Principle] A (specification, design, proce-
page of code into just a handful of instructions for dure, test plan) that will not fit on one page of 8.5-by-l.1
speed, expand the comments to keep the number of inch paper cannot be understood.
source lines, constant. Mark Ardis
Mike Morton Wang Institute
Boston, Massachusetts
If the programmer can simulate a construct faster than The job’s not over until the paperwork’s done.
Anon.
the compiler can implement the construct itself, then
the compiler writer has blown it badly.
Gu:y L. Steele, jr. Managing Software
Tartan Laboratories The structure of a system reflects the structure of the
organization that built it.
To speed up an I/O-bound program, begin by account- Richard E. Fairley
ing for all 1,/O. Eliminate that which is unnecessary or Wang Institute
redundant, and make the remaining as fast as possible.
David Martin Don’t keep doing what doesn’t work.
Norristown, Pennsylvania Anon.
The fastest I/O is no I/O. [Rule of Credibility] The first 90 percent of the code
Nil’s-Peter Nelson accounts for the first 90 percent of the development
Bell Labs time. The remaining 10 percent of the code accounts
for the other 90 percent of the development time.
The cheapest, fastest, and most reliable components of Tom Cargill
a computer system are those that aren’t there. Belt Labs
Gordon Bell Less than 10 percent of the code has to do with the
Encore Computer Corporation ostensible purpose of the system; the rest deals with
[Compiler Writer’s Motto-Optimization Pass] Making input-output, data validation, data structure mainte-
a wrong program worse is no sin. nance, and other housekeeping.
Bill McKeeman May Shaw
Wang Znstitute Carnegie-Mellon University
Electricity travels a foot in a nanosecond. Good judgment comes from experience, and experience
Commodore Grace Murray Hopper comes from bad judgment.
United States Navy Fred Brooks
University of North Carolina
LISP programmers know the value of everything but Don’t write a new program if one already does more or
the cost of nothing. less what you want. And if you must write a program,
Alan Perlis use existing code to do as much of the work as possible.
Yale University Richard Hill
Hewlett-Packard S.A.
[Little’s Formula] The average number of objects in a Geneva, Switzerland
queue is the product of the entry rate and the average
holding time. Whenever possible, steal code.
Peter Denning Tom Duff
RL4cs Bell Labs
898 Communications of the ACM September 1985 Volume 28 Number 9
Programming Pearls
Good customer relations double productivity. . ..‘.r.il-.!ii)‘~::,: ii tlr:
Larry Bernstein If you lie to the computer, it will get you.
Bell Communications Research Perry Farrar
Germantown, Maryland
Translating a working program to a new language or
system takes 10 percent of the original development If a system doesn’t have to be reliable, it can do any-
time or manpower or cost. thing else.
Douglas W. Jones H. H. Williams
University of Iowa Oakland, California
Don’t use the computer to do things that can be done One person’s constant is another person’s variable.
efficiently by hand. Susan Gerhart
Richard Hill Microelectronics and Computer Technology Corp.
Hewlett-Packard S.A.
Geneva, Switzerland One person’s data is another person’s program.
Don’t use hands to do things that can be done effi- Guy L. Steele, Jr.
ciently by the computer. Tartan Laboratories
Tom Duff
Bell Labs
I’d rather write programs to write programs than write If you’ve made it this far, you’ll certainly appreciate
programs. this excellent advice.
Dick Sites Eschew clever rules.
Digital Equipment Corporation Joe Condon
Bell Labs
[Brooks’s Law of Prototypes] Plan to throw one away,
you will anyhow.
Fred Brooks
University of North Carolina Although this column has allocated just a few words to
If you plan to throw one away, you will throw away each rule, most could be greatly expanded (say, into an
two. undergraduate paper or into a bull session over a few
Craig Zerouni beers). These problems show how one might expand
Computer FX Ltd. the following rule.
London, England Make it work first before you make it work fast.
Bruce Whiteside
Prototyping cuts the work to produce a system by 40 Woodridge, Ittinois
percent. Your “assignment” is to expand other rules in a similar
Larry Bernstein fashion.
Bell Communications Research Restate the rule to be more precise. The example
[Thompson’s rule for first-time telescope makers] It is rule might actually be intended as
faster to make a four-inch mirror then a six-inch mirror Ignore efficiency concerns until a program is known
than to make a six-inch mirror. to be correct.
Bill McKeeman or as
Wang Institute
Furious activity is no substitute for understanding. If a program doesn’t work, it doesn’t matter how
fast it runs; after all, the null program gives a wrong
H. H. Williams answer in no time at all.
Oakland, California Present small, concrete examples to support your
Always do the hard part first. If the hard part is impos- rule. In Chapter 7 of their Elements of Programming
sible, why waste time on the easy part? Once the hard Style, Kernighan and Plauger present 10 tangled
part is done, you’re home free. lines of code from a programming text; the convo-
Always do the easy part first. What you think at first is luted code saves a single comparison (and inciden-
the easy part often turns out to be the hard part. Once tally introduced a minor bug). By “wasting” an
the easy part is done, you can concentrate all your extra comparison, they replace the code with two
efforts on the hard part. crystal-clear lines. With that object lesson fresh on
Al Schapira the page, they present the rule
Bell Labs Make it right before you make it faster.
September 1985 Volume 28 Number 9 Communications of the ACM
no reviews yet
Please Login to review.