Credo v1.5.0-rc.1 Credo.Check.Refactor.NegatedIsNil View Source

This check has a base priority of low and works with any version of Elixir.

Explanation

We should avoid negating the is_nil predicate function. Here are a couple of examples: The code here ...

def fun(%{external_id: external_id, id: id}) when not is_nil(external_id) do
   ...
end

... can be refactored to look like this:

def fun(%{external_id: nil, id: id}) do
  ...
end
def fun(%{external_id: external_id, id: id}) do
  ...
end

... or even better, can match on what you were expecting on the first place:

def fun(%{external_id: external_id, id: id}) when is_binary(external_id) do
  ...
end
def fun(%{external_id: nil, id: id}) do
  ...
end
def fun(%{external_id: external_id, id: id}) do
  ...
end

Similar to negating unless blocks, the reason for this check is not technical, but a human one. If we can use the positive, more direct and human friendly case, we should.

Configuration parameters

There are no parameters for this check.

Link to this section Summary

Link to this section Functions

Link to this function

do_run_on_source_file(exec, source_file, params)

View Source