+ All Categories
Home > Documents > CodeComplete, Second Edition - CERN · Viii Tableof Contents Part V CodeImprovements 20...

CodeComplete, Second Edition - CERN · Viii Tableof Contents Part V CodeImprovements 20...

Date post: 14-Sep-2019
Category:
Upload: others
View: 1 times
Download: 0 times
Share this document with a friend
12
Code Complete, Second Edition Steve McConnell
Transcript
Page 1: CodeComplete, Second Edition - CERN · Viii Tableof Contents Part V CodeImprovements 20 TheSoftware-Quality Landscape. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Code Complete, Second Edition

Steve McConnell

Page 2: CodeComplete, Second Edition - CERN · Viii Tableof Contents Part V CodeImprovements 20 TheSoftware-Quality Landscape. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Part 1

Laying ~ha Foundatbaan1

Welcome to Software Construction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .32

Metaphors for a Richer Understanding of Software Development . . . . . 93

Measure Twice, Cut Once: Upstream Prerequisites . . . . . . . . . . . . . . . . . 234

Key Construction Decisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61

Part 11

Creating High-Quality Code

Part III VaF6a leS10 General Issues in Using Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23711 The Power of Variable Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25912 Fundamental Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29113 Unusual Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319

Part IV Statements14 Organizing Straight-Line Code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34715 Using Conditionals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35516 Controlling Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36717 Unusual Control Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39118 Table-Driven Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41119 General Control Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431

5 Design in Construction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 736 Working Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1257 High-Quality Routines. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1618 Defensive Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1879 The Pseudocode Programming Process . . . . . . . . . . . . . . . . . . . . . . . . . 215

Page 3: CodeComplete, Second Edition - CERN · Viii Tableof Contents Part V CodeImprovements 20 TheSoftware-Quality Landscape. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Viii

Table of Contents

Part V Code Improvements20

The Software-Quality Landscape . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463

21

Collaborative Construction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479

22

Developer Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49923

Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53524

Refactoring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56325

Code-Tuning Strategies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58726

Code-Tuning Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 609

Part VI System Considerations27

How Program Size Affects Construction . . . . . . . . . . . . . . . . . . . . . . . . 64928

Managing Construction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66129

Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68930

Programming Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 709

Part VII Software Craftsmanship31

Layout and Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72932

Self-Documenting Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77733

Personal Character . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81934

Themes in Software Craftsmanship . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83735

Where to Find More Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 855

Page 4: CodeComplete, Second Edition - CERN · Viii Tableof Contents Part V CodeImprovements 20 TheSoftware-Quality Landscape. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Table of ContentsPreface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xixAcknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxviiList of Checklists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxixList of Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxxi

List of Figures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxxiii

Part I

Laying the Foundation1

Welcome to Software Construction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

1.1 What Is Software Construction? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

1.2 Why Is Software Construction Important? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61.3 How to Read This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

2

Metaphors for a Richer Understanding of Software Development . . . . .9

2 .1 The Importance of Metaphors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

2.2 How to Use Software Metaphors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

2.3 Common Software Metaphors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

3

Measure Twice, Cut Once: Upstream Prerequisites . . . . . . . . . . . . . . . . . 23

3.1 Importance of Prerequisites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243.2 Determine the Kind of Software You're Working On . . . . . . . . . . . . . . . . . . . . . . . . 313.3 Problem-Definition Prerequisite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363.4 Requirements Prerequisite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38

3.5 Architecture Prerequisite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .43

3.6 Amount of Time to Spend on Upstream Prerequisites . . . . . . . . . . . . . . . . . . . . . . 55

4

Key Construction Decisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61

4.1 Choice of Programming Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 614.2 Programming Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 664.3 Your Location on the Technology Wave . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 664.4 Selection of Major Construction Practices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69

u think of, t

Ippok?

Microsoft is interested in hearing yourfeedback about this publication so we cancontinually improve our books and learning resourcesfor you.To participate in a brief

