Discover various techniques to develop maintainable code and keep it in shape. Key Features Learn all about refactoring - why it is important and how to do it Discover easy ways to refactor code with examples Explore techniques that can be applied to most other programming languages Book Description Refactoring improves your code without changing its behavior. With refactoring, the best approach is to apply small targeted changes to a codebase. Instead of doing a huge sweeping change to your code, refactoring is better as a long-term and continuous enterprise. Refactoring TypeScript explains how to spot bugs and remove them from your code. You'll start by seeing how wordy conditionals, methods, and null checks make code unhealthy and unstable. Whether it is identifying messy nested conditionals or removing unnecessary methods, this book will show various techniques to avoid these pitfalls and write code that is easier to understand, maintain, and test. By the end of the book, you'll have learned some of the main causes of unhealthy code, tips to identify them and techniques to address them. What you will learn Spot and fix common code smells to create code that is easier to read and understand Discover ways to identify long methods and refactor them Create objects that keep your code flexible, maintainable, and testable Apply the Single Responsibility Principle to develop less-coupled code Discover how to combine different refactoring techniques Learn ways to solve the issues caused by overusing primitives Who this book is for This book is designed for programmers who are looking to explore various refactoring techniques to develop healthy and maintainable code. Some experience in JavaScript and TypeScript can help you easily grasp the concepts explained in this book. Table of Contents Introduction Null Checks Everywhere! Wordy Conditionals Nested Conditionals Primitive Overuse Lengthy Method Signatures Methods That Never End Dumping Grounds Messy Object Creation Conclusion Cover FM Copyright Table of Contents Preface Chapter 1: Introduction About TypeScript What Is It? What's All the Fuss About? TypeScript versus JavaScript Why I Chose TypeScript for This Book What Is Refactoring? Let's Define It Our Approach Design Patterns Why Refactor at All? Slow Development Saving Money Navy SEALS Get It Being a Craftsman Case Study #1 Case Study #2 When Should I Refactor? The Boy Scout Rule Repetitive Work Difficulty Adding Features In the End Chapter 2: Null Checks Everywhere! Identification Billion-Dollar Mistake Example Is It That Bad? Non-Nullable Types Null Object Pattern Empty Collections Fixing It Up Take 2 What About Objects? Special Case Pattern Situation An Order Class That Needs to Be Refactored Refactoring the Order Class Chapter 3: Wordy Conditionals Identification A Little Bit of This, a Little Bit of That Misbehaving Conditionals Combining Conditionals Situation The Code Guideline Extracting Methods from Conditionals What's Wrong Here? The Fix Extracting Conditional Logic to Explicit Classes Let's Get Classy SRP Using It Pipe Classes Your Classes Might Be Doing Too Much... Piping Our Logic Bonus Refactor Chapter 4: Nested Conditionals Identification A Monster A Closer Look Guard Clauses Scenario Fail Fast Fixing It Up Gate Classes Scenario Gate Classes to the Rescue Useful for Web APIs Chapter 5: Primitive Overuse Identification What's Wrong Here? Value Objects Scenario Initial Refactor Addressing Primitives Creating Our Object Moving Along Immediate Validation Deceptive Booleans Scenario New Business Rules More Rules... Is It Really That Bad? Just Use Enums Strategy Pattern Chapter 6: Lengthy Method Signatures Identification The Slippery Slope of Optional Parameters The Issue Guidelines Creating a Reusable Private Method A Simple Example Creating Semantically Meaningful Methods A Brief Look at Some Advanced Solutions Extracting Data Objects Extraction Chapter 7: Methods That Never End Identification When Is a Method Too Long? Give It a Name Being Strategic Chapter 8: Dumping Grounds Identification Object-Oriented? The Great Debate Coupling Warning Sign One of These Things Is Not Like The Others You Have Mail Breaking It Up Keep Separate Things Separate Speak or Listen CQRS Read and Write Next Steps Commands Queries Conclusion A Word of Caution Chapter 9: Messy Object Creation Identification Factory Functions Functions versus Static Methods Combining Refactoring Techniques Complexity Remains... Building It The Builder Pattern One Last Improvement Chapter 10: Conclusion This Is Just The Beginning! Index Discover various techniques to develop maintainable code and keep it in shape. Key Features* Learn all about refactoring - why it is important and how to do it * Discover easy ways to refactor code with examples * Explore techniques that can be applied to most other programming languages Book DescriptionRefactoring improves your code without changing its behavior. With refactoring, the best approach is to apply small targeted changes to a codebase. Instead of doing a huge sweeping change to your code, refactoring is better as a long-term and continuous enterprise. Refactoring TypeScript explains how to spot bugs and remove them from your code. You'll start by seeing how wordy conditionals, methods, and null checks make code unhealthy and unstable. Whether it is identifying messy nested conditionals or removing unnecessary methods, this book will show various techniques to avoid these pitfalls and write code that is easier to understand, maintain, and test. By the end of the book, you'll have learned some of the main causes of unhealthy code, tips to identify them and techniques to address them. What you will learn* Spot and fix common code smells to create code that is easier to read and understand * Discover ways to identify long methods and refactor them * Create objects that keep your code flexible, maintainable, and testable * Apply the Single Responsibility Principle to develop less-coupled code * Discover how to combine different refactoring techniques * Learn ways to solve the issues caused by overusing primitives Who this book is forThis book is designed for programmers who are looking to explore various refactoring techniques to develop healthy and maintainable code. Some experience in JavaScript and TypeScript can help you easily grasp the concepts explained in this book. Table of Contents1. Introduction 2. Null Checks Everywhere! 3. Wordy Conditionals 4. Nested Conditionals 5. Primitive Overuse 6. Lengthy Method Signatures 7. Methods That Never End 8. Dumping Grounds 9. Messy Object Creation 10. Conclusion Refactoring TypeScript is an approachable look at getting your code into shape. Learn some of the causes of unhealthy code, how to identify them, techniques to cure them, and more.