From d9e98b66b3d0600ef040f890eaf0aa07561d6802 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sat, 23 Sep 2023 14:14:33 +0200 Subject: [PATCH] Apply suggestions from code review --- .../anti-patterns/design-anti-patterns.md | 44 +++++-------------- 1 file changed, 11 insertions(+), 33 deletions(-) diff --git a/lib/elixir/pages/anti-patterns/design-anti-patterns.md b/lib/elixir/pages/anti-patterns/design-anti-patterns.md index 1c8d7dde2c7..419103bb8cf 100644 --- a/lib/elixir/pages/anti-patterns/design-anti-patterns.md +++ b/lib/elixir/pages/anti-patterns/design-anti-patterns.md @@ -209,7 +209,7 @@ end #### Problem -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. +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. #### Example @@ -222,48 +222,24 @@ defmodule Order do def calculate_total_item(id) do item = OrderItem.find_item(id) total = (item.price + item.taxes) * item.amount - if discount = OrderItem.find_discount(item) do - total - total * discount - else - total - end - end -end -``` -```elixir -defmodule OrderItem do - def find_item(id) do - ... - %{price: ..., taxes: ..., amount: ...} - end - - def find_discount(item) do - ... + if discount = OrderItem.find_discount(item) do + total - total * discount + else + total + end end end ``` #### Refactoring -To remove this anti-pattern we can move `calculate_total_item/1` to `OrderItem`. Thus the `Order` module could become more cohesive and the coupling would decrease. - -```elixir -defmodule Order do - # Some functions... -end -``` +To remove this anti-pattern we can move `calculate_total_item/1` to `OrderItem`, decreasing coupling: ```elixir defmodule OrderItem do - def find_item(id) do - ... - %{price: ..., taxes: ..., amount: ...} - end - - def find_discount(item) do - ... - end + def find_item(id) + def find_discount(item) def calculate_total_item(id) do # <= function moved from Order! item = find_item(id) @@ -279,6 +255,8 @@ defmodule OrderItem do end ``` +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 change, and your only option is to define an additional module to augment the third-party module. + ## Excessive side-effects TODO