Refactor: Duplicate and Customize
Example of Duplicate and Customize, for an “if” statement “If” statements often attract complexity. They gather compound conditions that are hard to understand. More “if” statements get nested inside....
View ArticleRefactor: Parallel Arrays to Array of Objects
Your code sometimes evolves to have two or more arrays operating in parallel. If these arrays work in lockstep, it might be better to have one array with objects instead. Let’s look at an example...
View ArticleRefactor Template Method to Strategy
The Template Method design pattern defines an algorithm in an abstract class, and defers some steps to its subclasses. A Strategy defines a family of pluggable implementations. We’ll see how to...
View ArticleLocal Refactorings
To refactor is to systematically “improve the design of existing code”. There are many ways to classify refactorings, but one distinction stands out to me: local refactorings. A local refactoring...
View ArticleData Flow Analysis for Refactoring
As you refactor, you sometimes need to understand how the data flow through the code. We’ll look at several ways that can reveal that information. Data Flow Analysis Data flow analysis tracks the...
View ArticleTransliterating Old-School BASIC
Once in a while, I run into a 1980s-era BASIC program, typically written for Commodore 64, Apple ][, or TRS-80. I’d like to transliterate it into a modern language, refactor it, and explore it. These...
View ArticleWrapping a Type, All the Way
Sometimes you want to change from using a basic type (such as Int or String) to a type that contains the basic type as a value. (See Refactoring in the References.) The change itself isn’t hard, but...
View ArticleNested Loops to Functional Pipeline
Nested structures are challenging to read; a pipeline is usually easier to read and think about. Nested structures have that “arrow of doom” feeling, where you work your way in multiple levels then...
View ArticleRefactoring: Forcing the Right Extract Method
Automated refactoring tools are great, but sometimes the tool doesn’t make the best choice. We can often nudge the tool to a better choice. Why Bother? Why bother nudging? Couldn’t you do the...
View ArticleExtracting Parameterized Unit Tests
Duplication across tests may not be as harmful as duplication in production code, but a parameterized unit test can reduce duplication and help us create better tests. Judging Tests – The Pause...
View ArticleLambda for Control Structures, a Refactoring
Several mainstream languages have added better support for lambda expressions (including lighter syntax) in the last few years. This can let you define new forms of control structures, and reduce...
View ArticleLambda to Reduce Coupling
Coupling occurs when one part of a system depends on another part. That is, if one part changes, the other must be adjusted too. Lambda expressions can reduce coupling by needing to refer to fewer...
View ArticleRefactoring and Programming Language Semantics
Refactoring improves the design of code without changing its observable behavior. But what is observable behavior? We’ll look at how language semantics affect that. When you devise new refactorings,...
View ArticleLoop Unrolling: While ⇒ If-While
Loop unrolling is a performance optimization where you repeat the inside of a loop to reduce the looping overhead. It’s also a way to get rid of loops, and we’ll look at an example from a test. While...
View Article2021 Articles in Review
Here are the the articles and videos I made last year. Key topics: user stories, refactoring, TDD, functional languages, and design. I’ve grouped them into these areas: User...
View ArticleRefactoring, a Whole-Team Guide
A one-page summary of Refactoring is available in PDF. Refactoring is…? Changing the design of existing code without changing its observable behavior (per Martin Fowler). These changes include...
View ArticleRefactoring: Replace Method with Method Object
Sometimes, when you try to break up a long method, you run into a problem: you have to pass a bunch of local variables into the new method, some of which are both input and output. One solution is the...
View ArticleRefactoring: Extract Local Lambda
Extracting a local lambda turns a block of code into an anonymous inline function. Motivation A local lambda can isolate code for improvement (the Isolate-Improve-Inline pattern – see References), or...
View ArticleRefactoring Undifferentiated Exceptions
Some exception-handling code peels apart exceptions to figure out how to handle them. This is a missed opportunity for more direct code. The Simple Case: Type-Tested Exceptions Sometimes we see...
View ArticleReduce Dependencies Using Lambda
When A depends on B (A → B), a change in B may force A to change as well. Dependencies are both good and bad: they let objects work together, but they make software harder to change. We can use lambda...
View Article