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/design-anti-patterns.md
+56-1Lines changed: 56 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -279,4 +279,59 @@ A common practice followed by the community is to make the non-raising version t
279
279
280
280
## Using application configuration for libraries
281
281
282
-
TODO
282
+
#### Problem
283
+
284
+
The [*application environment*](https://hexdocs.pm/elixir/Application.html#module-the-application-environment) can be used to parameterize global values that can be used in an Elixir system. This mechanism can be very useful and therefore is not considered an anti-pattern by itself. However, library authors should avoid using the application environment to configure their library. The reason is exactly that the application environment is a **global** state, so there can only be a single value for each key in the environment for an application. This makes it impossible for multiple applications depending on the same library to configure the same aspect of the library in different ways.
285
+
286
+
#### Example
287
+
288
+
The `DashSplitter` module represents a library that configures the behavior of its functions through the global application environment. These configurations are concentrated in the *config/config.exs* file, shown below:
289
+
290
+
```elixir
291
+
importConfig
292
+
293
+
config :app_config,
294
+
parts:3
295
+
296
+
import_config "#{config_env()}.exs"
297
+
```
298
+
299
+
One of the functions implemented by the `DashSplitter` library is `split/1`. This function aims to separate a string received via a parameter into a certain number of parts. The character used as a separator in `split/1` is always `"-"` and the number of parts the string is split into is defined globally by the application environment. This value is retrieved by the `split/1` function by calling `Application.fetch_env!/2`, as shown next:
300
+
301
+
```elixir
302
+
defmoduleDashSplitterdo
303
+
defsplit(string) whenis_binary(string) do
304
+
parts =Application.fetch_env!(:app_config, :parts) # <= retrieve parameterized value
Due to this parameterized value used by the `DashSplitter` library, all applications dependent on it can only use the `split/1` function with identical behavior about the number of parts generated by string separation. Currently, this value is equal to 3, as we can see in the use examples shown below:
To remove this anti-pattern and make the library more adaptable and flexible, this type of configuration must be performed via parameters in function calls. The code shown below performs the refactoring of the `split/1` function by accepting [keyword lists](`Keyword`) as a new optional parameter. With this new parameter, it is possible to modify the default behavior of the function at the time of its call, allowing multiple different ways of using `split/2` within the same application:
322
+
323
+
```elixir
324
+
defmoduleDashSplitterdo
325
+
defsplit(string, opts \\ []) whenis_binary(string) andis_list(opts) do
326
+
parts =Keyword.get(opts, :parts, 2) # <= default config of parts == 2
0 commit comments