Bad excuses to want a rewrite
Here's a sneak peek at a new book I'm writing about Refactoring and Rewriting. It's with a publisher so I might get in trouble for sharing this, but it's a first draft ... they own the final version đ
This code is terrible. We should rewrite.
Literally every new engineer on every team.
- Does it work?
- Has the context changed?
Okay then leave it alone. Itâs fine.
Every one of those little things you see that looks weird is a bug fix. A lesson learned. An edge case found. A painful memory enshrined in code.
You start with a simple function that responds to an event and updates the database.
async function handleEvent(event) {
const item = await readFromDB(event.item.id)
if (event.newState) {
await item.save({
status: event.newState,
})
await notifyUserOfChange(item)
}
return `Status updated to ${event.newState}`
}
Pretend this function is called from a queue or notification service. You get an event that happened on event.item and update the item.status field in the database. After saving, you send a notification to the owner of this item. Like an email that says âYour item is out for deliveryâ
One day the notification service sends an event with no item. Due to a weird bug in readFromDB, your database locked up for 5 minutes and caused a huge outage.
Both the notification service and the database are outside your control. Best you can do is to check before reading.
async function handleEvent(event) {
if (!event.item) {
throw new Error(`Missing event.item`)
}
const item = await readFromDB(event.item.id)
if (event.newState) {
await item.save({
status: event.newState,
})
await notifyUserOfChange(item)
}
return `Status updated to ${event.newState}`
}
You now throw an error when thereâs no item in the event. The error handling framework will handle logging and sending the right response status to the notification service.
A few days pass and you notice something strange in the logs. Thereâs a lot of database errors when calling item.save. 𤨠You can barely find the logs you care about through the stack traces and convoluted error messages.
After some digging you find that the notification service is pinging you for items that donât exist. You canât fix that even if you wanted to. You can add another check to your code though.
async function handleEvent(event) {
if (!event.item) {
throw new Error(`Missing event.item`)
}
const item = await readFromDB(event.item.id)
if (!item) {
throw new Error(`Item not found`)
}
if (event.newState) {
await item.save({
status: event.newState,
})
await notifyUserOfChange(item)
}
return `Status updated to ${event.newState}`
}
Great. You make sure the item was found in your database before trying to do anything else. Still an error, but a correctly handled one.
A month passes. Your function is doing great.
âHey [name|] we had a user complain they got 500 emails about their item being out for delivery. Theyâre pretty pissedâ
Thatâs odd.
The notification service must have got stuck in a loop and sent the same event 500 times. Or there was an error in notifyUserOfChange after the email goes out, but before we tell the notification service the event was handled. Kept retrying đŠ
You add another check. This time looking for idempotency â calling the function with the same arguments creates the same result.
async function handleEvent(event) {
if (!event.item) {
throw new Error(`Missing event.item`)
}
const item = await readFromDB(event.item.id)
if (!item) {
throw new Error(`Item not found`)
}
if (event.newState && event.newState !== item.status) {
await item.save({
status: event.newState,
})
await notifyUserOfChange(item)
}
return `Status updated to ${event.newState}`
}
Fantastic. Now the status update only happens, if newState is different than existing status. No more notification spam đ
Until a user says âHey I got an out for delivery email 2 days after the item was deliveredâ
You check the database and sure enough, the item is marked as âout for deliveryâ even though you can dig up a log that says it was marked as âdeliveredâ 2 days before. Your database is wrong!
Again, the notification service had a hiccup and sent events out of sequence. You need to make sure transitions are valid before updating your state. Also known as the actor model of computation.
async function handleEvent(event) {
if (!event.item) {
throw new Error(`Missing event.item`)
}
const item = await readFromDB(event.item.id)
if (!item) {
throw new Error(`Item not found`)
}
if (isValidTransition(event, item)) {
if (event.newState && event.newState !== item.status) {
await item.save({
status: event.newState,
})
await notifyUserOfChange(item)
}
}
return `Status updated to ${event.newState}`
}
Wow look at that code. Itâs so ugly â we should rewrite.
Cheers, ~Swizec
PS: you can share this if you really want me to get into trouble