1 Introduction

It is well-known that high code quality leads to cheaper maintenance, fewer errors, and happier developers. The most common way to get high code quality is through refactoring. However, the way refactoring is usually taught — with code smells and unit testing — imposes an unnecessarily high barrier to entry. I believe that anyone can execute simple refactoring patterns safely, with a little practice.

CH01 F01 SkillsCultureTools

Figure 1. 1. Skills, culture, or tools

In software development, we place problems somewhere on the diagram shown in figure 1.1, indicating a lack of sufficient skills, culture, tools, or a combination of those. Refactoring is a sophisticated endeavor and therefore lies right in the middle. It requires each component:

  • Skills. We need the skills to know what code is bad and needs refactoring. Experienced programmers can determine this through their knowledge of code smells. But those are fluffy and not easy to learn; and to a junior developer, understanding code smells can look more like a sixth sense than a skill.
  • Culture. We need a culture and workflow that encourage taking the time to perform refactoring. In many cases, this is implemented through the famous red-green-refactor loop used in test-driven development. However, test-driven development is a much more difficult craft, in my opinion. Red-green-refactor also does not easily give way to doing refactoring in a legacy codebase.

1.1. What Is Refactoring?

1.2. Skills: What to Refactor?

1.2.1. An Example Code Smell

1.2.2. An Example Rule

1.3. Culture: When to Refactor?

1.3.1. Introducing Refactoring in a Legacy System

1.3.2. When Should You Not Refactor?

1.4. Tools: How to Refactor (Safely)?

1.5. Tools You Need to Get Started

1.5.1. Programming Language: TypeScript

1.5.2. Editor: Visual Studio Code

1.5.3. Version Control: Git

1.6. Overarching Example: a 2D Puzzle Game

1.7. A Note on Real-world Software

1.8. Summary