Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim authored Sep 11, 2023
1 parent 159fe84 commit 304fde0
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions lib/elixir/pages/anti-patterns/design-anti-patterns.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,26 +104,29 @@ iex> AlternativeInteger.parse("13", discard_rest: false)

#### Refactoring

To refactor this anti-pattern, as shown next, add a specific function for each return type (for example, `parse_no_rest/1`), no longer delegating this to options passed as arguments.
To refactor this anti-pattern, as shown next, add a specific function for each return type (for example, `parse_discard_rest/1`), no longer delegating this to options passed as arguments.

```elixir
defmodule AlternativeInteger do
@spec parse(String.t()) :: {integer(), rest :: String.t()} | :error
@spec parse(String.t()) :: {integer(), String.t()} | :error
def parse(string) do
Integer.parse(string)
end

@spec parse_discard_rest(String.t()) :: integer()
@spec parse_discard_rest(String.t()) :: integer() | :error
def parse_discard_rest(string) do
String.to_integer(string)
case Integer.parse(string) do
{int, _rest} -> int
:error -> :error
end
end
end
```

```elixir
iex> AlternativeInteger.parse("13")
{13, ""}
iex> AlternativeInteger.parse_no_rest("13")
iex> AlternativeInteger.parse_discard_rest("13")
13
```

Expand Down

0 comments on commit 304fde0

Please sign in to comment.