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
+1-1
Original file line number
Diff line number
Diff line change
@@ -299,7 +299,7 @@ There are few known exceptions to this anti-pattern:
299
299
300
300
*[Protocol implementations](`Kernel.defimpl/2`) are, by design, defined under the protocol namespace
301
301
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`
303
303
304
304
* 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
Copy file name to clipboardExpand all lines: lib/elixir/pages/anti-patterns/design-anti-patterns.md
+22-60
Original file line number
Diff line number
Diff line change
@@ -126,7 +126,7 @@ Remember booleans are internally represented as atoms. Therefore there is no per
126
126
127
127
#### Problem
128
128
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.
130
130
131
131
#### Example
132
132
@@ -186,81 +186,35 @@ end
186
186
187
187
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`.
188
188
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
-
defmoduleOrderdo
201
-
# Some functions...
202
-
203
-
defcalculate_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
-
defmoduleOrderItemdo
222
-
deffind_item(id)
223
-
deffind_discount(item)
224
-
225
-
defcalculate_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
-
unlessis_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
-
241
189
## Primitive obsession
242
190
243
191
#### Problem
244
192
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.
246
194
247
195
#### Example
248
196
249
197
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.
250
198
251
199
```elixir
252
200
defmoduleMyAppdo
253
-
defprocess_address(address) whenis_binary(address) do
254
-
# Do something with address...
201
+
defextract_postal_code(address) whenis_binary(address) do
202
+
# Extract postal code with address...
203
+
end
204
+
205
+
deffill_in_country(address) whenis_binary(address) do
206
+
# Fill in missing country...
255
207
end
256
208
end
257
209
```
258
210
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
+
259
213
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/).
260
214
261
215
#### Refactoring
262
216
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.
264
218
265
219
```elixir
266
220
defmoduleAddressdo
@@ -270,8 +224,16 @@ end
270
224
271
225
```elixir
272
226
defmoduleMyAppdo
273
-
defprocess_address(%Address{} = address) do
274
-
# Do something with address...
227
+
defparse(address) whenis_binary(address) do
228
+
# Returns %Address{}
229
+
end
230
+
231
+
defextract_postal_code(%Address{} = address) do
232
+
# Extract postal code with address...
233
+
end
234
+
235
+
deffill_in_country(%Address{} = address) do
236
+
# Fill in missing country...
275
237
end
276
238
end
277
239
```
@@ -342,7 +304,7 @@ Using multi-clause functions in Elixir, to group functions of the same name, is
342
304
343
305
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.
344
306
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.
0 commit comments