Refactoring JavaScript. Turning Bad Code into Good Code
398 pages
English
PDF
4.5

Refactoring JavaScript. Turning Bad Code into Good Code

Refactoring JavaScript. Turning Bad Code into Good Code

Author:

Year:

Summary:

Introduction

The book "Refactoring JavaScript: Turning Bad Code into Good Code" (2017) by Evan Burchard is a practical guide that teaches developers how to transform low-quality, tangled JavaScript code into clean, maintainable, and efficient code. Unlike classic refactoring books (e.g., by Martin Fowler), this work is entirely focused on the specifics of the JavaScript language, including asynchrony, prototypal inheritance, functional style, and the Node.js ecosystem. The book spans 398 pages and contains numerous code examples that can be immediately applied in practice.

What the book is about

The book consists of three parts. The first part introduces the basics of refactoring: what it is, why it is needed, how to identify code smells, and how to safely make changes using tests. The second part is dedicated to specific refactoring techniques for JavaScript: working with functions, objects, classes, modules, and asynchronous code (callbacks, promises, async/await). The third part covers advanced topics: refactoring in a functional style, working with inheritance, composition, and integrating refactoring into the development process (CI/CD, code review).

Who this book is for

The book is intended for developers who already have basic experience with JavaScript (know the syntax, can write simple scripts) and want to move to the next level — learning to write professional, maintainable code. It will be useful for both frontend developers and backend developers using Node.js. Beginners just starting to learn JS may find the book challenging, but after mastering the basics, it will be an excellent next step.

What the reader will learn

  • Basic principles of refactoring: how to safely change code without breaking functionality.
  • Identifying code smells: duplication, long functions, large classes, excessive complexity.
  • Refactoring techniques for functions: extract method, replace parameter with object, separate query from modifier.
  • Refactoring objects and classes: replace inheritance with composition, encapsulate fields, remove middle man.
  • Working with asynchrony: converting callbacks to promises, replacing promises with async/await, error handling.
  • Modular refactoring: splitting monolithic modules, improving cohesion and reducing coupling.
  • Testing as the foundation of refactoring: writing unit tests, integration tests, using TDD.
  • Functional approach: using pure functions, immutability, higher-order functions.
  • Design patterns in the context of refactoring: strategy, observer, factory, and others.
  • Tools and practices: ESLint, Prettier, automated tests, refactoring in the IDE.

Strengths of the book

The first and main advantage is its practical focus. Each technique is illustrated with a specific example of "bad" code and a step-by-step transformation into "good" code. The author doesn't just provide theory but shows how to think during the refactoring process. The second is the emphasis on modern JavaScript: the book was written in 2017 but covers ES6+ (arrow functions, classes, promises, let/const, destructuring), making it relevant today. The third is a deep understanding of JS specifics: the author thoroughly examines nuances such as scope, closures, prototypal inheritance, this, and shows how refactoring can make code more predictable. The fourth is good structure: the book reads like a textbook but can also be used as a reference — each chapter is self-contained. Finally, the author's humor and light style make reading engaging and complex topics accessible.

Recommendations