inside front cover
Quick overview of refactoring patterns
EXTRACT METHOD (P3.2.1)Takes part of one method and extracts it into its own method.
REPLACE TYPE CODE WITH CLASSES (P4.1.3)Transforms an enum into an interface, and the enums values become classes.
PUSH CODE INTO CLASSES (P4.1.5)Is a natural continuation of REPLACE TYPE CODE WITH CLASSES (P4.1.3), as it moves functionality into classes.
INLINE METHOD (P4.1.7)Removes methods that no longer add readability to our program.
SPECIALIZE METHOD (P4.2.2)Removes unnecessary and problematic generality from methods.
TRY DELETE THEN COMPILE (P4.5.1)Removes unused methods from interfaces and classes when we know their entire scope.
UNIFY SIMILAR CLASSES (P5.1.1)Unifies two or more classes that differ from each other in a set of constant methods.
COMBINE if
S (P5.2.1)Reduces duplication by joining consecutive if
s that have identical bodies.
INTRODUCE STRATEGY PATTERN (P5.4.2)Replaces variance through if
by instead instantiating classes.
EXTRACT INTERFACE FROM IMPLEMENTATION (P5.4.4)Replaces dependencies on a class with an interface.
ELIMINATE GETTER OR SETTER (P6.1.3)Eliminates getters and setters by moving the functionality closer to the data.
ENCAPSULATE DATA (P6.2.3)Localizes invariants related to variables and makes cohesion clearer.
ENFORCE SEQUENCE (P6.4.1)Makes the compiler guarantee things happen in a specific order.
Five Lines of Code
How and when to refactor
Christian Clausen
Foreword by Robert C. Martin
To comment go to liveBook
Manning
Shelter Island
For more information on this and other Manning titles go to
www.manning.com
Copyright
For online information and ordering of these and other Manning books, please visit www.manning.com. The publisher offers discounts on these books when ordered in quantity.
For more information, please contact
Special Sales Department
Manning Publications Co.
20 Baldwin Road
PO Box 761
Shelter Island, NY 11964
Email: orders@manning.com
2021 by Manning Publications Co. All rights reserved.
No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps.
Recognizing the importance of preserving what has been written, it is Mannings policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine.
| Manning Publications Co. 20 Baldwin Road Technical PO Box 761 Shelter Island, NY 11964 |
Development editor: | Helen Stergius |
Technical development editor: | Mark Elston |
Review editor: | Mihaela Batini |
Production editor: | Keri Hales |
Copy editor: | Tiffany Taylor |
Proofreader: | Katie Tennant |
Technical proofreader: | Jean Franois Morin |
Typesetter: | Dennis Dalinnik |
Cover designer: | Marija Tudor |
ISBN: 9781617298318
dedication
To my university mentors, who told me
The key to being consistently brilliant is hard work every day.
Olivier Danvy
and
Youre missing the point.
Mayer Goldberg
Thank you for teaching me to stop trying to do the right thing, and do the right thing.
front matter
foreword
Have you ever read a book on software and thought that the author was talking over your head? Did the book use unfamiliar vocabulary and overly complex concepts to make its points? Did it make you feel as though it was written for some elite inner circle of know-it-alls that didnt include you?
This is not that book. This book is down to earth, focused, and right on point.
Neither is this book a primer. It doesnt start at the atom and bore you with the basics of programming and languages. It doesnt try to coddle you and keep you safe. I guarantee that this book will challenge you. But it will challenge you without intimidating you and without insulting your intelligence.
Refactoring is the discipline of transforming bad code into good code without breaking it. When we consider that our entire civilization now depends on software for its further existence, it seems unlikely that there is a topic more worthy of study.
Perhaps you think thats hyperbolic. Its not. Look around you. How many processors running software are currently on your body? Your watch, your phone, your car keys, your headphones... how many are within 30 meters of you? Your microwave, your stove, your dishwasher, your thermostat, your clothes washer... and how about your car?
These days, nothing happens in our society without software. You cant buy or sell anything, or drive or fly anywhere, or cook a hot dog, or watch TV, or call someone on the phone without software.
And how much of that software is actually good code? Think of the systems you are working on right now. Are they clean? Or are they, like most, a mess in desperate need of refactoring?
This book does not present the kind of sterile and simplistic refactoring you may have heard or read about before. This book talks about real refactoring. Refactoring in real projects. Refactoring in legacy systems. Refactoring in the kinds of environments that we all face virtually every day.
Whats more, this book wont make you feel guilty for not having automated tests. The author realizes that most inherited systems grew and evolved over time, and we are not so fortunate as to have such test suites.
This book lays down a set of simple rules that you can follow to reliably refactor complex, messy, tangled, untested systems. By learning and following these rules, you can make a real difference in the quality of the systems you maintain.
Dont get me wrongits not a silver bullet. Refactoring old, crufty, untested code is never easy. But armed with the rules and examples in this book, you will be able to make inroads against the cruft and tangle of systems that have bedeviled you for too long.
So I advise you to read this book carefully. Study the examples. Think hard about the abstractions and intentions the author presents. Get the codebase he offers, and refactor it along with him. Follow his refactoring journey from beginning to end.