Patterncore
Speculative Generality
Also called presumptive feature.
A code smell: code has hooks, parameters, abstract classes and special cases for needs that the program does not have, and they make the code that it does need harder to read and to change.
- Family
- code-smell
- Intent
- Find the generality that no current requirement uses, and remove it, so that the code says only what the program does.
Intent
Find the generality that the program does not use: a parameter that every caller passes the same value for, a plugin point with one plugin, an abstract class with one subclass, a case for a format that nobody asks for. Remove it, so that the code says only what the program does, and add generality back when a real need comes.
Problem
Programmers try to see ahead. A function that exports CSV now will surely need JSON one day, so it takes a format parameter. A class that talks to one database might talk to another, so it gets an interface and a factory. Refactoring describes the smell by the sentence that justifies it: someone says that the code will need the ability one day, and adds hooks and special cases for things that are not required.1
The principle that the smell breaks has a name. Wikipedia describes YAGNI ("You aren't gonna need it") as "a principle which arose from extreme programming (XP) that states a programmer should not add functionality until deemed necessary."2 Fowler's article on it uses the term "presumptive feature" for "any code that supports a feature that isn't yet being made available for use."3
The forces are these:
- The future need may never come. Fowler cites a study at Microsoft which found that only a third of carefully planned features improved the metrics they were meant to improve.3
- Even when it comes, the guess about its shape is often wrong. Fowler writes that "your current ideas of what abstractions you'll need will not match what you learn when you do actually need them."3
- The generality has a cost while it waits. Fowler calls it the cost of carry: the code for the presumptive feature "adds some complexity to the software, this complexity makes it harder to modify and debug that software".3
- Refactoring gives a sign of the smell that is quick to check: the only users of a function or class are its tests.1
Solution
Refactoring names a refactoring for each form of the smell.1 The catalogue at refactoring.com has a card for each, and gives Remove Parameter as one of the names of Change Function Declaration.4
- An abstract class that does little: Collapse Hierarchy.
- A layer of delegation that is not needed: Inline Function and Inline Class.
- A parameter that no caller uses: Change Function Declaration, to remove it.
- A function or class that only tests use: delete the tests, then Remove Dead Code.
The first edition has the same smell, and names Remove Parameter for the parameters that no caller uses.5
Fowler gives a test for what to keep. Ask what the refactoring would cost later, when the need is real; "Often that thought experiment is enough to convince them that it won't be significantly more expensive to add it later."3 He also states the rule for abstractions: "any abstraction that makes it harder to understand the code for current requirements is presumed guilty."3
Example
The export function takes a format and an options object, and looks up a writer in a registry. The program only ever exports CSV with a comma. Each caller must pass the format, and a reader must follow the registry to find the code that runs.
import assert from "node:assert/strict";
// Before: a registry, a format parameter and options that no caller uses.
const writers = new Map();
function registerWriter(format, writer) {
writers.set(format, writer);
}
registerWriter("csv", (rows, options) =>
rows.map((r) => r.join(options.delimiter ?? ",")).join(options.lineEnd ?? "\n"),
);
function exportRowsBefore(rows, format = "csv", options = {}) {
const writer = writers.get(format);
if (!writer) throw new Error(`no writer for ${format}`);
return writer(rows, options);
}
// After: the one format that the program uses.
const toCsv = (rows) => rows.map((r) => r.join(",")).join("\n");
const rows = [["id", "name"], [1, "Ada"], [2, "Grace"]];
assert.equal(exportRowsBefore(rows), "id,name\n1,Ada\n2,Grace");
assert.equal(toCsv(rows), exportRowsBefore(rows, "csv", {}));
console.log("toCsv: tests pass");
toCsv: tests pass
The second version is one line, and its name says what it produces. If a JSON export is needed later, the programmer adds toJson next to it, and if many formats appear, a registry can come back with the real formats as a guide to its shape.
| Language | What the example shows |
|---|---|
| JavaScript | a writer registry and unused options, removed, as above |
| Python | the same registry and options, removed |
| Java | an interface and an abstract base class with one subclass, collapsed |
| Go | an options struct whose fields no caller sets, removed |
# Before: a registry, a format parameter and options that no caller uses.
_writers = {}
def register_writer(fmt, writer):
_writers[fmt] = writer
register_writer(
"csv",
lambda rows, options: options.get("line_end", "\n").join(
options.get("delimiter", ",").join(str(v) for v in r) for r in rows
),
)
def export_rows_before(rows, fmt="csv", options=None):
writer = _writers.get(fmt)
if writer is None:
raise ValueError(f"no writer for {fmt}")
return writer(rows, options or {})
# After: the one format that the program uses.
def to_csv(rows):
return "\n".join(",".join(str(v) for v in r) for r in rows)
rows = [["id", "name"], [1, "Ada"], [2, "Grace"]]
assert export_rows_before(rows) == "id,name\n1,Ada\n2,Grace"
assert to_csv(rows) == export_rows_before(rows, "csv", {})
print("to_csv: tests pass")
to_csv: tests pass
options=Noneandoptions or {}avoid a shared mutable default, which is the usual Python form. In the second version the question does not come up, because there are no options.- For real CSV with quotes and commas inside values, the standard
csvmodule is the right tool. The example keeps the values plain to show the shape of the change.
import java.util.List;
import java.util.stream.Collectors;
public class Main {
// Before: an interface and an abstract base class, with one subclass.
interface RowWriter {
String write(List<List<Object>> rows);
}
abstract static class AbstractDelimitedWriter implements RowWriter {
protected abstract String delimiter();
public String write(List<List<Object>> rows) {
return rows.stream()
.map(r -> r.stream().map(String::valueOf).collect(Collectors.joining(delimiter())))
.collect(Collectors.joining("\n"));
}
}
static final class CsvWriter extends AbstractDelimitedWriter {
protected String delimiter() { return ","; }
}
static String exportBefore(RowWriter writer, List<List<Object>> rows) {
return writer.write(rows);
}
// After: the hierarchy is collapsed into one static method.
static String toCsv(List<List<Object>> rows) {
return rows.stream()
.map(r -> r.stream().map(String::valueOf).collect(Collectors.joining(",")))
.collect(Collectors.joining("\n"));
}
public static void main(String[] args) {
List<List<Object>> rows = List.of(List.of("id", "name"), List.of(1, "Ada"), List.of(2, "Grace"));
if (!exportBefore(new CsvWriter(), rows).equals("id,name\n1,Ada\n2,Grace")) throw new AssertionError("before");
if (!toCsv(rows).equals(exportBefore(new CsvWriter(), rows))) throw new AssertionError("after");
System.out.println("toCsv: tests pass");
}
}
toCsv: tests pass
- Three types,
RowWriter,AbstractDelimitedWriterandCsvWriter, hold one decision: the delimiter is a comma. Collapse Hierarchy and Inline Class bring it down to one method. - If a second format appears, the interface can come back. Then its methods follow the two real formats, not a guess.
package main
import (
"fmt"
"strings"
)
// Before: an options struct whose fields no caller sets.
type ExportOptions struct {
Delimiter string
LineEnd string
Header bool
}
func exportBefore(rows [][]string, opts ExportOptions) string {
d, le := opts.Delimiter, opts.LineEnd
if d == "" {
d = ","
}
if le == "" {
le = "\n"
}
start := 0
if !opts.Header && len(rows) > 0 && rows[0][0] == "#" {
start = 1 // a rule for a header format that no input has
}
lines := make([]string, 0, len(rows))
for _, r := range rows[start:] {
lines = append(lines, strings.Join(r, d))
}
return strings.Join(lines, le)
}
// After: the one form that the program uses.
func toCSV(rows [][]string) string {
lines := make([]string, 0, len(rows))
for _, r := range rows {
lines = append(lines, strings.Join(r, ","))
}
return strings.Join(lines, "\n")
}
func main() {
rows := [][]string{{"id", "name"}, {"1", "Ada"}, {"2", "Grace"}}
if exportBefore(rows, ExportOptions{}) != "id,name\n1,Ada\n2,Grace" {
panic("before")
}
if toCSV(rows) != exportBefore(rows, ExportOptions{}) {
panic("after")
}
fmt.Println("toCSV: tests pass")
}
toCSV: tests pass
- Each caller writes
ExportOptions{}, so every field has its zero value, and the code for the other values never runs. A search forExportOptions{with fields set finds no caller. - The
Headerrule is the kind of special case that Refactoring names: code for an input that the program does not have.
Consequences
The code is shorter and says what the program does. A reader does not need to learn hooks that nothing uses, and a change to the real behaviour does not need to keep the unused paths working.
Fowler names the limits of the rule. YAGNI is about features, not about the health of the code: "expending effort on refactoring isn't a violation of yagni because refactoring makes the code more malleable."3 He also says that a choice for the future that "doesn't actually increase the complexity of the software" needs no YAGNI argument.3 His example is a lookup table for error messages, which costs little now and makes translation easier later.3
Some generality is required by the context, not guessed. A library with outside users must keep a stable interface, and a parameter that its own code does not use may be used by its clients. A public API is not the place to remove a parameter without a deprecation period.
YAGNI works together with other practices. Wikipedia notes that it "is meant to be used in combination with several other practices, such as continuous refactoring, continuous automated unit testing, and continuous integration."2 Without tests and refactoring, the cost of adding the generality later is real, and the argument for removing it is weaker.
How to fix it
- An abstract class with one subclass: Collapse Hierarchy.1
- A layer that only forwards: Inline Function or Inline Class.1
- A parameter that every caller passes the same way, or not at all: Change Function Declaration to remove it.1
- Code whose only callers are tests: delete the tests and Remove Dead Code.1
Variants and related patterns
Lazy Element. Generality that was never used often leaves behind elements that do nothing. See Lazy Element.
Dead code. Code that the program never runs. Speculative code that was never called is one kind of it. See Dead code.
Middle Man. A layer of delegation added for a future that did not come. See Middle Man.
References
- Martin Fowler, with Kent Beck, Refactoring, Improving the Design of Existing Code, second edition, Addison-Wesley, 2018, Chapter 3, Bad Smells in Code, Speculative Generality
- Wikipedia, You aren't gonna need it, revision 1374076145, the introduction
- Martin Fowler, Yagni, bliki, the introduction, the costs of a presumptive feature, and the section on abstractions
- Martin Fowler, Catalog of Refactorings, the cards Collapse Hierarchy, Inline Function, Inline Class, Change Function Declaration (alias Remove Parameter) and Remove Dead Code
- Martin Fowler, Refactoring, Improving the Design of Existing Code, Addison-Wesley, 1999, Chapter 3, Bad Smells in Code, Speculative Generality
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag 5C308A Edit this page History Report an error Markdown