Comparing against nil must use is_nil/1, not an equality operator.
value == nil and value != nil obscure intent and invite ==/===
inconsistency. is_nil/1 says exactly what is being asked, works in guards, and
is the required spelling in Ecto queries.
# BAD — equality operator against nil
defmodule MyApp.Worker do
def missing?(value), do: value == nil
def present?(value), do: value != nil
def fallback(value) when value === nil, do: :default
end
# GOOD — is_nil/1, in bodies and guards alike
defmodule MyApp.Worker do
def missing?(value), do: is_nil(value)
def present?(value), do: not is_nil(value)
def fallback(value) when is_nil(value), do: :default
endnil on either side is caught:
value == nil # caught
nil == value # caught
value !== nil # caughtPattern matches on nil (case value do nil -> ...) and nil passed as an
argument are not comparisons and are never flagged.