Programming/Software Engineering
The
Practice
of
Programming
With the same insight and authority that made their book The Unix Programming
Environment a classic, Brian Kernighan and Rob Pike have written The Practice
of Programming to help make individual programmers more effective and
productive.
The practice of programming is more than just writing code. Programmers must
also assess tradeoffs, choose among design alternatives, debug and test, improve
performance, and maintain software written by themselves and others. At the
same time, they must be concerned with issues like compatibility, robustness,
and reliability, while meeting specifications.
The Practice of Programming covers all these topics, and more. This book
is
full
of practical advice and real
-
world examples in
C, C++,
lava, and a variety of
special
-
purpose languages. It includes chapters on:
debugging: finding bugs quickly and methodically
testing: guaranteeing that software works correctly and reliably
performance: making programs faster and more compact
portability: ensuring that programs run ever
y
where without change
design: balancing goals and constraints to decide which algorithms and data
structures are best
interfaces: using abstraction and information hiding to control the interactions
between components
style: writing code that works well and is a pleasure to read
notation: choosing languages and tools that let the machine do more of the
work
Kernighan and Pike have distilled years of experience writing programs,
teaching, and working with other programmers to create this book. Anyone who
writes software will profit from the principles and guidance in The Practice of
Programming.
Brian
W.
Kernighan
and
Rob
Pike
work in the Computing Science Research
Center at Bell Laboratories, Lucent Technologies. Brian Kernighan is Consulting
Editor for Addison
-
Wesley's Professional Computing Series and the author, with
Dennis Ritchie, of The
C
Programming Language. Rob Pike was a lead architect
and implementer of the Plan
9
and Inferno operating systems. His research
focuses on software that makes it easier for people to write software
Cover art by Renee French
QText printed on recycled paper
h
ADDISON
-
WESLEY
Addison
-
Wesley is an imprint of
Addison Wesley
Longman, Inc.
The Practice of Programming
Many of the designations used by manufacturers and sellers to distinguish their products are
claimed as trademarks. Where those designations appear in this book, and Addison Wesley
Longman, Inc. was aware of
a
trademark claim. the designations have been printed in initial
capital letters or all capital letters.
The authors and publisher have taken care in preparation of this book, but make no expressed or
implied warranty of any kind and assume no responsibility for errors or omissions. No liability is
assumed for incidental or consequential damages in connection with or arising out of the use of
the information or programs contained herein,
The publisher offers discounts of this book when ordered in quantity for special sales. For more
information, please contact:
Computer and Engineering Publishing Group
Addison Wesley
Longman, Inc.
One Jacob Way
Reading, Massachusetts 01 867
This book was typeset
(gri~l~l)icltI)Ilqnlt~nff
-nip)
in Times and Lucida Sans Typewriter by the
authors.
Library of Congress Cataloging
-
in
-
Publication Data
Kernighan, Brian W.
The practice of programming
1
Brian W. Kernighan, Rob Pike.
p. cm.
--
(Addison
-
Wesley professional computing series)
Includes bibliographical references.
ISBN 0
-
201
-
6 1586
-
X
1.
Computer programming. I. Pike, Rob. 11. Title. 111. Series.
QA76.6
.K48 1999
005.1--dc2 1
99
-
10131
CIP
Copyright
O
1999 by Lucent Technologies.
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, photocopying, recording, or
otherwise, without the prior written permission of the publisher. Printed in the United States of
America. Published simultaneously in Canada.
Text
printed
on
recycled and acid-free paper.
ISBN
0
-
201
-
61586
-
X
2 34567
CRS
02010099
2nd
Printing May
1999
Contents
Preface
Chapter
1:
Style
1.1
Names
1.2
Expressions and Statements
1.3
Consistency and Idioms
1.4
Function Macros
1.5
Magic Numbers
1.6
Comments
1.7
Why Bother?
Chapter
2:
Algorithms and
Data
Structures
2.1
Searching
2.2
Sorting
2.3
Libraries
2.4
A Java Quicksort
2.5
0
-
Notation
2.6
Growing Arrays
2.7
Lists
2.8
Trees
2.9
Hash Tables
2.10
Summary
Chapter
3:
Design
and Implementation
3.1
The Markov Chain Algorithm
3.2
Data Structure Alternatives
3.3
Building the Data Suucture in C
3.4
Generating Output