Skip to content

Commit

Permalink
unless docs
Browse files Browse the repository at this point in the history
  • Loading branch information
novaugust committed Sep 9, 2024
1 parent 32ece53 commit 3f18d04
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions docs/control_flow_macros.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,22 @@ The number of "blocks" in Elixir means there are many ways to write semantically

We believe readability is enhanced by using the simplest api possible, whether we're talking about internal module function calls or standard-library macros.

### use `case`, `if`, or `unless` when...
### use `case`, `if`, or `cond` when...

We advocate for `case` and `if` as the first tools to be considered for any control flow as they are the two simplest blocks. If a branch _can_ be expressed with an `if` statement, it _should_ be. Otherwise, `case` is the next best choice. In situations where developers might reach for an `if/elseif/else` block in other languages, `cond do` should be used.

(`cond do` seems to see a paucity of use in the language, but many complex nested expressions or with statements can be improved by replacing them with a `cond do`).

`unless` is a special case of `if` meant to make code read as natural-language (citation needed). While it sometimes succeeds in this goal, its absence in most programming languages often makes it feel cumbersome to programmers with non-Ruby backgrounds. Thankfully, with Styler's help developers don't need to ever reach for `unless` - expressions that are "simpler" with its use are automatically rewritten to use it.
### use `unless` when...

Never! `unless` [is being deprecated](https://github.com/elixir-lang/elixir/pull/13769#issuecomment-2334878315) and so should not be used.

### use `with` when...

> `with` great power comes great responsibility
>
> - Uncle Ben

As the most powerful of the Kernel control-flow expressions, `with` requires the most cognitive overhead to understand. Its power means that we can use it as a replacement for anything we might express using a `case`, `if`, or `cond` (especially with the liberal application of small private helper functions).

Unfortunately, this has lead to a proliferation of `with` in codebases where simpler expressions would have sufficed, meaning a lot of Elixir code ends up being harder for readers to understand than it needs to be.
Expand Down

0 comments on commit 3f18d04

Please sign in to comment.