YAGNI: delete speculative options
Delete the settings nobody uses, so each new column is a two-line change.
Watch the narrated video
Every step
Problem
Steps 1–4Refactor
Steps 5–10Payoff
Step 11Kotlin bonus
Steps 12–13Recap
Steps 14–15The code: before and after
Before
/* application.yml
export:
format: csv
delimiter: ","
header: true
*/
data class ExportConfig(
val format: String,
val delimiter: String,
val header: Boolean,
)
class OrderExport(cfg: ExportConfig) {
private val format = cfg.format
private val delimiter = cfg.delimiter
private val header = cfg.header
fun export(orders: List<Order>): String {
val out = StringBuilder()
if (header && format == "csv") {
out.append("id${delimiter}total\n")
}
for (o in orders) {
val row = when (format) {
"csv" -> "${o.id}$delimiter${o.total}"
"json" -> """{"id":"${o.id}","total":${o.total}}"""
"xml" -> """<order id="${o.id}" total="${o.total}"/>"""
else -> throw IllegalArgumentException(format)
}
out.append(row).append("\n")
}
return out.toString()
}
}
class NightlyJob(
private val config: ExportConfig,
private val orders: OrderRepo,
private val warehouse: Warehouse,
) {
fun run() {
val csv = OrderExport(config).export(orders.today())
warehouse.uploadCsv(csv)
}
}After
/* application.yml
*/
class OrderExport {
fun export(orders: List<Order>): String {
return buildString {
appendLine("id,total,postalCode")
for (o in orders) {
appendLine("${o.id},${o.total},${o.postalCode}")
}
}
}
}
class NightlyJob(
private val orders: OrderRepo,
private val warehouse: Warehouse,
) {
fun run() {
val csv = OrderExport().export(orders.today())
warehouse.uploadCsv(csv)
}
}Transcript, step by step
Problem
Step 1: An export with three settings Before
Every night, this job exports today's orders and uploads them to the warehouse. Order Export reads three settings from the config file at the top, a format, a delimiter and a header flag. It writes a header line, then one row per order in whichever format the config names. It can write CSV, JSON or XML, so it looks ready for whatever the business asks for next.
Step 2: One column, four edits Code smell
The warehouse now wants the postal code as a third column. So the header gets a new name, and every branch of the when expression gets a new value. A speculative option is a setting built for a need nobody has yet, and this class reads three of them. You can see what they cost here in the header, here in the CSV branch, here in JSON, and here in XML.
Step 3: Two formats nobody reads Code smell
Yet the config file only ever says CSV, and the only caller uploads CSV to the warehouse. So the JSON and XML branches never run, and nobody would notice if one of them broke. One new column costs four edits, and two of them land in formats nothing reads. A missing quote in the XML line would compile fine and sit there until someone finally turns XML on. And every option multiplies the paths a reader has to check before trusting a change.
Step 4: Back it out, then delete Plan
Let's back that change out. YAGNI, you aren't gonna need it, says you build a feature when someone needs it, and these two formats have nobody. Kent Beck put it as make the change easy, then make the easy change. So we delete the options nobody uses, one small step at a time, and the file the warehouse gets stays the same after each one.
Refactor
Step 5: Delete the XML branch Remove Dead Code
No config value picks XML and no caller reads it, so the XML branch goes first. The when expression keeps CSV, JSON and the else branch, and the file the warehouse gets is the same as before.
Step 6: Delete the JSON branch Remove Dead Code
JSON goes for the same reason, so the when expression is down to the CSV branch and an else. That else only throws for a format the config file never sets.
Step 7: Format can only be csv Remove Field
With one branch left, format can only ever be CSV, so it stops being a property. The when expression collapses into the CSV row, and the header test only checks the header flag. Format leaves the config file too, because nothing reads it anymore.
Step 8: The delimiter is always a comma Remove Field
The delimiter was a comma in the config and nowhere else, so we write the comma in place. The property goes, along with its line in Export Config and its line in the config file.
Step 9: The header is always on Remove Field
Header is always true, so the path that skips the header line has never run. We delete the if, and then the property, because nothing reads it anymore. Export Config becomes a plain empty class, because a Kotlin data class needs at least one property. The config file is down to an export section with nothing in it.
Step 10: Nothing left to configure Remove Parameter
Order Export took a config and read nothing from it, so the parameter goes, and the Export Config class goes with it. The nightly job builds Order Export with no arguments now, and it drops its config property. And the config file at the top is empty.
Payoff
Step 11: The postal code, in two edits Payoff
Now the postal code goes back in. It takes one new name in the header and one new value in the row, so that's two edits. The JSON and XML branches that took two of the four edits are gone, and there's one row line where there were three branches. YAGNI fit here because the config always said CSV and the only caller uploads CSV, so those options cost an edit on every change and gave nothing back. The loop, the return and the nightly job didn't change.
Kotlin bonus
Step 12: A builder you keep naming Kotlin bonus
The method still carries some ceremony around its text. It names the out builder four times, types each newline by hand, and ends with a to String call that only turns the builder back into text.
Step 13: Let buildString hold the builder buildString
The standard library's build String creates the builder, hands it to the lambda as its receiver, and returns the finished string. Append Line adds the newline, so the header and each row fit on one line apiece. The warehouse gets the same bytes as before, and the next column is still two edits.
Recap
Step 14: Where we started, where we ended Before / after
On the left, three properties copied a format, a delimiter and a header flag out of the config. The when expression on format had three branches, and only the CSV one ever ran. On the right, there's one short method, and the config file is empty. It already writes the postal code, and the whole method is about the size of the old when expression.
Step 15: When to reach for it Takeaways
Delete an option when the config only ever sets one value and no caller reaches the other branches. Keep it when a second user is real and dated, like a signed contract with a JSON partner, or when ops set the value per environment, because deleting it means writing it again against a deadline. You'll spot it by config values that never change, branches no caller reaches, and defaults that nobody overrides.