Rbtz. CredoChecks. Readability. PreferBlockFormForMultilineIf
(rbtz_credo_checks v0.6.0)
Copy Markdown
View Source
Basics
This check is disabled by default.
Learn how to enable it via .credo.exs.
This check has a base priority of normal and works with any version of Elixir.
Explanation
Forbids the keyword form if cond, do: x, else: y (and the
unless equivalent) when the expression spans more than one line.
Switch to the explicit do ... else ... end block form.
The keyword form is great for tiny one-line conditionals. Once the
expression has to wrap across multiple lines, the , do: /
, else: punctuation is harder to spot than a plain do block
and encourages awkward mid-expression line breaks.
Bad
if Enum.any?(list, &is_integer/1),
do: List.first(list),
else: List.last(list)
unless map_size(m) == 0,
do: Map.values(m),
else: []
# do-only, still multiline — flagged
if cond,
do: some_very_long_function_call(arg1, arg2)Good
if Enum.any?(list, &is_integer/1) do
List.first(list)
else
List.last(list)
end
# single-line keyword form — fine
if cond, do: x, else: y
unless cond, do: x
# block form — never flagged
if cond do
x
else
y
endThe check inspects every if and unless that uses the shorthand
keyword form (i.e. , do: rather than do ... end). Block-form
conditionals are not considered.
Check-Specific Parameters
There are no specific parameters for this check.
General Parameters
Like with all checks, general params can be applied.
Parameters can be configured via the .credo.exs config file.