Skip to content

Commit 4b79587

Browse files
committed
Improve examples and docs
1 parent 3036401 commit 4b79587

File tree

3 files changed

+30
-68
lines changed

3 files changed

+30
-68
lines changed

lib/elixir/lib/registry.ex

+7-7
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ defmodule Registry do
2727
`Registry.start_link/1`, it can be used to register and access named
2828
processes using the `{:via, Registry, {registry, key}}` tuple:
2929
30-
{:ok, _} = Registry.start_link(keys: :unique, name: Registry.ViaTest)
31-
name = {:via, Registry, {Registry.ViaTest, "agent"}}
30+
{:ok, _} = Registry.start_link(keys: :unique, name: MyApp.Registry)
31+
name = {:via, Registry, {MyApp.Registry, "agent"}}
3232
{:ok, _} = Agent.start_link(fn -> 0 end, name: name)
3333
Agent.get(name, & &1)
3434
#=> 0
@@ -39,22 +39,22 @@ defmodule Registry do
3939
In the previous example, we were not interested in associating a value to the
4040
process:
4141
42-
Registry.lookup(Registry.ViaTest, "agent")
42+
Registry.lookup(MyApp.Registry, "agent")
4343
#=> [{self(), nil}]
4444
4545
However, in some cases it may be desired to associate a value to the process
4646
using the alternate `{:via, Registry, {registry, key, value}}` tuple:
4747
48-
{:ok, _} = Registry.start_link(keys: :unique, name: Registry.ViaTest)
49-
name = {:via, Registry, {Registry.ViaTest, "agent", :hello}}
48+
{:ok, _} = Registry.start_link(keys: :unique, name: MyApp.Registry)
49+
name = {:via, Registry, {MyApp.Registry, "agent", :hello}}
5050
{:ok, agent_pid} = Agent.start_link(fn -> 0 end, name: name)
51-
Registry.lookup(Registry.ViaTest, "agent")
51+
Registry.lookup(MyApp.Registry, "agent")
5252
#=> [{agent_pid, :hello}]
5353
5454
To this point, we have been starting `Registry` using `start_link/1`.
5555
Typically the registry is started as part of a supervision tree though:
5656
57-
{Registry, keys: :unique, name: Registry.ViaTest}
57+
{Registry, keys: :unique, name: MyApp.Registry}
5858
5959
Only registries with unique keys can be used in `:via`. If the name is
6060
already taken, the case-specific `start_link` function (`Agent.start_link/2`

lib/elixir/pages/anti-patterns/code-anti-patterns.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ There are few known exceptions to this anti-pattern:
299299

300300
* [Protocol implementations](`Kernel.defimpl/2`) are, by design, defined under the protocol namespace
301301

302-
* [Custom Mix tasks](`Mix.Task`) are always defined under the `Mix.Tasks` namespace, such as `Mix.Tasks.PlugAuth`
302+
* In some scenarios, the namespace owner may allow exceptions to this rule. For example, in Elixir itself, you defined [custom Mix tasks](`Mix.Task`) by placing them under the `Mix.Tasks` namespace, such as `Mix.Tasks.PlugAuth`
303303

304304
* If you are the maintainer for both `plug` and `plug_auth`, then you may allow `plug_auth` to define modules with the `Plug` namespace, such as `Plug.Auth`. However, you are responsible for avoiding or managing any conflicts that may arise in the future
305305

lib/elixir/pages/anti-patterns/design-anti-patterns.md

+22-60
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ Remember booleans are internally represented as atoms. Therefore there is no per
126126

127127
#### Problem
128128

129-
This anti-pattern refers to code that uses exceptions for control flow. Exception handling itself does not represent an anti-pattern, but developers must prefer to use `case` and pattern matching to change the flow of their code, instead of `try/rescue`. In turn, library authors should provide developers with APIs to handle errors without relying on exception handling. When developers have no freedom to decide if an error is exceptional or not, this is considered an anti-pattern.
129+
This anti-pattern refers to code that uses `Exception`s for control flow. Exception handling itself does not represent an anti-pattern, but developers must prefer to use `case` and pattern matching to change the flow of their code, instead of `try/rescue`. In turn, library authors should provide developers with APIs to handle errors without relying on exception handling. When developers have no freedom to decide if an error is exceptional or not, this is considered an anti-pattern.
130130

131131
#### Example
132132

@@ -186,81 +186,35 @@ end
186186

187187
A common practice followed by the community is to make the non-raising version return `{:ok, result}` or `{:error, Exception.t}`. For example, an HTTP client may return `{:ok, %HTTP.Response{}}` on success cases and `{:error, %HTTP.Error{}}` for failures, where `HTTP.Error` is [implemented as an exception](`Kernel.defexception/1`). This makes it convenient for anyone to raise an exception by simply calling `Kernel.raise/1`.
188188

189-
## Feature envy
190-
191-
#### Problem
192-
193-
This anti-pattern occurs when a function accesses more data or calls more functions from another module than from its own. The presence of this anti-pattern can make a module less cohesive and increase code coupling.
194-
195-
#### Example
196-
197-
In the following code, all the data used in the `calculate_total_item/1` function of the module `Order` comes from the `OrderItem` module. This increases coupling and decreases code cohesion unnecessarily.
198-
199-
```elixir
200-
defmodule Order do
201-
# Some functions...
202-
203-
def calculate_total_item(id) do
204-
item = OrderItem.find_item(id)
205-
total = (item.price + item.taxes) * item.amount
206-
207-
if discount = OrderItem.find_discount(item) do
208-
total - total * discount
209-
else
210-
total
211-
end
212-
end
213-
end
214-
```
215-
216-
#### Refactoring
217-
218-
To remove this anti-pattern we can move `calculate_total_item/1` to `OrderItem`, decreasing coupling:
219-
220-
```elixir
221-
defmodule OrderItem do
222-
def find_item(id)
223-
def find_discount(item)
224-
225-
def calculate_total_item(id) do # <= function moved from Order!
226-
item = find_item(id)
227-
total = (item.price + item.taxes) * item.amount
228-
discount = find_discount(item)
229-
230-
unless is_nil(discount) do
231-
total - total * discount
232-
else
233-
total
234-
end
235-
end
236-
end
237-
```
238-
239-
This refactoring is only possible when you own both modules. If the module you are invoking belongs to another application, then it is not possible to add new functions to it, and your only option is to define an additional module that augments the third-party module.
240-
241189
## Primitive obsession
242190

243191
#### Problem
244192

245-
This anti-pattern happens when Elixir basic types (for example, *integer*, *float*, and *string*) are abusively used in function parameters and code variables, rather than creating specific composite data types (for example, *tuples*, *maps*, and *structs*) that can better represent a domain.
193+
This anti-pattern happens when Elixir basic types (for example, *integer*, *float*, and *string*) are excessively used to carry structured information, rather than creating specific composite data types (for example, *tuples*, *maps*, and *structs*) that can better represent a domain.
246194

247195
#### Example
248196

249197
An example of this anti-pattern is the use of a single *string* to represent an `Address`. An `Address` is a more complex structure than a simple basic (aka, primitive) value.
250198

251199
```elixir
252200
defmodule MyApp do
253-
def process_address(address) when is_binary(address) do
254-
# Do something with address...
201+
def extract_postal_code(address) when is_binary(address) do
202+
# Extract postal code with address...
203+
end
204+
205+
def fill_in_country(address) when is_binary(address) do
206+
# Fill in missing country...
255207
end
256208
end
257209
```
258210

211+
While you may receive the `address` as a string from a database, web request, or a third-party, if you find yourself frequently manipulating or extracting information from the string, it is a good indicator you should convert the address into structured data:
212+
259213
Another example of this anti-pattern is using floating numbers to model money and currency, when [richer data structures should be preferred](https://hexdocs.pm/ex_money/).
260214

261215
#### Refactoring
262216

263-
Possible solutions to this anti-pattern is to use maps or structs to model our address. The example below creates an `Address` struct, better representing this domain through a composite type. Additionally, we can modify the `process_address/1` function to accept a parameter of type `Address` instead of a *string*. With this modification, we can extract each field of this composite type individually when needed.
217+
Possible solutions to this anti-pattern is to use maps or structs to model our address. The example below creates an `Address` struct, better representing this domain through a composite type. Additionally, we introduce a `parse/1` function, that converts the string into an `Address`, which will simplify the logic of remainng functions. With this modification, we can extract each field of this composite type individually when needed.
264218

265219
```elixir
266220
defmodule Address do
@@ -270,8 +224,16 @@ end
270224

271225
```elixir
272226
defmodule MyApp do
273-
def process_address(%Address{} = address) do
274-
# Do something with address...
227+
def parse(address) when is_binary(address) do
228+
# Returns %Address{}
229+
end
230+
231+
def extract_postal_code(%Address{} = address) do
232+
# Extract postal code with address...
233+
end
234+
235+
def fill_in_country(%Address{} = address) do
236+
# Fill in missing country...
275237
end
276238
end
277239
```
@@ -342,7 +304,7 @@ Using multi-clause functions in Elixir, to group functions of the same name, is
342304

343305
A frequent example of this usage of multi-clause functions is when developers mix unrelated business logic into the same function definition. Such functions often have generic names or too broad specifications, making it difficult for maintainers and users of said functions to maintain and understand them.
344306

345-
Some developers may use documentation mechanisms such as `@doc` annotations to compensate for poor code readability, however the documentation itself may end-up full of conditionals to describe how the function behaves for each different argument combination.
307+
Some developers may use documentation mechanisms such as `@doc` annotations to compensate for poor code readability, however the documentation itself may end-up full of conditionals to describe how the function behaves for each different argument combination. This is a good indicator that the clauses are ultimately unrelated.
346308

347309
```elixir
348310
@doc """

0 commit comments

Comments
 (0)