Prefer errors at boottime to errors at runtime

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the architecture category.

Last Updated: 2024-11-23

Imagine you had taxons data stored in a hash format:

taxons_data = [
  {name: "law", uuid: 1},
  {name: "medicine", uuid: 2}
]

Now imagine some data is missing. Compare your lot in two possible system designs.

System 1: Data only accessed in some random HTML templates

Say you put taxons_data.fetch(:name) in a helper method in a html.erb file. Now it will only explode due to the missing data when that particular page is loaded (which could be minutes, hours, or days after deploying)

System 2: Data accessed when application boots

Now imagine having a taxons_data.rb file that turns the hash data into Taxon objects (by calling title = taxons_data.fetch(:name) on application load).

Here's what the code might look like:

class Taxon
end

taxons_data.map do |taxon_data|
  Taxon.new(
    name: taxon_data.fetch(:name)
    uuid: taxon_data.fetch(:uuid)
  )
end

In this second scenario, you won't even be able to boot the application or deploy if there are any errors with this data. This brittleness is good in many systems - it alerts you to problems sooner.