Book Image

Refactoring TypeScript

By : James Hickey
Book Image

Refactoring TypeScript

By: James Hickey

Overview of this book

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.
Table of Contents (11 chapters)

Deceptive Booleans

We've been looking at some of the issues around using primitive types too often.

Primitive types should be the building blocks out of which we create more useful business-oriented concepts/abstractions in our code.

Again, this helps each specific business concept to have all of its logic in one place (which means we can share it and reason about it much more easily), implement more robust error handling, reduce bugs, and so on.

In this section, I want to look at the most common cause of primitive overuse that I've experienced. I see it all the time.

Like many of our code smells, it's deceptively simple at first glance.

Scenario

In our scenario, we're working on a web application that helps clients to sell their used items online.

We've been asked to add some extra rules around the part of our system that authenticates users.

Right now, the system only checks whether a user was successfully authenticated:

const isAuthenticated...