k

'

I

GfMC?I91'idQ'ldl

online survey, pleasevisit:wwwmicrosoftcomlleaminglbooksurveyl

Page 5: CodeComplete, Second Edition - CERN · Viii Tableof Contents Part V CodeImprovements 20 TheSoftware-Quality Landscape. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

x

Table of Contents

Part II

Creating Mgh-QuaNy Code

5

Design in Construction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 735 .1 Design Challenges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 745.2 Key Design Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 775.3 Design Building Blocks : Heuristics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 875.4 Design Practices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1105.5 Comments on Popular Methodologies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118

6

Working Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1256.1 Class Foundations: Abstract Data Types (ADTs) . . . . . . . . . . . . . . . . . . . . . . . . . . . 1266.2 Good Class Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1336.3 Design and Implementation Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1436.4 Reasons to Create a Class . . . . . . . . . . . . . . . . �������� . . �� . . . � . � 1526.5 Language-Specific Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1566.6 Beyond Classes: Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156

7

High-Quality Routines . . . . . . . . . . . . . . . . ��� , . � . ��� , . �� , 1617.1 Valid Reasons to Create a Routine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1647.2 Design at the Routine Level . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1687.3 Good Routine Names . . . . . . . . . ��� . � . ��� . . . . . . . . . . . . . . . . . ��� , 1717.4 How Long Can a Routine Be? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1737.5 How to Use Routine Parameters . . . . . . . . �� . �� . � , . � . � , . . . . . . ��� 1747.6 Special Considerations in the Use of Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 1817.7 Macro Routines and Inline Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182

8

Defensive Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1878.1 Protecting Your Program from Invalid Inputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1888.2 Assertions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

1898.3 Error-Handling Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . .

. .

. . . . 1948.4 Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

1988.5 Barricade Your Program to Contain the Damage Caused by .Errors . . . . . . . . . . 2038.6 Debugging Aids . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2058.7 Determining How Much Defensive Programmingto LeaveinProduction Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2098.8 Being Defensive About Defensive Programming . . . . . . . . . . . . . . . . . . . . . . . . . . 210

Page 6: CodeComplete, Second Edition - CERN · Viii Tableof Contents Part V CodeImprovements 20 TheSoftware-Quality Landscape. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

9

The Pseudocode Programming Process . . . . . . . . . . . . . . . . . . . . . . . . . 2159.1 Summary of Steps in Building Classes and Routines . . . . . . . . . . . . . . . . . . . . . . . 2169.2 Pseudocode for Pros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2189.3 Constructing Routines by Using the PPP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2209.4 Alternatives to the PPP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232

Part III Variables

Table of Contents

xi

10

General Issues in Using Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23710.1 Data Literacy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .23810.2 Making Variable Declarations Easy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23910.3 Guidelines for Initializing Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24010.4 Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .24410.5 Persistence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .25110.6 Binding Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25210 .7 Relationship Between Data Types and Control Structures . . . . . . . . . . . . . . . . . 25410.8 Using Each Variable for Exactly One Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255

11

The Power of Variable Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25911.1 Considerations in Choosing Good Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25911.2 Naming Specific Types of Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26411.3 The Power of Naming Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27011.4 Informal Naming Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27211.5 Standardized Prefixes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27911.6 Creating Short Names That Are Readable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28211.7 Kinds of Names to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285

12

Fundamental Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29112.1 Numbers in General . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29212.2 Integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .293

12.3 Floating-Point Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29512.4 Characters and Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29712.5 Boolean Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30112.6 Enumerated Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30312.7 Named Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30712.8 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .310

12.9 Creating Your Own Types (Type Aliasing) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311

Page 7: CodeComplete, Second Edition - CERN · Viii Tableof Contents Part V CodeImprovements 20 TheSoftware-Quality Landscape. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

xii

Table of Contents

13

Unusual Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31913 .1 Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .31913.2 Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .32313.3 Global Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .335

Part IV Statements

