Rbtz.CredoChecks.Readability.ShorthandDefMustBeCompact
(rbtz_credo_checks v0.1.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 shorthand def name(args), do: body form whose body spans
more than one line. Once the body has to wrap, switch to the explicit
do...end block form.
The shorthand exists to keep tiny bodies visually compact. Once the
body has to wrap across multiple lines, the , do: punctuation becomes
harder to spot than a plain do block — and the resulting layout
encourages awkward mid-expression line breaks.
A multi-line head (e.g. from nested pattern matching) is fine as long as the body still fits on a single line.
Bad
def something(x),
do:
one_very_long_method_call(x) || one_very_long_method_call(x) ||
one_very_long_method_call(x)Good
def something(x) do
one_very_long_method_call(x) || one_very_long_method_call(x) ||
one_very_long_method_call(x)
end
# still fine — body is a single line
def short(x),
do: x + 1
# still fine — only the head wraps; body fits on one line
defp put_id(map, %{
outer: %{inner: %{id: id}}
}),
do: Map.put(map, :id, id)The check inspects every def, defp, defmacro, and defmacrop
that uses the shorthand keyword form. Block-form (do...end)
definitions 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.