The Practice Of Writing Exellent Code
by Pete Goodliffe
PART I AT THE CODEFACE PART II THE SECRET LIFE OF CODE PART III THE SHAPE OF CODE PART IV A HERD OF PROGRAMMERS? PART V PART OF THE PROCESS PART VI VIEW FROM THE TOP
Pete Goodliffe is an expert software developer who never stays at the same
place in the software food chain; he’s worked in numerous languages on
diverse projects. He also has extensive experience in teaching and mentoring
programmers, and writes the regular “Professionalism in Programming”
column for ACCU’s C Vu magazine (www.accu.org). Pete enjoys writing
excellent, bug-free code so he can spend more time having fun with his kids.
ACKNOWLEDGMENTS
There is always something for which to be thankful.
—Charles Dickens
This book was written over a period of several years.
They say good things come to those who wait. In that time
countless people have helped along the way . . .
No one deserves more thanks, and indeed sympathy, than my wife
Bryony who has put up with me and this project over its long gestation
period. Phillipians 1v3.
My good friend, excellent programmer, and illustrator extraordinaire,
David Brookes, took my awful monkey cartoons with lame jokes and turned
them into things of beauty. Thanks Dave! The lame jokes are still my fault.
Many people have read early drafts of this material in one form or
another. Specific thanks are due to ACCU (www.accu.org) which has been
a fertile proving ground for my writing skills. Thanks to the cthree.org geeks
Andy Burrows, Andrew Bennet, and Chris Reed who gave valuable feedback,
to Steve Love, and to the #ant.org geeks. Jon Jagger provided well balanced
technical review and lent his own war stories and battle scars, which have
improved the book considerably.
Most of this book is born from my experience and frustration with the
poor state of software development in the Real World, and a desire to help
people improve. “Thanks” are therefore also due to the various dysfunctional
companies I’ve worked in, and the awful programmers I’ve encountered
there, who have provided me with almost a lifetime’s worth of things to
moan about! I never really realized how lucky I was.
Finally, thanks to all the guys at No Starch Press who have taken my
painful XML formatted manuscript and turned it into a really great book.
Thanks for your faith in the project, and for going the extra mile.
PREFACE
There are many things of which a wise
man might wish to be ignorant.
—Ralph Waldo Emerson
This book comes from the trenches. Well, it actually
comes from deep within the software factory, but sometimes
there isn’t too much difference. This book is for
programmers who care about what they’re doing. If you
don’t, then shut the book now and put it neatly back
on the bookshelf.
What’s In It for Me?
Programming is your passion. It’s sad, but it’s true. As a hardcore techie, you
practically program in your sleep. Now you’re in the heart of the Real World,
deep in the industry, doing what you could never imagine: being paid to play
with computers. The truth is, you’d have paid someone for the privilege.
But this is an odd place, not what you were expecting at all. Surprised by
the incursion of unrealistic deadlines and bad management (if management
is what they call it), of shifting requirements and a legacy of awful code, you’re
left wondering if this is really it. The world is conspiring to prevent you from
writing the code you always dreamed of. Welcome to life in the software
factory. You’re on the front line of a tough battle to create pieces of artistic
mastery and scientific genius. Good luck.
comes from deep within the software factory, but sometimes
there isn’t too much difference. This book is for
programmers who care about what they’re doing. If you
don’t, then shut the book now and put it neatly back
on the bookshelf.
What’s In It for Me?
Programming is your passion. It’s sad, but it’s true. As a hardcore techie, you
practically program in your sleep. Now you’re in the heart of the Real World,
deep in the industry, doing what you could never imagine: being paid to play
with computers. The truth is, you’d have paid someone for the privilege.
But this is an odd place, not what you were expecting at all. Surprised by
the incursion of unrealistic deadlines and bad management (if management
is what they call it), of shifting requirements and a legacy of awful code, you’re
left wondering if this is really it. The world is conspiring to prevent you from
writing the code you always dreamed of. Welcome to life in the software
factory. You’re on the front line of a tough battle to create pieces of artistic
mastery and scientific genius. Good luck.
That’s where Code Craft comes in. This book is about what no one has
taught you yet: how to program, properly, in the Real World. Code Craft picks
up where the textbooks left off. Sure, it’s about the technicalities and intricacies
of good code. But it’s also about something more than that: How to
write the right code, in the right way.
What does that mean? Writing good programs in the Real World means
many things:
Crafting technically elegant code
Creating maintainable code that others can interpret
Understanding and adapting other people’s messy code
Working well alongside other programmers
You need all of these skills (and more) to be a crack coder. You must
understand the secret life of code: What happens to it after you type it. You
must have a sense of aesthetics: distinguishing beautiful code from ugly code.
And you must have a head for the practicalities: to work out when shortcuts
are justified, when to labor on the code design, and when to give up and move
on (the pragmatic quit when you’re ahead principle). This book will help you to
achieve these goals. You’ll learn how to survive the software factory, how to
survey the battlefield and understand your enemy, how to work out tactics to
avoid enemy traps, and how to produce truly excellent programs, despite it all.
Software development is an interesting profession. It’s fast moving, full
of fleeting vogues and transient fashions, get-rich schemes and peddlers of
new ideologies. It’s not mature. I’m not claiming to have any magic answers
here, but I do have some practical, useful advice to impart. There’s no ivory
tower theory—just Real World experience and good practice.
By the time you’ve digested this stuff, you won’t just be a better programmer.
You will be a better inhabitant of the software factory. A real code warrior.
You’ll have learned code craft. If that doesn’t sound exciting, then perhaps
you should consider a career in the military.
Getting Better
So what sets good programmers apart from bad ones? More importantly, what
sets exceptional programmers apart from merely adequate ones? The secret
doesn’t lie solely in technical competence—I’ve seen intellectual programmers
who can write intense and impressive C++, who know their language standard
by heart, but who write the most awful code. I’ve seen more humble programmers
who stick to very simple code, but write the most elegant and
well-thought-out programs.
What’s the real difference? Good programming stems from your attitude. It
lies in knowing the professional approach and always wanting to write the best
software you can, despite the pressures of the software factory. Attitudes are the
lenses through which we view things. They color our work and our actions.
Good code needs to be carefully crafted by master artisans, not thoughtlessly
hacked by sloppy programmers. The code to hell is paved with good intentions.
To become exceptional programmers, we must learn to rise above intentions,
foster positive perspectives, and develop these healthy attitudes.
In this book, we’ll see how to do this. I cover a lot of ground, from
the lowest hands-on code-writing issues to larger organizational concerns.
Through all of these themes, I highlight what our correct attitude and
approach should be.
Who Should Read This Book?
Obviously, the people who should read this book are those who want to
improve the quality of their code. We should all aspire to be better programmers;
if you don’t have that aspiration, then this book isn’t for you.
You might be a professional programmer, perhaps a few years into your
employment. You might be an advanced student, familiar with programming
concepts but unsure about how best to apply them. This book is also a useful
aid if you are being mentored or are mentoring a trainee.
You must have programming experience. This book won’t teach you
how to program; it will teach you how to program better. While I’ve tried to
avoid language bias and dogma, I need to show code examples. Most of
these are written in C, C++, or Java, since they are in the family of popular
contemporary languages. None of them require great language expertise to
read, so don’t panic if you’re not a world-class C++ programmer.
The assumption here is that you are—or will be—writing code in the heat
of the software factory. This often means employment in a commercial development
organization, but it could be working on a chaotic open source
development project, or becoming a hired gun (a contractor) providing
software for a third party.
What’s Covered?
This book addresses programmer attitudes, but it’s not some kind of psychology
textbook. We’ll investigate many topics, including:
Source code presentation
Defensive coding techniques
How to debug programs effectively
Good teamworking skills
Managing your source code
Take a quick glance through the table of contents to see exactly what’s
covered. What is the rationale behind my selection of topics? I’ve been mentoring
trainee programmers for many years, and these are the topics that have
come up time and time again. I’ve also worked in the software factory for
long enough to have seen the recurring problems—I address these too.
If you can conquer all of these programming demons, you’ll progress
from an apprentice coder to a real code craftsman.
How This Book is Organized
I’ve tried to make this book as easy to read as possible. Conventional wisdom
says you should start at the beginning and work to the end. Forget that. You
can pick up this book, open it to a chapter that interests you, and start there.
Each chapter stands on its own, with helpful cross referencing so you can see
how they all fit together. Of course, if you enjoy being conventional, the
beginning is as good a place to start as any.
Each chapter is similarly structured; you won’t find any nasty surprises.
They are split into these sections:
In This Chapter
At the very beginning, I list the highlights of the chapter. You’ll get a few
lines of content overview. Go on, skim through them all now to see what
ground we’ll cover.
The chapter
All the riveting stuff that you paid good money to read.
Dotted throughout the chapter are key concepts. These emphasize the
important tips, issues, and attitudes, so watch out for them. They look
like this:
KEY CONCEPT This is important. Pay attention!
In a Nutshell
At the end of each chapter, this little section wraps up the discussion. It
provides a bird’s eye view of the material. If you’re really pushed for time,
you could just read the key concepts and these concluding sections. Just
don’t tell anyone I said that.
Afterwards, I contrast a good programmer’s approach with that of a
bad programmer to summarize the important attitudes you should aim
to develop. If you’re feeling brave, you can rate yourself against these
examples; hopefully the truth won’t hurt too much!
See Also
This list points you at the related chapters and explains how they tie in
to the topic at hand.
Get Thinking
Finally, there are some questions to consider. These haven’t just been
included to fluff out the book—they are an integral part of each chapter.
They don’t ask for a banal rehashing of the material you just read, but
are intended to make you think, and to think beyond the contents of the
chapter. The questions are split into two groups:
Mull it Over These questions investigate the chapter’s topic in
depth and raise some important issues.
Getting Personal These questions probe the working practices and
coding maturity of you and your software development team.
Don’t skip these questions! Even if you’re too lazy to sit down and
seriously think about each answer (believe me, you’ll gain a lot from
doing so), at least read the questions and consider them in passing.
The final part of this book contains answers and discussion for each of
these questions. It’s not a straight answer set—few of the questions have a
definite yes or no response. Once you’ve thought about them, compare your
answers with mine. Many of my “answers” contain extra information that isn’t
covered in the main chapter.
The Chapters—a Closer Look
Each chapter covers a single topic, a specific problem area in modern software
development. These are the common reasons people write bad code or
write code badly. Each chapter describes the correct approaches and attitudes,
which will make life on the front line more bearable.
The chapters are split into six parts; the contents page for each lists the
chapters in the part with a short description of the material contained in
each. These parts work from the inside, outwards. We’ll start off looking at
what code we write and end up looking at how we write it.
Our investigations begin at the codeface, focusing on the micro level of
writing source code. I’ve deliberately put this first; cutting code is what
programmers really care about:
Part I: At the Codeface
In this part we look at the nuts and bolts of developing source code. We’ll
investigate defensive programming techniques and how to format and
lay out code. Then we’ll move on to look at naming and documenting
our code. Comment-writing conventions and error-handling techniques
are also covered.
Part II: The Secret Life of Code
Next we’ll take a look at the process of writing code; how we create it
and work with it. We’ll look at construction tools, testing methods,
debugging techniques, the correct processes for building executables,
and optimization. Finally, we’ll consider how to write secure programs.
Part III: The Shape of Code
Then we’ll look at the wider issues of source code construction. We’ll
discuss the development of a code design, software architecture, and
how source code grows (or decays) over time.
We then move to the macro level, when we lift up our heads and see
what’s going on around us—life in the software factory. We can’t write largescale
software without being part of a development team, and the next three
parts contain tricks and techniques for getting the best out of these teams:
Part IV: A Herd of Programmers?
Few programmers exist in a vacuum. (It requires special breathing
equipment.) In this part we’ll move into the wider world with a look at
good development practices and how they fit into a professional programmer’s
daily routine. Good personal and team programming skills
and the use of revision control systems are covered here.
Part V: Part of the Process
Here we’ll look at some of the rites and rituals of the software development
process: writing specifications, performing code reviews, and the
black art of timescale estimation.
Part VI: From the Top
The final part provides a higher level look at the development process,
investigating software development methodologies, and the different
programming disciplines.
How to Use This Book
Work from the front cover to the back, or pick it up in the places that interest
you—it doesn’t matter.
What does matter is that you read Code Craft with an open mind, and
think about how to apply what you read to what you do. A wise man learns from
his mistakes; a wiser man learns from the mistakes of others. It’s always good to
learn from others’ experiences, so look at this material, and then ask the
opinion of a programmer you respect. Look over the questions and discuss
them together.
As you learn code craft, I hope you enjoy yourself. When you have
finished, look back and see how much more of the craft you appreciate, how
your skills have grown, and how your attitudes have improved. If nothing has
changed, then this book has failed. I’m sure it won’t.
A Note to Mentors
This book is a great tool for mentoring less experienced programmers. It
has been specifically designed with this in mind, and has proven to increase
programmer maturity and insight.
The best approach to this material is not to methodically work through
each section together. Instead, read a chapter separately, and then get
together with your trainee to discuss the contents. The questions really work
as a springboard for discussion, so it’s a good idea to start there.
Table of Contents
PREFACE xxvii
What’s In It for Me? ................. xxvii
Getting Better .............. xxviii
Who Should Read This Book?..........xxix
What’s Covered? ........................xxx
How This Book Is Organized ..................xxx
The Chapters—A Closer Look ............. xxxii
Part I: At The Codeface................... xxxii
Part II: The Secret Life of Code .............. xxxii
Part III: The Shape of Code............. xxxii
Part IV: A Herd of Programmers ............. xxxii
Part V: Part of the Process.............. xxxii
Part VI: From the Top ................ xxxii
How to Use This Book............... xxxiii
A Note to Mentors ............. xxxiii
ACKNOWLEDGMENTS xxxv
ABOUT THE AUTHOR xxxvii
PART I
AT THE CODEFACE
1 ON THE DEFENSIVE
Defensive Programming Techniques for Robust Code 3
Toward Good Code .................................................................................................. 4
Assume the Worst ..................................................................................................... 4
What Is Defensive Programming?................................................................................ 6
The Big, Bad World .................................................................................................. 8
Techniques for Defensive Programming........................................................................ 8
Employ a Good Coding Style and Sound Design ............................................. 9
Don’t Code in a Hurry .................................................................................. 9
Trust No One............................................................................................. 10
Write Code for Clarity, Not Brevity .............................................................. 10
Don’t Let Anyone Tinker with Stuff They Shouldn’t........................................... 11
Compile with All Warnings Switched On ...................................................... 11
Use Static Analysis Tools ............................................................................. 12
Use Safe Data Structures ............................................................................. 12
Check Every Return Value............................................................................ 13
Handle Memory (and Other Precious Resources) Carefully .............................. 13
Initialize All Variables at Their Points of Declaration ....................................... 14
Declare Variables as Late as Possible ........................................................... 14
Use Standard Language Facilities................................................................. 14
Use a Good Diagnostic Logging Facility ....................................................... 15
Cast Carefully............................................................................................ 15
The Fine Print............................................................................................. 15
Constraints ............................................................................................................. 16
What to Constrain...................................................................................... 17
Removing Constraints ................................................................................. 18
In a Nutshell ........................................................................................................... 20
See Also ................................................................................................................ 20
Get Thinking........................................................................................................... 21
Mull It Over ............................................................................................... 21
Getting Personal ........................................................................................ 22
2 THE BEST LAID PLANS
The Layout and Presentation of Source Code 23
What’s the Big Deal?............................................................................................... 24
Know Your Audience............................................................................................... 25
What Is Good Presentation? ..................................................................................... 26
Brace Yourself ........................................................................................................ 26
K&R Brace Style ......................................................................................... 27
Exdented Brace Style .................................................................................. 27
Indented Brace Style................................................................................... 29
Other Brace Styles...................................................................................... 29
One Style to Rule Them All ....................................................................................... 30
House Styles (and Where to Stick Them) .................................................................... 31
Setting the Standard ................................................................................................ 33
Righteous Wars?..................................................................................................... 35
In a Nutshell ........................................................................................................... 35
See Also ................................................................................................................ 37
Get Thinking........................................................................................................... 37
Mull It Over ............................................................................................... 37
Getting Personal ........................................................................................ 38
3 WHAT’S IN A NAME?
Giving Meaningful Things Meaningful Names 39
Why Should We Name Well?.................................................................................. 41
What Do We Name? .............................................................................................. 41
Name Games......................................................................................................... 42
Descriptive ................................................................................................ 42
Technically Correct..................................................................................... 42
Idiomatic................................................................................................... 43
Appropriate............................................................................................... 43
The Nuts and Bolts .................................................................................................. 44
Naming Variables...................................................................................... 44
Naming Functions ...................................................................................... 45
Naming Types ........................................................................................... 46
Naming Namespaces................................................................................. 47
Naming Macros......................................................................................... 48
Naming Files ............................................................................................. 48
A Rose by Any Other Name..................................................................................... 49
Be Consistent............................................................................................. 50
Exploit Context .......................................................................................... 51
Use Names to Your Advantage.................................................................... 51
In a Nutshell ........................................................................................................... 52
See Also ................................................................................................................ 53
Get Thinking........................................................................................................... 53
Mull It Over ............................................................................................... 53
Getting Personal ........................................................................................ 55
4 THE WRITE STUFF
Techniques for Writing “Self-Documenting” Code 57
Self-Documenting Code............................................................................................ 59
Techniques for Self-Documenting Code ...................................................................... 61
Write Simple Code with Good Presentation................................................... 61
Choose Meaningful Names ......................................................................... 62
Decompose into Atomic Functions ................................................................ 62
Choose Descriptive Types............................................................................ 63
Name Constants ........................................................................................ 63
Emphasize Important Code.......................................................................... 64
Group-Related Information........................................................................... 64
Provide a File Header ................................................................................. 64
Handle Errors Appropriately ........................................................................ 65
Write Meaningful Comments ....................................................................... 65
Practical Self-Documentation Methodologies ............................................................... 66
Literate Programming.................................................................................. 66
Documentation Tools .................................................................................. 67
In a Nutshell ........................................................................................................... 69
See Also ................................................................................................................ 70
Get Thinking........................................................................................................... 71
Mull It Over ............................................................................................... 71
Getting Personal ........................................................................................ 72
5 A PASSING COMMENT
How to Write Code Comments 73
What Is a Code Comment? ...................................................................................... 74
What Do Comments Look Like?................................................................................. 75
How Many Comments?............................................................................................ 75
What Goes Inside Our Comments? ........................................................................... 76
Explain Why, Not How .............................................................................. 76
Don’t Describe the Code ............................................................................. 76
Don’t Replace Code ................................................................................... 76
Keep It Useful ............................................................................................ 77
Avoid Distractions ...................................................................................... 78
In Practice .............................................................................................................. 79
A Comment on Aesthetics......................................................................................... 80
Consistency ............................................................................................... 80
Clear Block Comments................................................................................ 80
Indenting Comments ................................................................................... 81
End-of-Line Comments ................................................................................. 81
Helping You to Read the Code .................................................................... 81
Choose a Low-Maintenance Style ................................................................. 82
Breakwaters .............................................................................................. 82
Flags ........................................................................................................ 83
File Header Comments................................................................................ 83
Working with Comments.......................................................................................... 84
Helping You to Write Routines ..................................................................... 84
Bug-Fix Notices.......................................................................................... 85
Comment Rot ............................................................................................. 85
Maintenance and the Inane Comment........................................................... 86
In a Nutshell ........................................................................................................... 86
See Also ................................................................................................................ 87
Get Thinking........................................................................................................... 87
Mull It Over ............................................................................................... 87
Getting Personal ........................................................................................ 88
6 TO ERR IS HUMAN
Dealing with the Inevitable—Error Conditions in Code 89
From Whence It Came............................................................................................. 90
Error-Reporting Mechanisms ..................................................................................... 91
No Reporting............................................................................................. 91
Return Values............................................................................................. 92
Error Status Variables ................................................................................. 93
Exceptions................................................................................................. 93
Signals ..................................................................................................... 95
Detecting Errors ...................................................................................................... 95
Handling Errors....................................................................................................... 96
When to Deal with Errors ............................................................................ 97
Possible Reactions ...................................................................................... 98
Code Implications .................................................................................... 100
Raising Hell .......................................................................................................... 104
Managing Errors................................................................................................... 105
In a Nutshell ......................................................................................................... 106
See Also .............................................................................................................. 107
Get Thinking......................................................................................................... 107
Mull It Over ............................................................................................. 107
Getting Personal ...................................................................................... 108
PART II
THE SECRET LIFE OF CODE
7 THE PROGRAMMER’S TOOLBOX
Using Tools to Construct Software 111
What Is a Software Tool?....................................................................................... 112
Why Worry About Tools? ...................................................................................... 114
Power Tools.......................................................................................................... 115
Understand What It Can Do ...................................................................... 115
Learn How to Drive it ................................................................................ 116
Know What Tasks It’s Good For................................................................. 116
Check That It’s Working............................................................................ 116
Have a Clear Route to Find Out More......................................................... 117
Find Out When New Versions Appear ....................................................... 117
Which Tools? ....................................................................................................... 117
Source Editing Tools ................................................................................. 118
Code Construction Tools ........................................................................... 120
Debugging and Investigative Tools ............................................................. 123
Language Support Tools............................................................................ 124
Miscellaneous Tools.................................................................................. 125
In a Nutshell ......................................................................................................... 126
See Also .............................................................................................................. 127
Get Thinking......................................................................................................... 128
Mull It Over ............................................................................................. 128
Getting Personal ...................................................................................... 128
8 TESTING TIMES
The Black Art of Testing Code 129
Reality Check........................................................................................................ 131
Who, What, When, and Why? .............................................................................. 132
Why We Test .......................................................................................... 132
Who Tests ............................................................................................... 133
What Testing Involves ............................................................................... 133
When We Test ........................................................................................ 134
Testing Isn’t Hard . . . ............................................................................................ 135
The Types of Test................................................................................................... 138
Choosing Unit Test Cases....................................................................................... 142
Design for Test ...................................................................................................... 144
Look! No Hands! .................................................................................................. 144
The Face of Failure ................................................................................................ 145
Can You Manage It? ............................................................................................. 146
Fault-Tracking System................................................................................ 147
Bug Reviews ............................................................................................ 148
In a Nutshell ......................................................................................................... 149
See Also .............................................................................................................. 150
Get Thinking......................................................................................................... 150
Mull It Over ............................................................................................. 150
Getting Personal ...................................................................................... 151
9 FINDING FAULT
Debugging: What to Do When Things Go Wrong 153
The Facts of Life .................................................................................................... 154
Nature of the Beast ............................................................................................... 155
The View from 1,000 Feet......................................................................... 155
The View from the Ground ........................................................................ 156
The View from the Trenches ....................................................................... 158
Pest Extermination ................................................................................................. 160
The Low Road.......................................................................................... 161
The High Road......................................................................................... 161
Bug Hunting ......................................................................................................... 162
Compile-Time Errors ................................................................................. 162
Run-Time Errors ........................................................................................ 164
How to Fix Faults .................................................................................................. 167
Prevention ............................................................................................................ 169
Wasp Spray, Slug Repellent, Fly Paper . . . .............................................................. 169
Debugger................................................................................................ 169
Memory Access Validator ......................................................................... 170
System Call Tracing.................................................................................. 170
Core Dump ............................................................................................. 170
Logging .................................................................................................. 170
In a Nutshell ......................................................................................................... 171
See Also .............................................................................................................. 172
Get Thinking......................................................................................................... 173
Mull It Over ............................................................................................. 173
Getting Personal ...................................................................................... 173
10 THE CODE THAT JACK BUILT
Mechanisms to Turn Source Code into Executable Code 175
Language Barriers ................................................................................................. 176
Interpreted Languages............................................................................... 177
Compiled Languages................................................................................ 178
Byte-Compiled Languages ......................................................................... 179
Making Mountains out of Molehills.......................................................................... 179
Building Builds ...................................................................................................... 181
What Makes a Good Build System? ........................................................................ 184
Simplicity ................................................................................................ 184
Uniformity ............................................................................................... 184
Repeatable and Reliable ........................................................................... 185
Atomic.................................................................................................... 186
Coping with Errors ................................................................................... 187
The Mechanics...................................................................................................... 187
Choice of Targets..................................................................................... 187
Housekeeping.......................................................................................... 189
Dependencies.......................................................................................... 189
Automated Builds ..................................................................................... 190
Build Configuration .................................................................................. 191
Recursive Make ....................................................................................... 192
Please Release Me ................................................................................................ 192
Jack-of-All-Trades, Buildmaster Of? .......................................................................... 194
In a Nutshell ......................................................................................................... 195
See Also .............................................................................................................. 195
Get Thinking......................................................................................................... 196
Mull It Over ............................................................................................. 196
Getting Personal ...................................................................................... 196
11 THE NEED FOR SPEED
Optimizing Programs and Writing Efficient Code 199
What Is Optimization?........................................................................................... 200
What Makes Code Suboptimal? ............................................................................. 201
Why Not Optimize?.............................................................................................. 202
Alternatives ............................................................................................. 204
Why Optimize? .................................................................................................... 205
The Nuts and Bolts ................................................................................................ 206
Prove You Need to Optimize ..................................................................... 206
Identify the Slowest Code.......................................................................... 207
Testing the Code ...................................................................................... 208
Optimizing the Code ................................................................................ 209
After Optimization.................................................................................... 209
Optimization Techniques........................................................................................ 210
Design Changes....................................................................................... 210
Code Changes ........................................................................................ 213
Writing Efficient Code ........................................................................................... 217
In a Nutshell ......................................................................................................... 219
See Also .............................................................................................................. 219
Get Thinking......................................................................................................... 220
Mull It Over ............................................................................................. 220
Getting Personal ...................................................................................... 221
12 AN INSECURITY COMPLEX
Writing Secure Programs 223
The Risks .............................................................................................................. 224
The Opposition ..................................................................................................... 226
Excuses, Excuses ................................................................................................... 228
Feeling Vulnerable ................................................................................................ 229
Insecure Design and Architecture ............................................................... 229
Buffer Overrun ......................................................................................... 229
Embedded Query Strings .......................................................................... 230
Race Conditions....................................................................................... 231
Integer Overflow ...................................................................................... 231
Protection Racket................................................................................................... 232
System Installation Techniques ................................................................... 233
Software Design Techniques ...................................................................... 234
Code Implementation Techniques ............................................................... 235
Procedural Techniques .............................................................................. 236
In a Nutshell ......................................................................................................... 236
See Also .............................................................................................................. 237
Get Thinking......................................................................................................... 237
Mull It Over ............................................................................................. 237
Getting Personal ...................................................................................... 238
PART III
THE SHAPE OF CODE
13 GRAND DESIGNS
How to Produce Good Software Designs 241
Programming as Design ......................................................................................... 242
What Do We Design? ........................................................................................... 243
What’s All the Fuss About?..................................................................................... 244
Good Software Design .......................................................................................... 245
Simplicity ................................................................................................ 246
Elegance................................................................................................. 247
Modularity .............................................................................................. 247
Good Interfaces ....................................................................................... 248
Extensibility ............................................................................................. 251
Avoid Duplication .................................................................................... 251
Portability................................................................................................ 252
Idiomatic................................................................................................. 252
Well-Documented..................................................................................... 253
How to Design Code ............................................................................................. 253
Design Methods and Processes .................................................................. 254
Design Tools............................................................................................ 255
In a Nutshell ......................................................................................................... 257
See Also .............................................................................................................. 258
Get Thinking......................................................................................................... 258
Mull It Over ............................................................................................. 258
Getting Personal ...................................................................................... 259
14 SOFTWARE ARCHITECTURE
Laying the Foundations of Software Design 261
What Is Software Architecture? ............................................................................... 262
Software Blueprints................................................................................... 262
Points of View.......................................................................................... 263
Where and When Do You Do It? ............................................................... 264
What Is It Used For? ................................................................................. 265
Of Components and Connections............................................................... 266
What Is Good Architecture? ................................................................................... 268
Architectural Styles ................................................................................................ 269
No Architecture ....................................................................................... 269
Layered Architecture ................................................................................. 270
Pipe and Filter Architecture........................................................................ 271
Client/Server Architecture ......................................................................... 271
Component-Based Architecture .................................................................. 273
Frameworks............................................................................................. 274
In a Nutshell ......................................................................................................... 275
See Also .............................................................................................................. 276
Get Thinking......................................................................................................... 276
Mull It Over ............................................................................................. 276
Getting Personal ...................................................................................... 277
15 SOFTWARE EVOLUTION OR SOFTWARE REVOLUTION?
How Does Code Grow? 279
Software Rot......................................................................................................... 281
The Warning Signs ............................................................................................... 282
How Does Code Grow?......................................................................................... 284
Believe the Impossible............................................................................................ 286
What Can We Do About This? ............................................................................... 287
Writing New Code .................................................................................. 287
Maintenance of Existing Code ................................................................... 288
In a Nutshell ......................................................................................................... 290
See Also .............................................................................................................. 290
Get Thinking......................................................................................................... 291
Mull It Over ............................................................................................. 291
Getting Personal ...................................................................................... 292
PART IV
A HERD OF PROGRAMMERS?
16 CODE MONKEYS
Fostering the Correct Attitude and Approach to Programming 295
Monkey Business................................................................................................... 296
The Eager Coder...................................................................................... 297
The Code Monkey.................................................................................... 298
The Guru................................................................................................. 299
The Demiguru .......................................................................................... 300
The Arrogant Genius ................................................................................ 300
The Cowboy............................................................................................ 302
The Planner ............................................................................................. 302
The Old Timer.......................................................................................... 303
The Zealot ............................................................................................... 304
The Monocultured Programmer .................................................................. 305
The Slacker ............................................................................................. 306
The Reluctant Team Leader ........................................................................ 306
You ........................................................................................................ 307
The Ideal Programmer............................................................................................ 308
So What? ............................................................................................................ 308
The Stupidest of Men ............................................................................................. 309
In a Nutshell ......................................................................................................... 310
See Also .............................................................................................................. 310
Action Sheet ......................................................................................................... 311
Get Thinking......................................................................................................... 312
Mull It Over ............................................................................................. 312
Getting Personal ...................................................................................... 312
17 TOGETHER WE STAND
Teamwork and the Individual Programmer 315
Our Teams—The Big Picture ................................................................................... 316
Team Organization ............................................................................................... 318
Management Approach ............................................................................ 318
Division of Responsibility........................................................................... 318
Organization and Code Structure .............................................................. 320
Teamwork Tools .................................................................................................... 320
Team Diseases...................................................................................................... 322
Tower of Babel ........................................................................................ 322
Dictatorship ............................................................................................. 324
Development Democracy........................................................................... 325
Satellite Station ........................................................................................ 327
The Grand Canyon .................................................................................. 329
Quicksand .............................................................................................. 330
Lemmings ................................................................................................ 332
Personal Skills and Characteristics for Good Teamwork ............................................. 333
Communication........................................................................................ 333
Humility .................................................................................................. 334
Dealing with Conflict ................................................................................ 334
Learning and Adaptability ......................................................................... 335
Know Your Limitations............................................................................... 336
Teamwork Principles.............................................................................................. 336
Collective Code Ownership....................................................................... 336
Respect Other People’s Code..................................................................... 337
Code Guidelines...................................................................................... 337
Define Success......................................................................................... 337
Define Responsibility................................................................................. 338
Avoid Burnout.......................................................................................... 338
The Team Life Cycle............................................................................................... 339
Team Creation ......................................................................................... 339
Team Growth........................................................................................... 341
Teamwork ............................................................................................... 342
Team Closure .......................................................................................... 343
In a Nutshell ......................................................................................................... 345
See Also .............................................................................................................. 346
Action Sheet ......................................................................................................... 347
Get Thinking......................................................................................................... 348
Mull It Over ............................................................................................. 348
Getting Personal ...................................................................................... 348
18 PRACTICING SAFE SOURCE
Source Control and Self-Control 349
Our Responsibility ................................................................................................. 350
Source Control...................................................................................................... 351
Revision Control....................................................................................... 352
Access Control......................................................................................... 353
Working with the Repository...................................................................... 354
Leave Branching to the Trees ..................................................................... 354
A Brief History of Source Control................................................................ 356
Configuration Management.................................................................................... 356
Backups ............................................................................................................... 358
Releasing Source Code.......................................................................................... 359
Wherever I Lay My Source ..................................................................................... 360
In a Nutshell ......................................................................................................... 361
See Also .............................................................................................................. 362
Get Thinking......................................................................................................... 363
Mull It Over ............................................................................................. 363
Getting Personal ...................................................................................... 363
PART V
PART OF THE PROCESS
19 BEING SPECIFIC
Writing Software Specifications 367
What Are They, Specifically?.................................................................................. 368
The Types of Specification...................................................................................... 369
Requirements Specification ........................................................................ 371
Functional Specification ............................................................................ 373
System Architecture Specification ............................................................... 373
User Interface Specification ....................................................................... 374
Design Specification................................................................................. 374
Test Specification ..................................................................................... 375
What Should Specifications Contain? ...................................................................... 376
The Specification-Writing Process ............................................................................ 379
Why Don’t We Write Specifications? ...................................................................... 381
In a Nutshell ......................................................................................................... 383
See Also .............................................................................................................. 383
Get Thinking......................................................................................................... 384
Mull It Over ............................................................................................. 384
Getting Personal ...................................................................................... 384
20 A REVIEW TO A KILL
Performing Code Reviews 385
What Is a Code Review?........................................................................................ 386
When Do You Review? .......................................................................................... 387
Whether to Review................................................................................... 388
Which Code to Review ............................................................................. 389
Performing Code Reviews....................................................................................... 389
Code Review Meetings ............................................................................. 390
Integration Reviews .................................................................................. 392
Review Your Attitudes ............................................................................................ 393
The Author’s Attitude ................................................................................ 393
The Reviewer’s Attitude ............................................................................. 394
Code Perfection .................................................................................................... 395
Beyond the Code Review ....................................................................................... 396
In a Nutshell ......................................................................................................... 397
See Also .............................................................................................................. 397
Checklist .............................................................................................................. 398
Get Thinking......................................................................................................... 399
Mull It Over ............................................................................................. 399
Getting Personal ...................................................................................... 399
21 HOW LONG IS A PIECE OF STRING?
The Black Art of Software Timescale Estimation 401
A Stab in the Dark................................................................................................. 402
Why Is Estimation So Hard? ................................................................................... 403
Under Pressure...................................................................................................... 405
Practical Ways to Estimate ..................................................................................... 406
The Planning Game............................................................................................... 409
Keep Up! ............................................................................................................. 412
In a Nutshell ......................................................................................................... 415
See Also .............................................................................................................. 415
Get Thinking......................................................................................................... 416
Mull It Over ............................................................................................. 416
Getting Personal ...................................................................................... 416
PART VI
VIEW FROM THE TOP
22 RECIPE FOR A PROGRAM
Code Development Methodologies and Processes 419
Programming Styles ............................................................................................... 420
Structured Programming............................................................................ 421
Object-Oriented Programming ................................................................... 422
Functional Programming............................................................................ 423
Logic Programming .................................................................................. 424
Recipes: The How and the What............................................................................. 424
Development Processes .......................................................................................... 425
Ad Hoc................................................................................................... 426
Waterfall Model ...................................................................................... 427
SSADM and PRINCE ................................................................................ 429
V Model.................................................................................................. 430
Prototyping.............................................................................................. 430
Iterative and Incremental Development ........................................................ 432
Spiral Model ........................................................................................... 432
Agile Methodologies ................................................................................ 433
Other Development Processes .................................................................... 434
Enough, Already!.................................................................................................. 435
Pick a Process....................................................................................................... 436
In a Nutshell ......................................................................................................... 437
See Also .............................................................................................................. 438
Get Thinking......................................................................................................... 438
Mull It Over ............................................................................................. 438
Getting Personal ...................................................................................... 439
23 THE OUTER LIMITS
The Different Programming Disciplines 441
Applications Programming ..................................................................................... 442
Shrink-Wrap Software .............................................................................. 443
Custom Applications................................................................................. 444
Games Programming............................................................................................. 445
Systems Programming............................................................................................ 446
Embedded Programming........................................................................................ 447
Distributed Programming........................................................................................ 450
Web Application Programming............................................................................... 451
Enterprise Programming ......................................................................................... 453
Numerical Programming ........................................................................................ 454
So What? ............................................................................................................ 455
In a Nutshell ......................................................................................................... 456
See Also .............................................................................................................. 456
Get Thinking......................................................................................................... 457
Mull It Over ............................................................................................. 457
Getting Personal ...................................................................................... 457
24 WHERE NEXT?
All’s Well That Ends Well 459
But What Now?.................................................................................................... 460
ANSWERS AND DISCUSSION 463
Chapter 1: On the Defensive .................................................................................. 463
Mull It Over ............................................................................................. 463
Getting Personal ...................................................................................... 465
Chapter 2: The Best Laid Plans................................................................................ 466
Mull It Over ............................................................................................. 466
Getting Personal ...................................................................................... 471
Chapter 3: What’s in a Name? .............................................................................. 474
Mull It Over ............................................................................................. 474
Getting Personal ...................................................................................... 478
Chapter 4: The Write Stuff ..................................................................................... 480
Mull It Over ............................................................................................. 480
Getting Personal ...................................................................................... 484
Chapter 5: A Passing Comment .............................................................................. 485
Mull It Over ............................................................................................. 485
Getting Personal ...................................................................................... 486
Chapter 6: To Err Is Human .................................................................................... 487
Mull It Over ............................................................................................. 487
Getting Personal ...................................................................................... 490
Chapter 7: The Programmer’s Toolbox..................................................................... 491
Mull It Over ............................................................................................. 491
Getting Personal ...................................................................................... 492
Chapter 8: Testing Times........................................................................................ 494
Mull It Over ............................................................................................. 494
Getting Personal ...................................................................................... 498
Chapter 9: Finding Fault ........................................................................................ 500
Mull It Over ............................................................................................. 500
Getting Personal ...................................................................................... 502
Chapter 10: The Code That Jack Built ...................................................................... 502
Mull It Over ............................................................................................. 502
Getting Personal ...................................................................................... 508
Chapter 11: The Need for Speed............................................................................ 510
Mull It Over ............................................................................................. 510
Getting Personal ...................................................................................... 514
Chapter 12: An Insecurity Complex......................................................................... 515
Mull It Over ............................................................................................. 515
Getting Personal ...................................................................................... 518
Chapter 13: Grand Designs ................................................................................... 519
Mull It Over ............................................................................................. 519
Getting Personal ...................................................................................... 521
Chapter 14: Software Architecture .......................................................................... 522
Mull It Over ............................................................................................. 522
Getting Personal ...................................................................................... 525
Chapter 15: Software Evolution or Software Revolution? ............................................ 527
Mull It Over ............................................................................................. 527
Getting Personal ...................................................................................... 530
Chapter 16: Code Monkeys ................................................................................... 532
Mull It Over ............................................................................................. 532
Chapter 17: Together We Stand............................................................................. 533
Mull It Over ............................................................................................. 533
Getting Personal ...................................................................................... 538
Chapter 18: Practicing Safe Source......................................................................... 539
Mull It Over ............................................................................................. 539
Getting Personal ...................................................................................... 542
Chatper 19: Being Specific .................................................................................... 544
Mull It Over ............................................................................................. 544
Getting Personal ...................................................................................... 546
Chapter 20: A Review to a Kill ............................................................................... 547
Mull It Over ............................................................................................. 547
Getting Personal ...................................................................................... 549
Chapter 21: How Long Is a Piece of String? ............................................................. 550
Mull It Over ............................................................................................. 550
Getting Personal ...................................................................................... 552
Chapter 22: Recipe for a Program .......................................................................... 553
Mull It Over ............................................................................................. 553
Getting Personal ...................................................................................... 556
Chapter 23: The Outer Limits .................................................................................. 557
Mull It Over ............................................................................................. 557
Getting Personal ...................................................................................... 558
BIBLIOGRAPHY 559
INDEX 565