14

Organizing Straight-Line Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34714.1 Statements That Must Be in a Specific Order . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34714.2 Statements Whose Order Doesn't Matter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351

15

Using Conditionals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35515.1 if Statements

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35515.2 case Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361

16

Controlling Loops . . . . . . . . . . ������� , . . . . . . . . ����� 36716.1 Selecting the Kind of Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36716.2 Controlling the Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37316.3 Creating Loops Easily-From the Inside Out . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38516.4 Correspondence Between Loops and Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387

17

Unusual Control Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39117.1 Multiple Returns from a Routine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39117.2 Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39317.3 goto . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .39817.4 Perspective on Unusual Control Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408

18

Table-Driven Methods. . . . . . . ������ . . . . . . . ������� 41118.1 General Considerations in Using Table-Driven Methods . . . . . . . . . . . . . . . . . . 41118.2 Direct Access Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41318.3 Indexed Access Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42518.4 Stair-Step Access Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42618.5 Other Examples of Table Lookups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429

19

General Control Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43119.1 Boolean Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43119.2 Compound Statements (Blocks) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443

Page 8: CodeComplete, Second Edition - CERN · Viii Tableof Contents Part V CodeImprovements 20 TheSoftware-Quality Landscape. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

19.3 Null Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44419.4 Taming Dangerously Deep Nesting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44519.5 A Programming Foundation : Structured Programming . . . . . . . . . . . . . . . . . . . 45419.6 Control Structures and Complexity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456

Part V Code Improvements

Table of Contents

xiii

20

The Software-Quality Landscape . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46320.1 Characteristics of Software Quality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46320.2 Techniques for Improving Software Quality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46620.3 Relative Effectiveness of Quality Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46920.4 When to Do Quality Assurance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47320.5 The General Principle of Software Quality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474

21

Collaborative Construction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47921.1 Overview of Collaborative Development Practices . . . . . . . . . . . . . . . . . . . . . . . 48021.2 Pair Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48321.3 Formal Inspections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .48521.4 Other Kinds of Collaborative Development Practices . . . . . . . . . . . . . . . . . . . . . 492

22

Developer Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49922.1 Role of Developer Testing in Software Quality . . . . . . . . . . . . . . . . . . . . . . . . . . . 50022.2 Recommended Approach to Developer Testing . . . . . . . . . . . . . . . . . . . . . . . . . 50322.3 Bag of Testing Tricks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50522.4 Typical Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .517

22.5 Test-Support Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52322.6 Improving Your Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52822 .7 Keeping Test Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 529

23

Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53523 .1 Overview of Debugging Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53523.2 Finding a Defect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54023 .3 Fixing a Defect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 550

23 .4 Psychological Considerations in Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 554

23 .5 Debugging Tools-Obvious and Not-So-Obvious . . . . . . . . . . . . . . . . . . . . . . . . 556

Page 9: CodeComplete, Second Edition - CERN · Viii Tableof Contents Part V CodeImprovements 20 TheSoftware-Quality Landscape. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

xiv

Table of Contents

24

Refactoring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56324.1 Kinds of Software Evolution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56424.2 Introduction to Refactoring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56524.3 Specific Refactorings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57124.4 Refactoring Safely . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57924.5 Refactoring Strategies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 582

25

Code-Tuning Strategies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58725.1 Performance Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58825.2 Introduction to Code Tuning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59125.3 Kinds of Fat and Molasses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59725.4 Measurement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60325.5 Iteration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .60525.6 Summary of the Approach to Code Tuning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 606

26

Code-Tuning Techniques . . . . . . . . ��������� . . ���� , 60926.1 Logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .61026.2 Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61626.3 Data Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62426.4 Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63026.5 Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .63926.6 Recoding in a Low-Level Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64026.7 The More Things Change, the More They Stay the Same . . . . . . . . . . . . . . . . . 643

Part VI System Considerations

27

