Skip to content

Commit

Permalink
Public / private functions rule to not encourage do_* pattern
Browse files Browse the repository at this point in the history
the `do_*` pattern is discouraged in elixir-core [1],
I think we could follow the same "guidelines"?

it's discouraged because we usually can find more descriptive
names [2]

wdyt folks?

[1] elixir-lang/elixir#6542 (comment)
[2] https://elixirforum.com/t/dealing-with-private-and-public-function-default-conflicts/39683/6.
  • Loading branch information
mracos authored and christopheradams committed May 30, 2022
1 parent e19f99c commit dc29a21
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -697,16 +697,18 @@ rules.
```

* <a name="private-functions-with-same-name-as-public"></a>
Private functions with the same name as public functions should start with
`do_`.
Private functions should not have the same name as public functions.
Also, the `def name` and `defp do_name` pattern is discouraged.

Usually one can try to find more descriptive names focusing on the differences.
<sup>[[link](#private-functions-with-same-name-as-public)]</sup>

```elixir
def sum(list), do: do_sum(list, 0)
def sum(list), do: sum_total(list, 0)

# private functions
defp do_sum([], total), do: total
defp do_sum([head | tail], total), do: do_sum(tail, head + total)
defp sum_total([], total), do: total
defp sum_total([head | tail], total), do: sum_total(tail, head + total)
```

### Comments
Expand Down

0 comments on commit dc29a21

Please sign in to comment.