I like it when running a production system is boring. No outages, no hotfixes, no on-call duties… no problems. It might sound unachievable, but ther

Levels of Defensiveness

submited by
Style Pass
2025-01-13 09:30:06

I like it when running a production system is boring. No outages, no hotfixes, no on-call duties… no problems. It might sound unachievable, but there are steps you can take to get closer to this state.

To demonstrate, let’s take the arbitrary example of loading a string from a source of data and converting it to military time. Basically turn “11:30” into some kind time object. For the demonstration I’ll use Swift, but this works in any language as I won’t use Swift-specific tricks..

This will do what’s expected, and might be what you want to write if you are confident in the state of the data and how the function will be used in general.

Because of this, it’ll be key to edit the code to make sure all cases are properly covered if we want this function to be as stable as possible and not wake us up at night.

Moving forward, I’ll just focus on value possibly being nil, the logic for handling the problem will be the same for all other issues mentioned above. The principles will of course also apply to the other problem cases.

Leave a Comment