How Program Size Affects Construction . . . . . . . . . . . . . . . . . . . . . . . . 64927.1 Communication and Size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65027.2 Range of Project Sizes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65127.3 Effect of Project Size on Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65127.4 Effect of Project Size on Productivity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65327.5 Effect of Project Size on Development Activities . . . . . . . . . . . . . . . . . . . . . . . . . 654

Page 10: CodeComplete, Second Edition - CERN · Viii Tableof Contents Part V CodeImprovements 20 TheSoftware-Quality Landscape. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Table of Contents

xv

28

Managing Construction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 661

28.1 Encouraging Good Coding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66228.2 Configuration Management. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66428.3 Estimating a Construction Schedule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67128.4 Measurement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .67728.5 Treating Programmers as People . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 680

28.6 Managing Your Manager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 686

29

Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 689

29.1 Importance of the Integration Approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68929.2 Integration Frequency-Phased or Incremental? . . . . . . . . . . . . . . . . . . . . . . . . . 691

29.3 Incremental Integration Strategies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 694

29.4 Daily Build and Smoke Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 702

30

Programming Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 709

30.1 Design Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .71030.2 Source-Code Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71030.3 Executable-Code Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 716

30.4 Tool-Oriented Environments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72030.5 Building Your Own Programming Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 721

30.6 Tool Fantasyland . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .722

Part VII Software Craftsmanship

31

Layout and Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 729

31.1 Layout Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 730

31.2 Layout Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .73631.3 Layout Styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .73831.4 Laying Out Control Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 745

31.5 Laying Out Individual Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 753

31.6 Laying Out Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 763

31.7 Laying Out Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 766

31.8 Laying Out Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 768

Page 11: CodeComplete, Second Edition - CERN · Viii Tableof Contents Part V CodeImprovements 20 TheSoftware-Quality Landscape. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

xvi

Table of Contents

32

Self-Documenting Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77732 .1 External Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77732.2 Programming Style as Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77832.3 To Comment or Not to Comment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78132.4 Keys to Effective Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78532.5 Commenting Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79232 .6 IEEE Standards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 813

33

Personal Character . . . . . . . . . ��� . ���� , . . . . . . . . . � , . �� , 81933.1 Isn't Personal Character Off the Topic? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82033.2 Intelligence and Humility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82133 .3 Curiosity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82233 .4 Intellectual Honesty . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82633.5 Communication and Cooperation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82833 .6 Creativity and Discipline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82933 .7 Laziness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83033.8 Characteristics That Don't Matter As Much As You Might Think . . . . . . . . . . . 83033.9 Habits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 833

34

Themes in Software Craftsmanship . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83734.1 Conquer Complexity. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83734.2 Pick Your Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83934.3 Write Programs for People First, Computers Second . . . . . . . . . . . . . . . . . . . . . 84134.4 Program into Your Language, Not in It . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84334.5 Focus Your Attention with the Help of Conventions . . . . . . . . . . . . . . . . . . . . . . 84434.6 Program in Terms of the Problem Domain. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84534.7 Watch for Falling Rocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84834.8 Iterate, Repeatedly, Again and Again . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85034.9 Thou Shalt Rend Software and Religion Asunder . . . . . . . . . . . . . . . . . . . . . . . . 851

Page 12: CodeComplete, Second Edition - CERN · Viii Tableof Contents Part V CodeImprovements 20 TheSoftware-Quality Landscape. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Table of Contents

xvii

35

Where to Find More Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85535.1 Information About Software Construction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85635.2 Topics Beyond Construction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85735.3 Periodicals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .85935.4 A Software Developer's Reading Plan . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86035.5 Joining a Professional Organization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 862

Bibliography. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 863

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 885

What do, you thio* of this book?,

Microsoft is interested in hearing your feedback about this publication so we cant 1

herfrom

~;1;

continually improve our books and learning resources for you. To participate in a brief-

-

-

W

online survey, please visit wwwmicrosof :comlleominglbooksurveyl


Recommended