Copyright © 1969 by Chapman and Hall, Ltd By permission Paul M Chirlian, lntroductt0n to Fortran IV Copyright © 1973 by Academic Press By permission Frank J Clark, lntroduc11on to PL/I
Trang 1THE ELEMENTS
OF
PROGRAMMING
STYLE
SECOND EDITION
Kernighan and Plauger
Trang 2THE ELEMENTS
OF
PROGRAMMING
STYLE
Second Edition
Brian W Kernighan
Bell laboratories Murray Hill, New Jersey
P J Plauger
Yourdon, Inc
New York, New York
McGRAW-HILL BOOK COMPANY
Trang 3Library of Congress Cataloging in Publication Data
Kernighan, Brian W
The elements of programming style
Bibliography: p
Includes index
I Electronic digital computers-Programming
I Plauger, P.J., date joint author
II Title
QA 76.6.K47 1978 001.6'42 78-3498
ISBN 0-07-034207-5
The Elements of Programming Style
Copyright © 1978, 1974 by Bell Telephone Laboratories, Incorporated
All rights reserved No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, electronic, mechanical, photocopy-ing, recordphotocopy-ing, or otherwise, without the prior written permission of Bell Laboratories Printed in the United States of America
12 13 14 15 DODO 8 9
This book was set in Times Roman and Courier 12 by the authors, using a Graphic Sys-tems phototypesetter driven by a PDP-11/70 running under the UNIX operating system
UNIX is a Trademark of Bell Laboratories
Trang 41973 by R V Andree By permission of Prentice-Hall, Inc
F Bates and M L Douglas, Programmmg Language/One with Structured Programming (Thtrd Edmon) Copyright © 1975 by Prentice-Hall, Inc Reprinted by permission
C R Bauer and A P Peluso, Basic Fortran IV with Waifor & Wa(/iv Copyright © 1974 by Addison-Wesley Publishing Com-pany, Inc By permission
C R Bauer, A P Peluso, and D A Gomberg, Baste PL/I Programming Copyright © 1968 by Addison-Wesley Publishing Company, Inc By permission
M Bohl and A Walter, Introduction to PL/I Programming and PL/C Copyright © 1973 by Science Research Associates, Inc Reprinted by permission of the publisher
V J Calderbank, A Course on Programming m Fortran IV Copyright © 1969 by Chapman and Hall, Ltd By permission Paul M Chirlian, lntroductt0n to Fortran IV Copyright © 1973 by Academic Press By permission
Frank J Clark, lntroduc11on to PL/I Programming Copyright© 1971 by Allyn and Bacon, Inc By permission
Computerworld Copyright© 1972 by Computerworld, Newton, Mass 02160 By permission
Datoma11on 9 Copyright© 1972, 1973 by Technical Publishing Company, Greenwich, Connecticut 06830 Reprinted with
per-mission
D F DeTar, Prmetples of Fortran Programming Copyright © 1972 by W A Benjamin, Inc, Menlo Park, California By per-mission of the publisher
H Dinter, lntroductt0n to Computing Copyright © 1973, Heinz Dinter By permission of The Macmillan Company, New York
D Dmitry and T Mott, Jr, lntroduc11on to Fortran IV Programming Copyright © Holt, Rinehart and Winston, Inc, 1966 By
permission
V T Dock, Fortran IV Programming, Copyright © 1972 by Reston Publishing Company, Inc By permission
W S Dorn, G G Bitter, and D L Hector, Computer Appltca11ons for Calculus Copyright © Prindle, Weber & Schmidt, Inc,
1972 By permission
W S Dorn and D D McCracken, Numerical Methods with Fortran IV Case Studtes Copyright © 1972 by John Wiley & Sons, Inc By permission
L E Edwards, PL// for Busmess Applica11ons Copyright© 1973 by Reston Publishing Company, Inc By permission
M V Farina, Fortran IV Seif-Taught Copyright© Prentice-Hall, Inc, 1966 By permission
B S Gottfried, Programming with Fortran IV Copyright© 1972 by Quantum Publishers, Inc By permission
Gabriel F Groner, PL// Programming 1n Technological Applteatt0ns Copyright © 1971 by John Wiley and Sons, Inc Reprinted
by permission of the publisher
N Haag, Comprehensive Standard Fortran Programmmg Copyright© Hayden Book Company, Inc, 1969 By permission
K Hughes, PL/I Programming Copyright © 1973 by John Wiley & Sons, Inc By permission
K Hughes and J I Michtom, A Structured Approach to Programming Copyright © 1977 by Prentice-Hall, Inc Reprinted by
permission
R J Kochenburger and C J Turcio, Introduction to PL/I and PL/C Programming - Instructor's Guide Copyright © 1974 by John Wiley & Sons, Inc By permission
C B Kreitzberg and B Shneiderman, The Elements of Fortran Style Copyright © 1972 by Harcourt Brace Jovanovich, Inc By
permission
J L Kuester and J H Mize, Op11m1za11on Techniques with Fortran Copyright © 1973 by McGraw-Hill, Inc By permission
S S Kuo, Computer Applica11ons of Numer1cal Methods Copyright ©Addison-Wesley Publishing Company, 1972 By
permis-sion
H L Ledgard, Programming Proverbs Copyright © 1975 by Hayden Book Company By permission
R S Ledley, Fortran IV Programming Copyright© McGraw-Hill, Inc, 1966 By permission
G 0 Manifold, Ca/cu/atmg W1th Fortran Copyright © 1972 by Charles E Merrill Publishing Co , Inc By permission
W A Manning and R S Gamero, A Fortran IV Problem Solver Copyright © McGraw-Hill, Inc, 1970 By permission
E Marxer and D Hartford, Elements of Computer Programming: Fortran Copyright© 1973 Published by Delmar Publishers, a division of Litton Educational Publishing, Inc By permission
D D McCracken, A Guide to Fortran IV Programming Copyright © 1965 by John Wiley and Sons, Inc Reprinted by permis-sion of the publisher
v
Trang 5vi THE ELEMENTS OF PROGRAMMING STYLE
D D McCracken, A Guide to Fortran IV Programming, Second Edmon Copyright © 1972 by John Wiley and Sons, Inc
Re-printed by permission of the publisher
C L McGowan and J R Kelly, Top-Down Structured Programming Techniques Copyright © 1975 by Litton Educational
Pub-lishing, Inc Reprinted by permission of Van Nostrand Reinhold Company, a division of Litton Educational PubPub-lishing, Inc
L P Meissner, The Science ofCompullng Copyright© 1974 by Wadsworth Publishing Company, Inc By permission
H Mullish, Modern Programming: Fortran IV Copyright © 1968 by John Wiley & Sons, Inc By permission
Paul W Murrill and Cecil L Smith, Fortran IV Programming for Engineers and Scientists, Second Edmon Ontext) Copyright ©
1973 by Harper & Row, Publishers, Inc Used by permission of Harper & Row, Publishers
Paul W Murrill and Cecil L Smith, PL/I Programming Ontext) Copyright© 1973 by Harper & Row, Publishers, Inc Used by permission of Harper & Row, Publishers
R L Nolan, Fortran IV Compullng and App/1ca11ons Copyright © Addison-Wesley Publishing Company, 1971 By permission
E I Organick and L P Meissner, For1ran IV (Second Edmon) Copyright © 1974 by Addison-Wesley Publishing Company,
Inc By permission
S V Pollack, A Guide to Fortran IV Copyright © Columbia University Press, 1965 By permission
Seymour V Pollack and Theodor D Sterling, A Guide to PL/I Copyright © 1969 by Holt, Rinehart and Winston Reprinted
by permission of Holt, Rinehart and Winston
Seymour V Pollack and Theodor D Sterling, A Guide 10 Pl// (Second Ed1t1onJ Copyright © 1976 by Holt, Rinehart and
Win•-ton Reprinted by permission of Holt, Rinehart and WinsWin•-ton
Seymour V Pollack and Theodor D Sterling, Essen11als of PL/I Copyright © 1974 by Holt, Rinehart and Winston Reprinted
by permission of Holt, Rinehart and Winston
A Ralston, Fortran IV Programming, A Concise Expos111on Copyright© McGraw-Hill, Inc, 1971 By permission
J K Rice and J R Rice, lntroduc11on to Computer Science Copyright © 1969 by Holt, Rinehart and Winston, Inc Reprinted
by permission of Holt, Rinehart and Winston, Inc
G L Richardson and S J Birkin, Program Solving Using Pl/C Copyright © 1975 by John Wiley ~· Sons, Inc By permission
J S Roper, Pl/I in Easy Stages Copyright© 1973 by Paul Elek (Scientific Books) Ltd By permis-.;m
W P Rule, Fortran IV Programming Copyright © 1968 by W P Rule Prindle, Weber & Schmidt, Inc By permission
School Mathematics Study Group, Algortthms, Computa11on and Mathema11cs, Fortran Supplement Studen1 Tex1 (Revised Edt110n)
Copyright ©Stanford University, 1966 By permission No endorsement by SMSG is implied
G L Scott and J Scott, Pl/I A Se!f-lnstruc11onal Manual Copyright © 1969 by Dickenson Publishing Company By
permis-sion
R C Scott and N E Sondak, Pl// for Programmers Copyright ©Addison-Wesley Publishing Company, 1970 By permission Donald D Spencer, Programming with USA S1andard Fortran and Fortran IV Copyright© 1969 by Xerox Corporation Used by
permission of Ginn and Company (Xerox Corporation)
Donald D Spencer, Computers and Programming Guide For Engineers Copyright © 1973 by Howard W Sams & Co Inc By
permission
R C Sprowls, lntroduc11on to Pl/I Programming Copyright © 1969 by Harper & Row, Publishers Inc By permission
R A Stern and N B Stern Principles of Data Processing Copyright © 1973 by John Wiley & Sons Inc By permission
F Stuart, Fortran Programming Copyright © 1969 by Fredric Stuart Reprinted by permission of John Wiley and Sons, Inc
A Vazsonyi, Problem Solving by D1g1tal Computers wl/h Pl/I Programming Copyright © Prentice-Hall, Inc 1970 By
permis-sion
T M Walker and W W Cotterman, An Introduction to Computer Science and A/gortthm1c Processes Copyright© 1971 by Allyn
and Bacon Inc Used by permission
G M Weinberg, Pl/I Programming· A Manual of Style Copyright© McGraw-Hill, Inc 1970 By permission
Trang 6CONTENTS
vii
Trang 8PREFACE to the Second Edition
The practice of computer programming has changed since The Elements of Pro-gramming Style first appeared Programming style has become a legitimate topic of discussion After years of producing "write-only code," students, teachers, and computing professionals now recognize the importance of readable programs There has also been a widespread acceptance of structured programming as a valuable cod-ing discipline, and a growcod-ing recognition that program design is an important phase, too often neglected in the past
We have revised The Elements of Programming Style extensively to reflect these changes The first edition avoided any direct mention of the term "structured pro-gramming," to steer well clear of the religious debates then prevalent Now that the fervor has subsided, we feel comfortable in discussing structured coding techniques that actually work well in practice
The second edition devotes a whole new chapter to program structure, showing how top-down design can lead to better organized programs Design issues are dis-cussed throughout the text We have made considerable use of pseudo-code as a program development tool
We have also rewritten many of the examples presented in the first edition, to reflect (we hope) a greater understanding of how to program well There are new examples as well, including several from the first edition which now serve as models
of how not to do things New exercises have been added Finally, we have extended and generalized our rules of good style
We are once again indebted to the authors and publishers who have graciously given us permission to reprint material from their textbooks Looking back on some
of our own examples makes us realize how demanding an effort good programming
is
We would also like to thrnk friends who read the second edition in draft form
In particular, Al Aho, Jim Blue, Stu Feldman, Paul Kernighan, Doug Mcilroy, Ralph Muha, and Dick Wexelblat provided us with valuable suggestions
Brian W Kernighan
P J Plauger
ix
Trang 10PREF ACE to the First Edition
Good programming cannot be taught by preaching generalities The way to learn to program well is by seeing, over and over, how real programs can be improved by the application of a few principles of good practice and a little common sense Practice in critical reading leads to skill in rewriting, which in turn leads to better writing
This book is a study of a large number of "real" programs, each of which pro-vides one or more lessons in style We discuss the shortcomings of each example, rewrite it in a better way, then draw a general rule from the specific case The approach is pragmatic and down-to-earth; we are more interested in improving current programming practice than in setting up an elaborate theory of how pro-gramming should be done Consequently, this book can be used as a supplement in
a programming course at any level, or as a refresher for experienced programmers The examples we give are all in Fortran and PL/I, since these languages are widely used and are sufficiently similar that a reading knowledge of one means that the other can also be read well enough (We avoid complicated constructions in either language and explain unavoidable idioms as we encounter them.) The princi-ples of style, however, are applicable in all languages, including assembly codes
Our aim is to teach the elements of good style in a small space, so we concen-trate on essentials Rules are laid down throughout the text to emphasize the les-sons learned Each chapter ends with a summary and a set of "points to ponder," which provide exercises and a chance to investigate topics not fully covered in the text itself Finally we collect our rules in one place for handy reference
A word on the sources of the examples: all of the programs we use are taken from programming textbooks Thus, we do not set up artificial programs to illus-trate our points - we use finished products, written and published by experienced programmers Since these examples are typically the first code seen by a novice pro-grammer, we would hope that they would be models of good style Unfortunately,
we sometimes find that the opposite is true - textbook examples often demonstrate the state of the art of computer programming all too well (We have done our best
to play fair - we don't think that any of the programs are made to look bad by being quoted out of context.)
Let us state clearly, however, that we intend no criticism of textbook authors, either individually or as a class Shortcomings show only that we are all human, and that under the pressure of a large, intellectually demanding task like writing a pro-gram or a book, it is much too easy to do some things imperfectly We have no
xi