||= operator, 257
Account class, 129
Introduce Parameter Object refactoring, 322-324
Move Field refactoring, 174-175
Move Method refactoring, 170-172
Remove Setting Method refactoring, 325-326
Replace Error Code with Exception refactoring, 334-335
AccountNumberCapture module, 360-362
ActiveDeal class, 403
add_charge method, 322-323
add_course method, 223
add_customer method, 213
add_front_suspension method, 247
add_option method, 117
add_order method, 213
Add Parameter
overview, 300
step-by-step description, 301-302
when to use, 300-301
add_rear_suspension method, 247
adjusted_capital method, 278-279
advantages of refactoring
easier-to-understand software, 55-56
faster programming, 56-57
improved software design, 54-55
why refactoring works, 60-61
algorithms, substituting
goals, 132
overview, 131-132
step-by-step description, 132
alternative classes with difference interfaces, 83
Ambler, Scott, 65
a_method method, 125
amount calculation (video store program), moving, 12-18
amount_for method, moving, 12-18
APIs, disjointed, 86
apply method, 388
assert_equal method, 89
assertions, adding
example, 294-295
goals, 293
overview, 292
step-by-step description, 293-294
AssertValidKeys module, 146-147
attributes, 255-259
base_charge method, 309
base_price method, 111
Beck, Kent, 51, 54, 56, 69, 73
behavior, moving into classes, 223-224
benefits of refactoring
easier-to-understand software, 55-56
faster programming, 56-57
improved software design, 54-55
why refactoring works, 60-61
Bid class, Extract Module refactoring, 360-361
bidirectional association, changing to unidirectional
example, 215-217
goals, 214
overview, 213
step-by-step description, 214-215
bidirectional association, changing unidirectional association to
example, 211-213
goals, 210-211
overview, 210
step-by-step description, 211
Billing Scheme class, 414-416
Books class, 145-146
bugs
finding by refactoring, 56
refactoring when fixing bugs, 58
calculate_outstanding method, 107
case statement, 80
replacing with polymorphism (video store program example), 32-49
chains, replacing temps with
example, 115-117
goals, 115
overview, 114
step-by-step description, 115
Change Bidirectional Association to Unidirectional refactoring, 83
example, 215-217
overview, 213
step-by-step description, 214-215
when to use, 214
Change Reference to Value refactoring
example, 199-201
overview, 198
step-by-step description, 199
when to use, 198-199
Change Unidirectional Association to Bidirectional refactoring
example, 211-213
overview, 210
step-by-step description, 211
when to use, 210-211
Change Value to Reference refactoring
example, 196-198
overview, 194
step-by-step description, 195
when to use, 195
changing
bidirectional association to unidirectional
example, 215-217
goals, 214
overview, 213
step-by-step description, 214-215
divergent change, 77
interfaces, 63-64
reference objects to value objects
example, 199-201
goals, 198-199
overview, 198
step-by-step description, 199
unidirectional association to bidirectional
example, 211-213
goals, 210-211
overview, 210
step-by-step description, 211
value objects to reference objects
example, 196-198
goals, 195
overview, 194
step-by-step description, 195
Charge class, 322
check_security method, 271-273, 305
Chrysler Comprehensive Compensation case study, 69-72
class annotations, adding
examples, 141-142
goals, 140
overview, 139-140
step-by-step description, 140-141
classes. See also specific classes
alternative classes with difference interfaces, 83
data classes, 84
delegate classes
calling directly with Remove Middle Man, 185-186
hiding with Hide Delegate, 181-184
extracting. See extracting
inappropriate intimacy, 83
incomplete library classes, 84
large classes, eliminating, 76
lazy classes, 81
merging hierarchy, 371-372
merging modules into, 362
moving behavior into, 223-224
moving into another class with Inline Class
example, 180-181
overview, 179
step-by-step description, 180
when to use, 179
order, 128
replacing records with, 224
replacing with modules
example, 393-395
overview, 392
step-by-step description, 393
when to use, 392
special case classes, 292
Code Complete: A Practical Handbook of Software Construction (McConnel), 71
code reviews, refactoring with, 58-59
alternative classes with difference interfaces, 83
case statements, 80
comments, 85
data classes, 84
data clumps, 79
disjointed APIs, 86
divergent change, 77
duplicated code, 74
feature envy, 78-79
inappropriate intimacy, 83
incomplete library classes, 84
large classes, 76
lazy classes, 81
long methods, 74-76
long parameter lists, 76-77
message chains, 82
metaprogrammming, 86
middle man, 83
overview, 73-74
parallel inheritance hierarchies, 81
primitives, 79-80
refused bequests, 84-85
repetitive boilerplate, 86
shotgun surgery, 78
speculative generality, 81
temporary fields, 82
Collapse Hierarchy, 81
overview, 371
step-by-step description, 372
when to use, 371
collection closure methods, replacing loops with
example, 133-135
goals, 133
step-by-step description, 133
collections, encapsulating
example, 220-223
goals, 219
overview, 219
step-by-step description, 219-220
CommandCenter class, 163
comments, 85
communication, telling managers about refactoring, 61
Company class
Expression Builder, adding, 348-352
Introduce Gateway refactoring, 343-346
compute method, 130-131
Concurrent Programming in Java (Lea), 297
assertions
example, 294-295
goals, 293
overview, 292
step-by-step description, 293-294
consolidating
examples, 266-267
goals, 265-266
overview, 265
step-by-step description, 266
consolidating duplicate conditional fragments
example, 269
goals, 268
overview, 268
step-by-step description, 268-269
control flags, removing
examples, 271-274
goals, 269-270
overview, 269
step-by-step description, 270-271
decomposing
example, 263
goals, 262
overview, 261
step-by-step description, 262
nested conditionals, replacing with guard clauses
examples, 276-279
goals, 275-276
overview, 274-275
step-by-step description, 276
null objects, adding
examples, 288-291
goals, 285-287
overview, 284
special cases, 292
step-by-step description, 287-288
recomposing
examples, 264-265
goals, 264
overview, 264
replacing with polymorphism
example, 282-284
goals, 280-281
overview, 279
step-by-step description, 281
conditional logic, removing, 225-226
Consolidate Conditional Expression refactoring
examples, 266-267
overview, 265
step-by-step description, 266
when to use, 265-266
Consolidate Duplicate Conditional Fragments refactoring
example, 269
overview, 268
step-by-step description, 268-269
when to use, 268
consolidating
conditional expressions
examples, 266-267
goals, 265-266
overview, 265
step-by-step description, 266
duplicate conditional fragments
example, 269
goals, 268
overview, 268
step-by-step description, 268-269
constants, replacing magic numbers with
goals, 218
overview, 217
step-by-step description, 218
constructors, replacing with factory methods
example, 330-332
goals, 329
overview, 328-329
step-by-step description, 329
control flags, removing
examples, 271-274
goals, 269-270
overview, 269
step-by-step description, 270-271
controller classes, separating domain logic from
example, 408-412
goal of, 406-407
overview, 406
step-by-step description, 407-408
converting procedural design to objects
example, 406
goal of, 405
overview, 405
step-by-step description, 406
Convert Procedural Design to Objects
example, 406
overview, 405
step-by-step description, 406
when to use, 405
count_descendants_matching method, 138-139
Course class, 220
Cunningham, Ward, 51
Currency class, 199
Customer class, 3, 196-198, 212, 215-216, 375
CustomInitializers module, 141, 144
databases, problems with refactoring, 64-65
data classes, 84
data clumps, eliminating, 79
changing bidirectional association to unidirectional
example, 215-217
goals, 214
overview, 213
step-by-step description, 214-215
changing reference objects into value objects
example, 199-201
goals, 198-199
overview, 198
step-by-step description, 199
changing unidirectional association to bidirectional
example, 211-213
goals, 210-211
overview, 210
step-by-step description, 211
changing value objects into reference objects
example, 196-198
goals, 195
overview, 194
step-by-step description, 195
eagerly initialized attributes, 257-259
encapsulating collections
example, 220-223
goals, 219
overview, 219
step-by-step description, 219-220
lazily initialized attributes, 255-257
moving behavior into classes, 223-224
overview, 187-188
replacing arrays with objects
example, 202-206
goals, 201
overview, 201
step-by-step description, 202
replacing data values with objects
example, 192-194
goals, 191
overview, 191
step-by-step description, 192
replacing hashes with objects
example, 207-209
goals, 206
overview, 206
step-by-step description, 207
replacing magic numbers with symbolic constants
goals, 218
overview, 217
step-by-step description, 218
replacing records with data classes, 224
replacing subclasses with fields
example, 253-255
goals, 252
overview, 251
step-by-step description, 252-253
replacing type code with module extensions
example, 234-238
goals, 233
overview, 232
step-by-step description, 233
replacing type code with polymorphism
example, 227-232
goals, 225
overview, 225
removing conditional logic, 225-226
step-by-step description, 226-227
replacing type code with state/strategy
example, 240-251
goals, 239
overview, 239
step-by-step description, 239-240
self-encapsulating fields
example, 189-191
goals, 188-189
overview, 188
step-by-step description, 189
data values, replacing with objects
example, 192-194
goals, 191
overview, 191
step-by-step description, 192
@days_overdrawn instance variable, 170
Deal class, Tease Apart Inheritance refactoring, 401-404
Decompose Conditional refactoring
example, 263
overview, 261
step-by-step description, 262
when to use, 262
decomposing conditional expressions
example, 263
goals, 262
overview, 261
step-by-step description, 262
Decorator class, 159
def_each method, 154-155
definition of refactoring, 52-54
delegate classes
calling directly with Remove Middle Man
example, 186
overview, 185
step-by-step description, 185
when to use, 185
hiding with Hide Delegate
example, 183-184
overview, 181
step-by-step description, 183
when to use, 182-183
delegation
replacing inheritance with
example, 387-389
goals of, 386
overview, 386
step-by-step description, 386-387
replacing with hierarchy
example, 390-392
goals, 390
overview, 389
step-by-step description, 390
deprecation, refactoring with, 205-206
design
design changes that are difficult to refactor, 65-66
improving with refactoring, 54-55
relationship with refactoring, 67-68
developer tests, 91-92
development of refactoring, 51-52
disability_amount method, 265-266
discount_factor method, 114
discount method, 124-125
disjointed APIs, 86
distance_traveled method, 123
divergent change, 77
domain logic, separating from view
example, 408-412
goal of, 406-407
overview, 406
step-by-step description, 407-408
duplicated code, 74
duplicate methods, eliminating with Extract Surrounding Method
example, 137-139
overview, 135-136
step-by-step description, 136-137
when to use, 136
Dynamic Method Definition refactoring
examples, 153-157
overview, 152
step-by-step description, 153
when to use, 153
examples, 153-157
goals, 153
overview, 152
replacing dynamic receptors with
examples, 158-160
goals, 158
overview, 158
step-by-step description, 158
step-by-step description, 153
dynamic receptors
isolating, 160
example, 162-165
goals, 161
step-by-step description, 162
replacing with dynamic method definitions, 158-160
Eagerly Initialized Attribute refactoring, 257-259
Employee class
assertions, 294-295
Replace Delegation with Hierarchy refactoring, 390-391
Encapsulate Collection refactoring, 84
example, 220-223
overview, 219
step-by-step description, 219-220
when to use, 219
encapsulating collections
example, 220-223
goals, 219
overview, 219
step-by-step description, 219-220
eql? method, 200
error codes, replacing with exceptions
examples, 334-33
goals, 333
overview, 332
step-by-step description, 333-334
eval, moving from runtime to parse time, 165-166
exceptions
replacing error codes with
examples, 334-337
goals, 333
overview, 332
step-by-step description, 333-334
replacing with tests
example, 338-341
goals, 338
overview, 337
step-by-step description, 338
expense_limit method, 292
explaining variables, adding
examples, 119-121
goals, 118
overview, 117-118
step-by-step description, 119
Expression Builders, adding
example, 348-352
goals, 347
overview, 346
step-by-step description, 347
expressions. See conditional expressions
changing data clumps into objects, 79
eliminating large classes, 76
example, 177-179
organizing orphan variables, 82
overview, 175
step-by-step description, 176-177
when to use, 175-176
Extract Hierarchy refactoring
example, 414-416
overview, 412
step-by-step description, 413-414
when to use, 413
classes
changing data clumps into objects, 79
eliminating large classes, 76
example, 177-179
organizing orphan variables, 82
overview, 175
step-by-step description, 176-177
when to use, 175-176
frequent renter points (video store program), 18-21
hierarchy of classes
example, 414-416
goals, 413
overview, 412
step-by-step description, 413-414
methods
goals, 102
methods with local variables, 105-106
methods with no local variables, 104-105
overview, 102
reassigning local variables, 106-108
step-by-step description, 103
modules
example, 360-362
goals, 358-359
overview, 357-358
step-by-step description, 359
subclasses
example, 364-367
goals, 363
overview, 363
step-by-step description, 363-364
surrounding methods
example, 137-139
overview, 135-136
step-by-step description, 136-137
when to use, 136
Extract Method refactoring
eliminating duplicated code, 74
methods with local variables, 105-106
methods with no local variables, 104-105
overview, 102
reassigning local variables, 106-108
removing duplication, 86
shortening long methods, 75
step-by-step description, 103
video store program example, 10
when to use, 102
example, 360-362
overview, 357-358
step-by-step description, 359
when to use, 358-359
Extract Subclass refactoring
example, 364-367
overview, 363
step-by-step description, 363-364
when to use, 363
Extract Surrounding Method refactoring
eliminating duplicated code, 74
example, 137-139
overview, 135-136
step-by-step description, 136-137
when to use, 136
eXtreme Programming eXplained (Beck), 51
failure method, 152-153
feature envy, 78-79
fields
encapsulating fields
example, 189-191
goals, 188-189
overview, 188
step-by-step description, 189
moving with Move Field
example, 174-175
overview, 172
step-by-step description, 173
when to use, 173
replacing subclasses with
goals, 252
overview, 251
step-by-step description, 252-255
temporary fields, 82
File class, writing tests for, 89-91
FileTest class, 89
finding references, 99
Foo class, 205
format of refactorings, 97-98
Form Template Method refactoring
elmininating duplicated code, 74
overview, 372
step-by-step description, 374
template method with extension of modules (example), 380-385
template method with inheritance (example), 374-379
when to use, 373
found_friends method, 131-132
found_miscreant method, 273-274, 305-306
found_person method, 305
Fowler, Martin, 69
frequent_renter_points method, 47
frequent_renter_points temporary variable, 19, 22
frequent renter points (video store program), extracting, 18-21
FrontSuspensionMountainBike class, 228-231, 236-237, 245, 369-371
FullSuspensionMountainBike class, 228-231, 237, 284
Gamma class, 130
Gateways, adding
example, 342-346
goals, 341-342
overview, 341
step-by-step description, 342
GemStone, 71
generalization refactorings
Collapse Hierarchy
overview, 371
step-by-step description, 372
when to use, 371
Extract Module
example, 360-362
overview, 357-358
step-by-step description, 359
when to use, 358-359
Extract Subclass
example, 364-367
overview, 363
step-by-step description, 363-364
when to use, 363
Form Template Method
overview, 372
step-by-step description, 374
template method with extension of modules (example), 380-385
template method with inheritance (example), 374-379
when to use, 373
Inline Module, 362
Introduce Inheritance
example, 369-371
overview, 368
step-by-step description, 369
when to use, 368
Pull Down Method
overview, 356
step-by-step description, 357
when to use, 357
Pull Up Method
example, 355-356
overview, 353
step-by-step description, 355
when to use, 354
Replace Abstract Superclass with Module
example, 393-395
overview, 392
step-by-step description, 393
when to use, 392
Replace Delegation with Hierarchy
example, 390-392
overview, 389
step-by-step description, 390
when to use, 390
Replace Inheritance with Delegation
example, 387-389
overview, 386
step-by-step description, 386-387
when to use, 386
goals, setting, 418
Hash class, 157
Haungs, Jim, 71
HeatingPlan class, 315-317
Hide Delegate refactoring, 82
example, 183-184
overview, 181
step-by-step description, 183
when to use, 182-183
Hide Method refactoring
overview, 327
step-by-step description, 328
when to use, 327
hiding
delegates
example, 183-184
overview, 181
step-by-step description, 183
when to use, 182-183
methods
goals, 327
overview, 327
step-by-step description, 328
hierarchy
hierarchy of classes, creating
example, 414-416
goals, 413
overview, 412
step-by-step description, 413-414
replacing delegation with
example, 390-392
goals, 390
overview, 389
step-by-step description, 390
history of refactoring, 51-52
HtmlStatement class, 376-378, 381-382
html_statement method, 30, 375
immutable objects, 199
importance of big refactorings, 398
ImportedItem class, 190
inappropriate intimacy, 83
incomplete library classes, 84
indirection and refactoring, 61-63
inheritance, 374-379
adding to code
example, 369-371
goals, 368
overview, 368
step-by-step description, 369
refactoring
examples, 401-404
goal of, 400
overview, 399
step-by-step description, 400-401
parallel inheritance hierarchies, 81
replacing with delegation
example, 387-389
goals of, 386
overview, 386
step-by-step description, 386-387
video store program example, 36-49
initialize_courses method, 222
initialize method, 130, 190, 254
initializing attributes, 255-259
Inline Class refactoring, 81
example, 180-181
organizing changes into single class, 78
overview, 179
removing unnecessary delegation, 81
step-by-step description, 180
when to use, 179
Inline Method refactoring
overview, 108-109
step-by-step description, 109-110
when to use, 109
inline methods
goals, 109
overview, 108-109
step-by-step description, 109-110
Inline Module refactoring, 81, 362
Inline Temp refactoring, 110
inline temps, 110
instance_variable_defined? method, 257
@interest_rate field, moving, 174
interfaces, changing, 63-64
Introduce Assertion refactoring, 85
example, 294-295
overview, 292
step-by-step description, 293-294
when to use, 293
Introduce Class Annotation refactoring, 86
examples, 141-142
overview, 139-140
step-by-step description, 140-141
when to use, 140
Introduce Explaining Variable refactoring
examples, 119-121
overview, 117-118
step-by-step description, 119
when to use, 118
Introduce Expression Builder refactoring, 86
example, 348-352
overview, 346
step-by-step description, 347
when to use, 347
Introduce Gateway refactoring, 86
example, 342-346
overview, 341
step-by-step description, 342
when to use, 341-342
Introduce Inheritance refactoring
example, 369-371
overview, 368
step-by-step description, 369
when to use, 368
Introduce Named Parameter refactoring
examples, 143-147
overview, 142
shortening long parameter lists, 77
step-by-step description, 143
when to use, 142-143
Introduce Null Object refactoring, 82
examples, 288-291
overview, 284
special cases, 292
step-by-step description, 287-288
when to use, 285-287
Introduce Parameter Object refactoring
example, 321-324
reducing parameter lists, 79
shortening long parameter lists, 77
step-by-step description, 321
when to use, 320
Isolate Dynamic Receptor refactoring
example, 162-165
overview, 160
step-by-step description, 162
when to use, 161
Item class, 189-190
JobItem class, extracting LaborItem subclass from, 364-367
Johnson, Ralph, 51
Join class, 394
joins_for_table method, 394
Knuth, Donald, 166
LaborItem class, extracting from JobItem class, 364-367
Laptop class, 346-352
large classes, eliminating, 76
large refactorings, tips for, 420
Lazily Initialized Attributes, 255-257
lazy classes, 81
Lea, Doug, 297
learning refactoring, 418-419
Ledger class, 126
libraries, incomplete library classes, 84
local variables
example, 105-106
reassigning, 106-108
long methods, shortening, 74-76
long parameter lists, shortening, 76-77
loops, replacing with collection closure methods, 133-135
magic numbers, replacing with symbolic constants
goals, 218
overview, 217
step-by-step description, 218
managers, telling about refactoring, 61
measuring performance, Chrysler Comprehensive Compensation case study, 69-72
merging
class hierarchy, 371-372
modules into including class, 362
message chains, 82
MessageCollector class, 164
metaprogramming, 86
method_missing method, 156, 160
methods
add_charge, 322-323
add_course, 223
add_customer, 213
add_front_suspension, 247
add_option, 117
add_order, 213
add_rear_suspension, 247
adjusted_capital, 278-279
a_method, 125
amount_for, 12-18
apply, 388
assert_equal, 89
base_charge, 309
base_price, 111
calculate_outstanding, 107
class annotations, adding
examples, 141-142
goals, 140
overview, 139-140
step-by-step description, 140-141
collection closure methods, replacing loops with
example, 133-135
goals, 133
step-by-step description, 133
compute, 130-131
constructors, replacing with factory methods, 328-332
count_descendants_matching, 138-139
creating
goals, 373
overview, 372
step-by-step description, 374
template method with extension of modules (example), 380-385
template method with inheritance (example), 374-379
def_each, 154-155
defining dynamically
examples, 153-157
goals, 153
overview, 152
replacing dynamic receptors with dynamic method definition, 158-160
step-by-step description, 153
disability_amount, 265-266
discount, 124-125
discount_factor, 114
distance_traveled, 123
duplicate methods, eliminating, 135-139
eql?, 200
expense_limit, 292
Expression Builders, adding
example, 348-352
goals, 347
overview, 346
step-by-step description, 347
extracting
goals, 102
methods with local variables, 105-106
methods with no local variables, 104-105
overview, 102
reassigning local variables, 106-108
step-by-step description, 103
failure, 152-153
found_friends, 131-132
found_miscreant, 273-274, 305-306
found_person, 305
frequent_renter_points, 47
Gateways, adding
example, 342-346
goals, 341-342
overview, 341
step-by-step description, 342
hiding
goals, 327
overview, 327
step-by-step description, 328
initialize_courses, 222
inline methods
goals, 109
overview, 108-109
step-by-step description, 109-110
instance_variable_defined?, 257
isolating dynamic receptors, 160
example, 162-165
goals, 161
step-by-step description, 162
joins_for_table, 394
long methods, shortening, 74-76
MountainBike, 250
moving to subclasses
goals of, 357
overview, 356
step-by-step description, 357
moving up to superclass
example, 355-356
goals, 354
overview, 353
step-by-step description, 355
moving with Move Method
example, 170-172
overview, 167
step-by-step description, 168-170
when to use, 168
not_summer, 263
number_of_descendants_named, 138-139
number_of_living_descendants, 138-139
office_telephone_number, 300
overdraft_charge, 170-171
parameters
adding, 300-302
named parameters, 142-147
named parameters, removing, 147-150
parameterized methods, creating, 307-310
parameter objects, creating, 320-324
removing, 302-303
removing assignments to, 124-127
replacing with explicit methods, 310-313
replacing with methods, 317-320
unused default parameters, removing, 150-152
pay_amount, 275-277
price, 112-121, 228, 247, 319-320
price_code, 48
print_owing, 104-108
product_count_items, 150-151
remove_customer, 213
remove_order, 213
removing
example, 325-327
goals, 325
overview, 324
step-by-step description, 325
renaming, 298
example, 299-300
goals, 298-299
step-by-step description, 299
replacing with method objects
example, 129-131
goals, 128
overview, 127
step-by-step description, 129
resource, 339-341
reward_points, 264
RigidMountainBike, 250
send_alert_if_miscreant_in, 306
separating query from modifier
concurrency issues, 307
example, 305-307
goals, 304
overview, 303
step-by-step description, 304-305
code listing after refactoring, 9
code listing before refactoring, 7-8
extracting frequent renter points, 18-21
initial code listing, 3-4
moving amount calculation, 12-18
removing temporary variables, 22-31
renaming variables, 10-11
substituting algorithms
goals, 132
overview, 131-132
step-by-step description, 132
summer_charge, 263
telephone_number, renaming, 299
temporary variables
explaining variables, 117-121
inline temps, 110
replacing with chains, 114-117
replacing with queries, 111-114
splitting, 121-124
total_amount_for_order_lines, 411
total_charge, 23-25
triple, 126
usage_in_range, 309
winter_charge, 263
withdraw, 336
middle man classes, removing, 83, 185-186
MissingCustomer class, 289
Module class, 205
module extensions, replacing type code with
example, 234-238
goals, 233
overview, 232
step-by-step description, 233
modules
AccountNumberCapture, 360-362
AssertValidKeys, 146-147
CustomInitializers, 141-144
extracting
example, 360-362
goals, 358-359
overview, 357-358
step-by-step description, 359
merging into including class, 362
Person, 391
replacing superclasses with
example, 393-395
overview, 392
step-by-step description, 393
when to use, 392
MonthlyStatement class, 385
MountainBike class, 227-229, 234-236, 240-248, 280-283, 369
MountainBike method, 250
Move Eval from Runtime to Parse Time refactoring, 165-166
example, 174-175
organizing changes into single class, 78
overview, 172
reducing inappropriate intimacy, 83
step-by-step description, 173
when to use, 173
Move Method refactoring, 13
example, 170-172
moving behaviors into data classes, 84
organizing changes into single class, 78
overview, 167
reducing inappropriate intimacy, 83
step-by-step description, 168-170
when to use, 168
Movie class, 2
moving
amount calculation (video store program), 12-18
behavior into classes, 223-224
fields. See Move Field refactoring
methods. See Move Method refactoring
methods to subclasses
goals of, 357
overview, 356
step-by-step description, 357
methods up to superclass
example, 355-356
goals, 354
overview, 353
step-by-step description, 355
named parameters
adding
examples, 143-147
goals, 142-143
overview, 142
step-by-step description, 143
example, 148-150
goals, 148
overview, 147
step-by-step description, 148
nature of refactoring, 397-398
nested conditionals, replacing with guard clauses
examples, 276-279
goals, 275-276
overview, 274-275
step-by-step description, 276
NetworkResult class, 208-210
not_summer method, 263
NullCustomer class, 290-291
null objects, adding
examples, 288-291
goals, 285-288
overview, 284
special cases, 292
number_of_descendants_named method, 138-139
number_of_living_descendants method, 138-139
objects
converting procedural design to
example, 406
goal of, 405
overview, 405
step-by-step description, 406
method objects, replacing methods with, 127-131
null objects, adding
examples, 288-291
goals, 285-287
overview, 284
special cases, 292
step-by-step description, 287-288
parameter objects, creating, 320-324
preserving whole objects
example, 315-317
goals, 313-314
overview, 313
step-by-step description, 314-315
reference objects
changing into value objects, 198-201
changing value objects into, 194-198
replacing arrays with
example, 202-205
goals, 201
overview, 201
step-by-step description, 202
replacing data values with
example, 192-194
goals, 191
overview, 191
step-by-step description, 192
replacing hashes with
example, 207-209
goals, 206
overview, 206
step-by-step description, 207
replacing with arrays, 206
value objects
changing reference objects into, 198-201
changing to reference objects, 194-198
office_telephone_number method, 300
off_road_ability method, 228, 244
Opdyke, Bill, 52
Order class, 128, 192-193, 196, 212-216, 411
OrdersController class, 409-410
organizing data. See data organization
orphan variables, organizing, 82
overdraft_charge method, 170-171
parallel inheritance hierarchies, 81
parameterized methods, creating
example, 308-310
goals, 308
overview, 307
step-by-step description, 308
Parameterize Method refactoring
example, 308-310
overview, 307
step-by-step description, 308
when to use, 308
parameter lists, shortening, 76-77
parameters
adding to methods
goals, 300-301
overview, 300
step-by-step description, 301-302
named parameters
adding, 142-147
removing, 147-150
parameter objects, creating, 320-324
removing assignments to
example, 125-127
goals, 124-125
step-by-step description, 125
removing from methods
goals, 302
overview, 302
step-by-step description, 302-303
replacing with explicit methods
example, 311-313
goals, 310-311
overview, 310
step-by-step description, 311
replacing with methods
example, 318-320
goals, 318
overview, 317
step-by-step description, 318
unused default parameters, removing
example, 151-152
goals, 151
overview, 150
step-by-step description, 151
partners, value of, 419
pay_amount method, 275-277
performance
effect of refactoring on, 70-71
measuring, Chrysler Comprehensive Compensation case study, 69-72
Performance class, 203-204
Person class, 165, 220-222, 253-254
Expression Builder, adding, 348-352
Extract Class refactoring, 177-179
Hide Delegate refactoring, 183-184
Remove Middle Man refactoring, 186
Person module, 391
Policy class, 387-389
polymorphism
replacing conditional logic with
example, 282-284
goals, 280-281
overview, 279
step-by-step description, 281
video store program example, 32-49
replacing type code with
example, 227-232
goals, 225
overview, 225
removing conditional logic, 225-226
step-by-step description, 226-227
PostData class, 156
The Pragmatic Programmer (Thomas), 87
Preserve Whole Object refactoring
example, 315-317
overview, 313
reducing parameter lists, 79
shortening long parameter lists, 77
step-by-step description, 314-315
when to use, 313-314
price_code method, 48
price method, 112-121, 228, 247, 319-320
primitives, 79-80
print_owing method, 104-108
problems with refactoring
databases, 64-65
design changes, 65-66
interface changes, 63-64
when not to refactor, 66-67
procedural design, converting to objects
example, 406
goal of, 405
overview, 405
step-by-step description, 406
Product class, 127
ProductController class, 330-332
product_count_items method, 150-151
programs. See video store program
Pull Down Method refactoring
overview, 356
step-by-step description, 357
when to use, 357
Pull Up Method refactoring
example, 355-356
overview, 353
step-by-step description, 355
when to use, 354
Push Down Method refactoring, 85
QA (quality assurance) tests, 91-92
queries
replacing temps with
example, 112-114
goals, 111-112
step-by-step description, 112
separating from modifiers
concurrency issues, 307
example, 305-307
goals, 304
overview, 303
step-by-step description, 304-305
reassigning local variables, 106-108
Recompose Conditional refactoring
examples, 264-265
overview, 264
when to use, 264
recomposing conditional expressions
examples, 264-265
goals, 264
overview, 264
Recorder class, 162-164
records, replacing with data classes, 224
Red/Green/Refactor movement, 87-88
Refactoring Databases (Sadalage and Ambler), 65
refactorings. See specific refactorings
refactoring tips
learning refactoring, 418-419
overview, 417-418
tips for large refactorings, 420
working with a partner, 419
reference objects
changing into value objects
example, 199-201
goals, 198-199
overview, 198
step-by-step description, 199
changing value objects into
example, 196-198
goals, 195
overview, 194
step-by-step description, 195
references, finding, 99
refused bequests, 84-85
Remove Assignments to Parameters refactoring
example, 125-127
overview, 124
step-by-step description, 125
when to use, 124-125
Remove Control Flag refactoring
examples, 271-274
overview, 269
step-by-step description, 270-271
when to use, 269-270
remove_customer method, 213
Remove Middle Man refactoring, 83
example, 186
overview, 185
step-by-step description, 185
when to use, 185
Remove Named Parameter refactoring
example, 148-150
overview, 147
step-by-step description, 148
when to use, 148
remove_order method, 213
Remove Parameter refactoring
overview, 302
step-by-step description, 302-303
when to use, 302
Remove Setting Method refactoring, 84
example, 325-327
overview, 324
step-by-step description, 325
when to use, 325
Remove Unused Default Parameter refactoring
example, 151-152
overview, 150
step-by-step description, 151
when to use, 151
removing
assignments to parameters
example, 125-127
goals, 124-125
step-by-step description, 125
code smells. See code smells
control flags
examples, 271-274
goals, 269-270
overview, 269
step-by-step description, 270-271
middle man, 83
named parameters
example, 148-150
goals, 148
overview, 147
step-by-step description, 148
repetitive boilerplate, 86
setting methods
example, 325-327
goals, 325
overview, 324
step-by-step description, 325
temporary variables, 22-31
unused default parameters
example, 151-152
goals, 151
overview, 150
step-by-step description, 151
Rename Method refactoring
example, 299-300
step-by-step description, 299
when to use, 298-299
renaming
methods
example, 299-300
goals, 298-299
step-by-step description, 299
variables, 10-11
Rental class, 2-3
repetitive boilerplate, removing, 86
Replace Abstract Superclass with Module refactoring
example, 393-395
overview, 392
step-by-step description, 393
when to use, 392
Replace Array with Object refactoring, 80
example, 202-206
overview, 201
step-by-step description, 202
when to use, 201
Replace Conditional with Polymorphism refactoring
example, 282-284
overview, 279
step-by-step description, 281
when to use, 280-281
Replace Constructor with Factory Method refactoring
example, 330-332
overview, 328-329
step-by-step description, 329
when to use, 329
Replace Data Value with Object refactoring, 80
example, 192-194
overview, 191
step-by-step description, 192
when to use, 191
Replace Delegation with Hierarchy refactoring, 83
example, 390-392
overview, 389
step-by-step description, 390
when to use, 390
Replace Dynamic Receptor with Dynamic Method Definition refactoring, 86
examples, 158-160
overview, 158
step-by-step description, 158
when to use, 158
Replace Error Code with Exception refactoring
examples, 334-337
overview, 332
step-by-step description, 333-334
when to use, 333
Replace Exception with Test refactoring
example, 338-341
overview, 337
step-by-step description, 338
when to use, 338
Replace Hash with Object refactoring
example, 207-209
overview, 206
step-by-step description, 207
when to use, 206
Replace Inheritance with Delegation refactoring, 85
example, 387-389
overview, 386
step-by-step description, 386-387
when to use, 386
Replace Loop with Collection Closure Method refactoring
example, 133-135
overview, 133
step-by-step description, 133
when to use, 133
Replace Magic Number with Symbolic Constant refactoring
overview, 217
step-by-step description, 218
when to use, 218
Replace Method with Method Object refactoring
example, 129-131
overview, 127
shortening long methods, 75
step-by-step description, 129
when to use, 128
Replace Nested Conditional with Guard Clauses refactoring
examples, 276-279
overview, 274-275
step-by-step description, 276
when to use, 275-276
Replace Parameter with Explicit Methods refactoring, 80
example, 311-313
overview, 310
step-by-step description, 311
when to use, 310-311
Replace Parameter with Method refactoring
example, 318-320
overview, 317
shortening long parameter lists, 77
step-by-step description, 318
when to use, 318
Replace Record with Data Class refactoring, 224
Replace Subclass with Fields refactoring
example, 253-255
overview, 251
step-by-step description, 252-253
when to use, 252
Replace Temp with Chain refactoring
example, 115-117
overview, 114
when to use, 115
Replace Temp with Query refactoring
example, 112-114
overview, 111
step-by-step description, 112
when to use, 111-112
Replace Type Code with Module Extension refactoring, 80
example, 234-238
overview, 232
step-by-step description, 233
when to use, 233
Replace Type Code with Polymorphism refactoring, 80
example, 227-232
overview, 225
removing conditional logic, 225-226
step-by-step description, 226-227
when to use, 225
Replace Type Code with State/Strategy refactoring, 80
example, 240-251
overview, 239
step-by-step description, 239-240
video game program example, 38-45
when to use, 239
replacing
algorithms
goals, 132
overview, 131-132
step-by-step description, 132
arrays with objects
example, 202-206
goals, 201
overview, 201
step-by-step description, 202
conditional logic with polymorphism
example, 282-284
goals, 280-281
overview, 279
step-by-step description, 281
video store program example, 32-49
constructors with factory methods
example, 330-332
goals, 329
overview, 328-329
step-by-step description, 329
data values with objects
example, 192-194
goals, 191
overview, 191
step-by-step description, 192
delegation with hierarchy
example, 390-392
goals, 390
overview, 389
step-by-step description, 390
dynamic receptors with dynamic method definitions
examples, 158-160
goals, 158
overview, 158
step-by-step description, 158
hashes with objects
example, 207-209
goals, 206
overview, 206
step-by-step description, 207
inheritance with delegation
example, 387-389
goals of, 386
overview, 386
step-by-step description, 386-387
loops with collection closure methods
example, 133-135
goals, 133
step-by-step description, 133
magic numbers with symbolic constants
goals, 218
overview, 217
step-by-step description, 218
methods with method objects
example, 129-131
goals, 128
overview, 127
step-by-step description, 129
nested conditionals with guard clauses
examples, 276-279
goals, 275-276
overview, 274-275
step-by-step description, 276
parameters with methods
example, 318-320
goals, 318
overview, 317
step-by-step description, 318
records with data classes, 224
subclasses with fields
example, 253-255
goals, 252
overview, 251
step-by-step description, 252-253
temps with chains
example, 115-117
goals, 115
overview, 114
step-by-step description, 115
example, 112-114
goals, 111-112
step-by-step description, 112
type code with module extensions
example, 234-238
goals, 233
overview, 232
step-by-step description, 233
type code with polymorphism
example, 227-232
goals, 225
overview, 225
removing conditional logic, 225-226
step-by-step description, 226-227
type code with state/strategy
example, 240-251
goals, 239
overview, 239
step-by-step description, 239-240
resource method, 339-341
ResourceStack class, 338-339
reward_points method, 264
RigidMountainBike class, 228-231, 243-244, 247, 282-283
RigidMountainBike method, 250
Room class, 315
Rule of Three, 57
Sadalage, Pramod, 65
SearchCriteria class, 141-144
Select class, 115-117
Self-Delegation pattern, 78
Self Encapsulate Field refactoring, 174
example, 189-191
overview, 188
step-by-step description, 189
when to use, 188-189
self-testing code, 87-88
send_alert_if_miscreant_in method, 306
Separate Domain from Presentation refactoring
example, 408-412
overview, 406
step-by-step description, 407-408
when to use, 406-407
Separate Query from Modifier refactoring
concurrency issues, 307
example, 305-307
overview, 303
step-by-step description, 304-305
when to use, 304
separating domain logic from view
example, 408-412
goal of, 406-407
overview, 406
step-by-step description, 407-408
shortening
methods, 74-76
parameter lists, 76-77
shotgun surgery, 78
Smalltalk, 51
smells. See code smells
software design, improving with refactoring, 54-55
special case class, 292
speculative generality, 81
Split Temporary Variable refactoring
example, 122-124
overview, 121-122
step-by-step description, 122
when to use, 122
splitting temporary variables
example, 122-124
goals, 122
overview, 121-122
step-by-step description, 122
Statement class, 379
code listing after refactoring, 9
code listing before refactoring, 7-8
extracting frequent renter points, 18-21
initial code listing, 3-4
moving amount calculation, 12-18
removing temporary variables, 22-31
renaming variables, 10-11
state/strategy, replacing type code with
example, 240-251
goals, 239
overview, 239
step-by-step description, 239-240
subclasses
extracting
example, 364-367
goals, 363
overview, 363
step-by-step description, 363-364
moving methods into
goals of, 357
overview, 356
step-by-step description, 357
replacing with fields
example, 253-255
goals, 252
overview, 251
step-by-step description, 252-253
Substitute Algorithm refactoring
overview, 131-132
step-by-step description, 132
when to use, 132
substituting algorithms
goals, 132
overview, 131-132
step-by-step description, 132
summer_charge method, 263
superclasses, replacing with modules
example, 393-395
overview, 392
step-by-step description, 393
when to use, 392
symbolic constants, replacing magic numbers with
goals, 218
overview, 217
step-by-step description, 218
tangled inheritance, refactoring
examples, 401-404
goal of, 400
overview, 399
step-by-step description, 400-401
Tease Apart Inheritance refactoring
examples, 401-404
overview, 399
step-by-step description, 400-401
when to use, 400
TelephoneNumber class
defining, 177-178
Inline Class refactoring example, 180-181
telephone_number method, renaming, 299
telling managers about refactoring, 61
template methods, creating
goals, 373
overview, 372
step-by-step description, 374
template method with extension of modules (example), 380-385
template method with inheritance (example), 374-379
temporary fields, 82
temporary variables
explaining variables, adding
examples, 119-121
goals, 118
overview, 117-118
step-by-step description, 119
frequent_renter_points, 19, 22
inline temps, 110
replacing with chains
example, 115-117
goals, 115
overview, 114
step-by-step description, 115
replacing with queries
example, 112-114
goals, 111-112
overview, 111
step-by-step description, 112
splitting
example, 122-124
goals, 122
overview, 121-122
step-by-step description, 122
total_amount, 22-23
TestCase class, 89
Test::Unit testing framework, 88-91
testing
developer tests, 91-92
importance of, 9-10
QA (quality assurance) tests, 91-92
self-testing code, 87-88
Test::Unit testing framework, 88-91
video store program, 6-7
writing tests, 92-95
tests, replacing exceptions with
example, 338-341
goals, 338
overview, 337
step-by-step description, 338
TextStatement class, 376-377, 381
Thomas, Dave, 87
tips for refactoring
learning refactoring, 418-419
overview, 417-418
tips for large refactorings, 420
working with a partner, 419
total_amount_for_order_lines method, 411
total_amount temporary variable, 22-23
total_charge method, 23-25
triple method, 126
troubleshooting refactoring
databases, 64-65
design changes, 65-66
interface changes, 63-64
when not to refactor, 66-67
two hat metaphor, 54
type code
replacing with module extensions
example, 234-238
goals, 233
overview, 232
step-by-step description, 233
replacing with polymorphism
example, 227-232
goals, 225
overview, 225
removing conditional logic, 225-226
step-by-step description, 226-227
replacing with state/strategy
example, 240-251
goals, 239
overview, 239
step-by-step description, 239-240
UML (Unified Modeling Language) diagrams, 20-21
unidirectional association
changing bidirectional to
example, 215-217
goals, 214
overview, 213
step-by-step description, 214-215
changing to bidirectional
example, 211-213
goals, 210-211
overview, 210
step-by-step description, 211
Unified Modeling Language (UML) diagrams, 20-21
unused default parameters, removing
example, 151-152
goals, 151
overview, 150
step-by-step description, 151
usage_in_range method, 309
value objects
changing reference objects into, 198-201
changing to reference objects, 194-198
variables
@days_overdrawn, 170
local variables
example, 105-106
reassigning, 106-108
orphan variables, organizing, 82
renaming, 10-11
temporary variables
explaining variables, adding, 117-121
frequent_renter_points, 19, 22
inline temps, 110
removing, 22-31
replacing with chains, 114-117
replacing with queries, 111-114
splitting, 121-124
total_amount, 22-23
Customer class, 3
design issues, 5-6
frequent_renter_points method, 47
html_statement method, 30
inheritance, 36-49
Movie class, 2
overview, 1-2
price_code method, 48
Rental class, 3
replacing conditional logic with polymorphism, 32-49
statement method
code listing after refactoring, 9
code listing before refactoring, 7-8
extracting frequent renter points, 18-21
initial code listing, 3-4
moving amount calculation, 12-18
removing temporary variables, 22-31
renaming variables, 10-11
testing, 6-7
total_charge method, 23-25
Unified Modeling Language (UML) diagrams, 20-21
views, separating domain logic from
example, 408-412
goal of, 406-407
overview, 406
step-by-step description, 407-408
when not to refactor, 66-67
when to refactor
for greater understanding, 59-60
overview, 57
Rule of Three, 57
when adding function, 57-58
when fixing bugs, 58
with code reviews, 58-59
why refactoring works, 60-61
winter_charge method, 263
withdraw method, 336
writing tests, 92-95