
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. TomDuff Bell Labs Coding So if your program takes lo7 seconds, be prepared to When in doubt, use brute force. wait four months. Ken Thompson February’s column solicited bumper-sticker-sized ad- Bell Labs vice on computing. Some of the contributions aren’t 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 Norristown, Petmsylvania 70 percent: these tests save development time. There’s a problem with advice that is even less quan- titative. Everyone agrees that Avoid asymmetry. Andy Huber Absence makes the heart grow fonder. Data General Corporation Anorl. and The sooner you start to code, the longer the program will take. Out of sight, out of mind. Roy Carlson Anon. University of Wisconsin Everyone, that is, except the sayings themselves-they are contradictory. There are similar contradictions in If you can’t write it down in English, you can’t code it the slogans in this column. Although there is some Peter Halpern truth in each saying in this column, all should be taken Brooklyn, New York with a grain of salt. A word about credit. The name associated with a Details count. 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 Don’t debug standing up. It cuts your patience in half, If you have too many special cases, you are doing it 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 terribly misleading. Debug only the code. Get your data structures correct first, and the rest of 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. Edsger W. Dijkstra User Interfaces University of Texas [The Principle of Least Astonishment] Make a user in- 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. Brian Kernighan A program designed for inputs from people is usually 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- ial logical errors. Of the remaining 4 percent, 80 per- On some machines indirection is slower with displace- cent are pointer errors. And the remaining 0.8 percent ment, so the most-used member of a structure or a 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 The job’s not over until the paperwork’s done. If the programmer can simulate a construct faster than 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 Encore Computer Corporation Less than 10 percent of the code has to do with the 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. May Shaw Bill McKeeman Carnegie-Mellon University Wang Znstitute Good judgment comes from experience, and experience Electricity travels a foot in a nanosecond. comes from bad judgment. Commodore Grace Murray Hopper Fred Brooks United States Navy 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.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages6 Page
-
File Size-