
Refactoring TypeScript Keeping your code healthy James Hickey Refactoring TypeScript Copyright © 2019 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. Author: James Hickey Managing Editor: Aritro Ghosh Acquisitions Editor: Karan Wadekar Production Editor: Salma Patel Editorial Board: Shubhopriya Banerjee, Bharat Botle, Ewan Buckingham, Megan Carlisle, Simon Cox, Mahesh Dhyani, Manasa Kumar, Alex Mazonowicz, Dominic Pereira, Shiny Poojary, Abhishek Rane, Erol Staveley, Ankita Thakur, and Jonathan Wray First Published: October 2019 Production Reference: 1181019 ISBN: 978-1-83921-804-0 Published by Packt Publishing Ltd. Livery Place, 35 Livery Street Birmingham B3 2PB, UK Table of Contents Preface i Chapter 1: Introduction 1 About TypeScript .................................................................................................... 1 What Is It? ............................................................................................................. 1 What's All the Fuss About? .................................................................................. 2 TypeScript versus JavaScript .............................................................................. 2 Why I Chose TypeScript for This Book ............................................................... 4 What Is Refactoring? ............................................................................................. 4 Let's Defne It ........................................................................................................ 4 Our Approach ....................................................................................................... 5 Design Patterns .................................................................................................... 5 Why Refactor at All? .............................................................................................. 5 Slow Development ............................................................................................... 5 Saving Money ....................................................................................................... 6 Navy SEALS Get It ................................................................................................. 6 Being a Craftsman ............................................................................................... 7 Case Study #1 ....................................................................................................... 7 Case Study #2 ....................................................................................................... 8 When Should I Refactor? ...................................................................................... 9 The Boy Scout Rule .............................................................................................. 9 Repetitive Work .................................................................................................... 9 Difculty Adding Features .................................................................................. 9 In the End ............................................................................................................ 10 Chapter 2: Null Checks Everywhere! 13 Identifcation ......................................................................................................... 13 Billion-Dollar Mistake ........................................................................................ 13 Example ............................................................................................................... 14 Is It That Bad? ..................................................................................................... 15 Non-Nullable Types ........................................................................................... 15 Null Object Pattern .............................................................................................. 16 Empty Collections .............................................................................................. 16 Fixing It Up .......................................................................................................... 16 Take 2 .................................................................................................................. 17 What About Objects? ......................................................................................... 17 Special Case Pattern ........................................................................................... 19 Situation .............................................................................................................. 19 An Order Class That Needs to Be Refactored ................................................. 19 Refactoring the Order Class ............................................................................. 20 Chapter 3: Wordy Conditionals 23 Identifcation ......................................................................................................... 23 A Little Bit of This, a Little Bit of That .............................................................. 23 Misbehaving Conditionals ................................................................................ 24 Combining Conditionals ..................................................................................... 24 Situation .............................................................................................................. 24 The Code ............................................................................................................. 25 Guideline ............................................................................................................. 25 Extracting Methods from Conditionals .......................................................... 26 What's Wrong Here? .......................................................................................... 26 The Fix ................................................................................................................. 26 Extracting Conditional Logic to Explicit Classes ........................................... 27 Let's Get Classy ................................................................................................... 27 SRP ....................................................................................................................... 28 Using It ................................................................................................................ 28 Pipe Classes ........................................................................................................... 29 Your Classes Might Be Doing Too Much... ....................................................... 29 Piping Our Logic ................................................................................................. 29 Bonus Refactor ................................................................................................... 31 Chapter 4: Nested Conditionals 33 Identifcation ......................................................................................................... 33 A Monster ........................................................................................................... 33 A Closer Look ...................................................................................................... 34 Guard Clauses ....................................................................................................... 34 Scenario ............................................................................................................... 34 Fail Fast ............................................................................................................... 34 Fixing It Up .......................................................................................................... 35 Gate Classes ........................................................................................................... 36 Scenario ............................................................................................................... 37 Gate Classes to the Rescue ............................................................................... 37 Useful for Web APIs ........................................................................................... 38 Chapter 5: Primitive Overuse 41 Identifcation ......................................................................................................... 41 What's Wrong Here? .......................................................................................... 42 Value Objects .......................................................................................................
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages15 Page
-
File Size-