Table of Contents
Preface ix
Part I Introduction
1 Refactoring 3
What Is Refactoring? 4
What Is Refactoring at Scale? 6
Why Should You Care About Refactoring? 8
Benefits of Refactoring 9
Developer Productivity 9
Identifying Bugs 10
Risks of Refactoring 11
Serious Regressions 11
Unearthing Dormant Bugs 12
Scope Creep 12
Unnecessary Complexity 12
When to Refactor 13
Small Scope 13
Code Complexity Actively Hinders Development 13
Shift in Product Requirements 14
Performance 14
Using a New Technology 15
When Not to Refactor 16
For Fun or Out of Boredom 16
Because You Happened to Be Passing By 16
To Making Code More Extendable 18
When You Don't Have Time 18
Our First Refactoring Example 18
Simplifying Conditionals 21
Extracting Magic Numbers 22
Extracting Self-Contained Logic 23
2 How Code Degrades 27
Why Understanding Code Degradation Matters 28
Requirement Shifts 29
Scalability 29
Accessibility 30
Device Compatibility 30
Environmental Changes 31
External Dependencies 32
Unused Code 32
Changes in Product Requirements 33
Tech Debt 35
Working Around Technology Choices 35
Persistent Lack of Organization 39
Moving Too Quickly 40
Applying Our Knowledge 41
Part II Planning
3 Measuring Our Starting State 45
Why Is Measuring the Impact of a Refactor Difficult? 46
Measuring Code Complexity 47
Halstead Metrics 47
Cyclomatic Complexity 50
NPath Complexity 53
Lines of Code 55
Test Coverage Metrics 57
Documentation 60
Formal Documentation 61
Informal Documentation 62
Version Control 64
Commit Messages 64
Commits in Aggregate 65
Reputation 66
Building a Complete Picture 69
4 Drafting a Plan 71
Defining Your End State 72
On the Road 72
At Work 72
Mapping the Shortest Distance 74
On the Road 74
At Work 75
Identifying Strategic Intermediate Milestones 76
On the Road 77
At Work 77
Choosing a Rollout Strategy 80
Dark Mode/Light Mode 81
Smart DNA's Rollout 86
Cleaning Up Artifacts 87
Referencing Metrics in Your Plan 88
Interpolating Goal Metrics to Intermediate Milestones 89
Distinct Milestone Metrics 89
Estimating 90
Sharing Your Plan with Other Teams 91
Transparency 92
Perspective 92
Refined Plan 94
5 Getting Buy-In 97
Why Your Manager Is Not Onboard 98
Managers Aren't Coding 98
Managers Are Evaluated Differently 99
Managers See the Risk 99
Managers Need to Coordinate 100
Strategies for Making a Compelling Argument 101
Using Conversational Devices 102
Building an Alignment Sandwich 104
Relying on Evidence 107
Playing Hardball 108
Buy-In Shapes the Refactor 109
6 Building the Right Team 111
Identifying Different Kinds of Experts 112
Matchmaking 113
Experts of Many Trades 115
Revisiting Active Contributors 115
Biases in Our Expert List 116
Types of Refactoring Teams 117
Owners 117
Proposed Approach 119
Cleanup Crews 120
The Pitch 122
Metrics 123
Generosity 124
Opportunity 124
Bartering 125
Repeat 126
A Few Outcomes 126
Realistic Scenario 127
Worst-Case Scenario 127
Fostering Strong Teams 128
Part III Execution
7 Communication 131
Within Your Team 132
Stand-Ups 133
Weekly Syncs 135
Retrospectives 137
Outside Your Team 138
When Kicking Off the Project 138
During Project Execution 141
Always Iterate 146
8 Strategies for Execution 147
Team Building 147
Pair Programming 148
Keeping Everyone Motivated 150
Keeping a Tally 152
Intermediate Metric Measurements 152
Unearthed Bugs 153
Clean-Up Items 154
Out-of-Scope Items 154
Programming Productively 155
Prototyping 155
Keep Things Small 156
Test, Test, Test 157
Asking the "Stupid" Question 157
Conclusion 158
9 Making the Refactor Stick 159
Fostering Adoption 160
Education 161
Active Education 161
Passive Education 164
Reinforcement 165
Progressive Linting 165
Code Analysis Tools 166
Gates Versus Guardrails 166
Integrating Improvement into the Culture 167
Part IV Case Studies
10 Case Study: Redundant Database Schemas 171
Slack 101 172
Slack Architecture 101 174
Scalability Problems 177
Booting Up the Slack Client 177
File Visibility 178
Mentions 178
Consolidating the Tables 180
Gathering the Scattered Queries 181
Developing a Migration Strategy 183
Quantifying Our Progress 186
Attempting to Keép the Team Motivated 187
Communicating Our Progress 188
Tidying Up 190
Lessons Learned 192
Develop a Well-Defined, Well-Communicated Plan 192
Understand the Code's History 193
Ensure Adequate Test Coverage 194
Keep Your Team Motivated 194
Focus on Strategic Milestones 195
Identify and Rely on Meaningful Metrics 195
Takeaways 196
11 Case Study: Migrating to a New Database 197
Workspace-Sharded Data 198
Migrating channels_members to Vitess 199
Sharding Scheme 200
Developing a New Schema 201
Detangling JOINs 203
A Difficult Rollout 208
Backfill Mode 209
Dark Mode 210
Light Mode 215
Sunset Mode 216
Tidying Up 217
Lessons Learned 219
Set Realistic Estimates 219
Source the Teammates You Need 220
Plan Scope Carefully 220
Choose a Single Place for Project Communication 221
Design a Thoughtful Rollout Plan 221
Takeaways 222
Index 223