christopheradams / christopheradams/elixir_style_guide

Clarification on multiline defs

Open
#204 3 comments 1 reaction 0 assignees View on GitHub
discussion
Dominant language
Elixir
Stars
4.4k
Forks
300
PR merge metrics
No merged PRs in 30d

Description

Greetings,

First of all, thank you for the guide.

I have some difficulty understanding two points that seem (to me) to conflict. I'm not saying they're not exact, but I have trouble guessing what they mean, maybe others will too:

*
Run single-line `def`s that match for the same function together, but separate
multiline `def`s with a blank line.
[[link](#single-line-defs)]

```elixir
def some_function(nil), do: {:error, "No Value"}
def some_function([]), do: :ok

def some_function([first | rest]) do
some_function(rest)
end
```

*
If you have more than one multiline `def`, do not use single-line `def`s.
[[link](#multiple-function-defs)]

```elixir
def some_function(nil) do
{:error, "No Value"}
end

def some_function([]) do
:ok
end

def some_function([first | rest]) do
some_function(rest)
end

def some_function([first | rest], opts) do
some_function(rest, opts)
end
```

The way I see it, the second point seems to contradict the first one. No one-line clause whenever we have a clause of the same function spanning multiple lines? This arises a lot when dealing with recursion. Some clauses (the ones dealing with empty collections in particular) can be extremely short while some can be more verbose. As a teacher (and writer), I'd prefer to follow some consistency.

From what I've seen, the first point seems to be used by most Elixir developers: one-line clauses are grouped together but multiline clauses are isolated by a blank line. That's the convention I personally follow.

I probably misunderstood the second point. A clarification (on this issue or in the `README` itself) would be great.

Thank you again,

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.