Skip to content

Commit 35619fd

Browse files
committed
Clarify "known keys" and struct usage
1 parent 7cfe015 commit 35619fd

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ There are few known exceptions to this anti-pattern:
375375

376376
#### Problem
377377

378-
In Elixir, it is possible to access values from `Map`s, which are key-value data structures, either statically or dynamically. When the keys are known upfront, they must be accessed using the `map.key` notation, which asserts the key exists. If `map[:key]` is used and the informed key does not exist, `nil` is returned. This return can be confusing and does not allow developers to conclude whether the key is non-existent in the map or just has a bound `nil` value. In this way, this anti-pattern may cause bugs in the code.
378+
In Elixir, it is possible to access values from `Map`s, which are key-value data structures, either statically or dynamically. When a key is expected to exist in the map, it must be accessed using the `map.key` notation, which asserts the key exists. If `map[:key]` is used and the informed key does not exist, `nil` is returned. This return can be confusing and does not allow developers to conclude whether the key is non-existent in the map or just has a bound `nil` value. In this way, this anti-pattern may cause bugs in the code.
379379

380380
#### Example
381381

@@ -407,7 +407,7 @@ As can be seen in the example above, even when the key `:z` does not exist in th
407407

408408
#### Refactoring
409409

410-
To remove this anti-pattern, whenever accessing a known key of `Atom` type, replace the dynamic `map[:key]` syntax by the static `map.key` notation. This way, when a non-existent key is accessed, Elixir raises an error immediately, allowing developers to find bugs faster. The next code illustrates the refactoring of `plot/1`, removing this anti-pattern:
410+
To remove this anti-pattern, whenever accessing an existing key of `Atom` type in the map, replace the dynamic `map[:key]` syntax by the static `map.key` notation. This way, when a non-existent key is accessed, Elixir raises an error immediately, allowing developers to find bugs faster. The next code illustrates the refactoring of `plot/1`, removing this anti-pattern:
411411

412412
```elixir
413413
defmodule Graphics do
@@ -457,7 +457,7 @@ iex> Graphics.plot(point_3d)
457457
{5, 6, nil}
458458
```
459459

460-
Another alternative is to use structs. By default, structs only support static access to its fields, promoting cleaner patterns:
460+
Another alternative is to use structs. By default, structs only support static access to its fields:
461461

462462
```elixir
463463
defmodule Point.2D do
@@ -477,6 +477,8 @@ iex> point[:x] # <= by default, struct does not support dynamic access
477477
** (UndefinedFunctionError) ... (Point does not implement the Access behaviour)
478478
```
479479

480+
Generally speaking, structs are useful when sharing data structures across modules, at the cost of adding a compile time dependency between these modules. If module `A` uses a struct defined in module `B`, `A` must be recompiled if the fields in the struct `B` change.
481+
480482
#### Additional remarks
481483

482484
This anti-pattern was formerly known as [Accessing non-existent map/struct fields](https://github.com/lucasvegi/Elixir-Code-Smells#accessing-non-existent-mapstruct-fields).

0 commit comments

Comments
 (0)