You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: lib/elixir/pages/anti-patterns/code-anti-patterns.md
+5-3Lines changed: 5 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -375,7 +375,7 @@ There are few known exceptions to this anti-pattern:
375
375
376
376
#### Problem
377
377
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.
379
379
380
380
#### Example
381
381
@@ -407,7 +407,7 @@ As can be seen in the example above, even when the key `:z` does not exist in th
407
407
408
408
#### Refactoring
409
409
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:
411
411
412
412
```elixir
413
413
defmoduleGraphicsdo
@@ -457,7 +457,7 @@ iex> Graphics.plot(point_3d)
457
457
{5, 6, nil}
458
458
```
459
459
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:
461
461
462
462
```elixir
463
463
defmodulePoint.2D do
@@ -477,6 +477,8 @@ iex> point[:x] # <= by default, struct does not support dynamic access
477
477
** (UndefinedFunctionError) ... (Point does not implement the Access behaviour)
478
478
```
479
479
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
+
480
482
#### Additional remarks
481
483
482
484
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