Five Lines of Code
Christian Clausen
  • MEAP began May 2020
  • Publication in Spring 2021 (estimated)
  • ISBN 9781617298318
  • 275 pages (estimated)
  • printed in black & white

Delivers concise refactoring patterns and corresponding rules that will prove immediately valuable for any developer who struggles to quickly, safely and consistently refactor code.

Jeff Neumann
Improving existing code—refactoring—is one of the most common tasks you’ll face as a programmer. Five Lines of Code teaches you clear and actionable refactoring rules that you can apply without relying on intuitive judgements such as “code smells.” It’s written for working developers, guiding you step by step through applying refactoring patterns to the codebase of a 2D puzzle game. Following the author’s expert perspective—that refactoring and code smells can be learned by following a concrete set of principles—you’ll learn when to refactor your code, what patterns to apply to what problem, and the code characteristics that indicate it’s time for a rework. Thanks to this hands-on guide, you’ll find yourself programming faster while still delivering high-quality code that your teammates will love to work with.

About the Technology

Refactoring is a fact of life. All code is imperfect, and refactoring is a systematic process you can use to improve the quality of your codebase. Whatever your architecture, choice of OO language, or skill as a programmer, the continuous design improvements of refactoring make your code simpler, more readable, and less prone to bugs. You’ll be amazed at the productivity boost of adding refactoring to your code hygiene routine—it’s quicker to hammer out bad code and then improve it than spending hours writing good code in the first place!

About the book

Five Lines of Code teaches working developers the shortcuts to quality code. You’ll follow author Christian Clausen’s unique approach to teaching refactoring that’s focused on concrete rules, and getting any method down to five lines or less to implement! There’s no jargon or tricky automated-testing skills required, just easy guidelines and patterns illustrated by detailed code samples. Chapter by chapter you’ll put techniques into action by refactoring a complete 2D puzzle game. Before you know it, you’ll be making serious and tangible improvements to your codebase.
Table of Contents detailed table of contents

1 Introduction

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 Summary

2 Getting to Know Refactoring; the What, Why, and Why Now?

2.1 Improving Readability and Maintainability

2.1.1 Making Code Better

2.1.2 …​ Without Changing What It Does

2.2 Gaining Speed, Flexibility, and Stability

2.2.1 Composition over Inheritance

2.2.2 Change by Addition

2.3 Refactoring and Your Daily Work

2.4 Refactoring as a Method for Learning

2.5 Summary

Part 1: Learn by Refactoring a Computer Game

3 Leveraging Functions, to Break Up Long Functions

3.1 Our First Rule, Why 5 Lines

3.1.1 Rule: 5 Lines

3.2 Our First Refactoring Pattern

3.2.1 Refactoring: Extract Method

3.3 A Second Rule

3.3.1 Rule: Either Call or Pass

3.4 Naming

3.5 Refining Further

3.5.1 Rule: if Only at the Start

3.5.2 Applying It

3.6 Summary

4 Leveraging Objects, to Eliminate Complex if Statements

4.1 A Simple if Statement

4.1.1 Rule: Never Use if With else

4.1.2 Applying It

4.1.3 Refactoring: Replace Type Code with Classes

4.1.4 Pushing Code into Classes

4.1.5 Refactoring: Push Code into Classes

4.1.6 Inlining a Superfluous Method

4.1.7 Refactoring: Inline Method

4.2 A Large if Statement

4.2.1 Removing Generality

4.2.2 Refactoring: Specialize Method

4.2.3 The Only switch Allowed

4.2.4 Rule: Never Use switch

4.2.5 Eliminating the if

4.3 Code Duplication

4.3.1 Could We Not Use an Abstract Class Instead of the Interface?

4.3.2 Rule: Only Inherit from Interfaces

4.3.3 Then, What Is Up With All This Code Duplication?

4.4 A Pair of Complex if Statements

4.5 Cleaning Up

4.5.1 Refactoring: Try Delete, Compile

4.6 Summary

5 Unifying, to Simplify and Enable Reuse

5.1 Unifying Similar Classes

5.1.1 Refactoring: Unify Similar Classes

5.2 Unifying Simple Conditions

5.2.1 Refactoring: Combine ifs

5.3 Unifying Complex Conditions

5.3.1 Arithmetics of Conditions

5.3.2 Rule: Use Pure Conditions

5.3.3 Using Condition Arithmetic

5.4 Unifying Code across Classes

5.4.1 UML Class Diagrams 101

5.4.2 Refactoring: Introduce Strategy-Pattern

5.4.3 Rule: No Interface With Only One Implementation

5.4.4 Refactoring: Extract Interface From Implementation

5.5 Unifying Identical Code

5.6 Unifying Similar Functions

5.7 Unifying Similar Code

5.8 Summary

6 Encapsulate, to Localize Invariants

Part 2: Taking What You Have Learned Into the Real World

7 Work With the Compiler

8 Avoid Comments

9 Do One Thing at a Time

10 Love Deleting Code

11 Never be Afraid to Add Code

12 Avoid Optimizations and Generality

13 Follow the Structure in the Code

14 Bad Code Should Look Bad

15 Wrapping Up

What's inside

  • The symptoms of bad code
  • The extracting method, introducing strategy pattern, and many other refactoring patterns
  • Modifying code safely, even when you don’t understand it
  • Writing stable code that enables change-by-addition
  • Proper compiler practices
  • Writing code that needs no comments
  • Real-world practices for great refactoring

About the reader

For developers who know an object-oriented programming language.

About the author

Christian Clausen works as a technical Agile coach teaching teams how to properly refactor their code. Previously he worked as a software engineer on the Coccinelle semantic patching project, an automated refactoring tool. He has an MSc in computer science, and five years’ experience teaching software quality at a university level.

placing your order...

Don't refresh or navigate away from the page.
Manning Early Access Program (MEAP) Read chapters as they are written, get the finished eBook as soon as it’s ready, and receive the pBook long before it's in bookstores.
print book $24.99 $49.99 pBook + eBook + liveBook
Additional shipping charges may apply
Five Lines of Code (print book) added to cart
continue shopping
go to cart

eBook $19.99 $39.99 3 formats + liveBook
Five Lines of Code (eBook) added to cart
continue shopping
go to cart

Prices displayed in rupees will be charged in USD when you check out.

FREE domestic shipping on three or more pBooks