Cover....1
Title Page....2
Copyright and Credits....3
Contributors....5
Table of Contents....8
Preface....14
Part 1: Introduction to Refactoring....20
Chapter 1: What is Refactoring?....22
What do we mean by refactoring?....23
Refactoring and clean code....24
Misconceptions about refactoring....24
Why you should consider refactoring....26
Improving the design of your software....27
Maintainability and scalability....27
Understanding, avoiding, and fixing bugs....28
Faster development....28
When you should refactor....29
The “Rule of Three”....29
Preparatory refactoring for smooth feature implementation....29
Refactoring for bug fixing....30
Comprehension refactoring....30
The “Boy Scout Rule”....30
Planned refactoring....31
Long-term refactoring and “Branch by Abstraction”....31
Refactoring in a code review....34
What you should refactor....34
Impact analysis....35
Risk assessment....36
Value estimation....37
Prioritization matrix....37
Further considerations....38
Refactor or rewrite?....39
Summary....40
Further reading....40
Chapter 2: Good Coding Habits....42
Characteristics of good code....43
Good code is readable....43
Good code is reliable....44
Good code is hard to misuse....45
Good code is modular....48
Good code is reusable....54
Clean Code....56
Why Clean Code?....57
Some principles of Clean Code....58
Write SOLID code....60
Single responsibility principle....61
Open-closed principle....62
Liskov substitution principle....64
Interface segregation principle....65
Dependency inversion principle....66
Side effects and mutability....67
The builder pattern....70
Causes of bad code....71
Deadlines....72
The Broken Window theory....73
No code review process....73
Insufficient domain or technical knowledge....74
Summary....74
Further reading....75
Part 2: Essence of Refactoring and Good Code....76
Chapter 3: Code Smells....78
Duplicated code....80
Long methods....81
Replace temporary variables with query methods....82
Parameter object....83
Replace a function or method with a command....83
Decompose conditionals....84
Split loops....85
Large classes....86
Switches....89
Primitive Obsession....91
Middle man....92
Message chains....93
Feature envy methods....94
Divergent change....95
Shotgun Surgery....97
God object....99
Summary....100
Further reading....100
Chapter 4: Testing....102
Why you should test (often)....103
Identifying and fixing defects....103
Ensuring quality and reliability....104
Customer satisfaction....105
Compliance and standards....105
Security....106
Integration and compatibility....107
Confidence and peace of mind....107
Unit testing....108
JUnit framework....110
Integration testing....112
Mockito....113
Contract testing....114
Be safe – checking and improving your test coverage....115
What is test coverage?....115
A Java code coverage tool – JaCoCo....116
Test-driven development....121
Advantages of TDD....123
Summary....124
Further reading....124
Chapter 5: Refactoring Techniques....126
Writing better methods....127
Extract Method....128
Inline Method....129
Extract and inline variables....131
Combining a function into a class....132
Moving features between objects....133
Moving a method or field....133
Moving statements into/from methods....135
Hiding delegates....135
Removing dead code....137
Organizing data....138
Field encapsulation....138
Replacing primitives with objects....141
Replacing type code with subclasses....143
Simplifying conditional logic....144
Returning a special case instead of null....144
Using polymorphism instead of conditions....146
Removing duplicated conditions....147
Guard clauses....148
Simplifying method calls....150
Avoiding side effects....150
Removing setter methods....151
Using generalization....153
Pull up field....153
Push down field....154
Pull up method....155
Push down method....156
Template method....157
Using enums instead of constants....159
Summary....162
Further reading....162
Chapter 6: Metaprogramming....164
What is metaprogramming?....164
Exploring compile-time metaprogramming tools....166
Lombok....166
MapStruct....177
Exploring runtime metaprogramming tools....180
Reflection....180
Weighing the pros and cons of metaprogramming....183
Summary....185
Further reading....185
Chapter 7: Static and Dynamic Analysis....186
What is static analysis?....186
Code errors or bad practices....187
Security....189
Cyclomatic complexity....190
Exception (mis)handling....192
Automated static analysis tools....193
What is dynamic analysis?....196
Debugging....197
Profiling....197
Fuzzing....199
Symbolic execution....201
Taint tracking....203
Summary....205
Further reading....206
Part 3: Further Learning....208
Chapter 8: Crafting Quality Every Day....210
Code versioning....210
Graph structure....211
Local VCSs....214
Centralized VCSs....215
Distributed VCSs....215
Branching strategies....216
Best practices....222
Code formatting and style....223
Indentation....224
Braces and whitespaces....225
Line length and wrapping....225
Comments and documentation....227
Naming conventions....228
Automated tools....229
Code reviews....231
Email pass-arounds....233
Code review automation tools....234
In-person (or remote) teamwork reviews....234
Pair programming....235
Advantages....235
Disadvantages....236
Styles of pair programming....237
Time management....238
Documentation....239
Requirements....239
Architecture documentation....240
Technical documentation....241
Good practices for documentation....247
A hard dose of reality....248
The importance of knowing what you’re doing....249
Summary....250
Further reading....250
Chapter 9: Beyond Code – Mastering Software Architecture....252
What is an architecture?....252
Development....253
Deployment....253
System operation....254
Maintenance....254
Architectural patterns....255
Layered architecture....255
Microservices....257
Event-driven architecture....260
Reactive architecture....263
Monolith to microservices....266
Bad smells in the microservices architecture....269
Shared persistence....269
Shared libraries....271
Direct communication....273
Summary....277
Further reading....277
Index....278
Other Books You May Enjoy....289
Refactoring in Java serves as an indispensable guide to enhancing your codebase's quality and maintainability.
The book begins by helping you get to grips with refactoring fundamentals, including cultivating good coding habits and identifying red flags. You'll explore testing methodologies, essential refactoring techniques, and metaprogramming, as well as designing a good architecture. The chapters clearly explain how to refactor and improve your code using real-world examples and proven techniques. Part two equips you with the ability to recognize code smells, prioritize tasks, and employ automated refactoring tools, testing frameworks, and code analysis tools. You'll discover best practices to ensure efficient code improvement so that you can navigate complexities with ease. In part three, the book focuses on continuous learning, daily practices enhancing coding proficiency, and a holistic view of the architecture. You'll get practical tips to mitigate risks during refactoring, along with guidance on measuring impact to ensure that you become an efficient software craftsperson.
By the end of this book, you'll be able to avoid unproductive programming or architecturing, detect red flags, and propose changes to improve the maintainability of your codebase.
This book is for Java developers, software architects, and technical leads looking for a comprehensive guide to advancing their skills in software design and refactoring. The book is ideal for experienced Java enthusiasts, quality assurance engineers, and codebase maintainers as it provides practical insights, real-world examples, and essential patterns. Development managers who want to foster clean coding practices by using best practices for efficient workflows will also find this book useful.