Trust data

Look at the example below.

def set_new_api_key!
  reset_api_key
  save(validate: false)
end

When asked why validation was being skipped, the programmer replied that because the user record might be invalid, the api might not be reset.

This attitude is incorrect.

We can’t code in an environment of perpetual fear.

We can’t code in an environment where we do not trust our data.

We must trust our data and our code to work as they should. Having said that, we need to take precautionary measures, just in case the data is invalid.

In this case, the code should be refactored to

def set_new_api_key!
  reset_api_key
  save!
end

Now, if the record is invalid, we will receive feedback. We will look over the code and the data and fix whatever necessary.

Do not live in fear - code confidently.

We encourage you to watch Confident Code presentation by Avdi Grimm.