# When to use a variable vs simple value returning function

**URL:** <https://forum.elixirforum.com/t/when-to-use-a-variable-vs-simple-value-returning-function/37190>\
**Category:** Questions\
**Tags:** variable\
**Created:** [January 30, 2021, 7:06pm UTC](https://forum.elixirforum.com/t/when-to-use-a-variable-vs-simple-value-returning-function/37190 "2021-01-30T19:06:07Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![dimitarvp](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/dimitarvp/32/38664_2.png) [@dimitarvp](https://forum.elixirforum.com/u/dimitarvp)\
**Post date:** [January 30, 2021, 7:16pm UTC](https://forum.elixirforum.com/t/when-to-use-a-variable-vs-simple-value-returning-function/37190/2 "2021-01-30T19:16:05Z")

</div>

I use functions in these situations to allow myself the liberty to replace a hard-coded constant with a value fetched from a configuration provider – be it plain normal OS environment variables, various secret managers like those found in the Kubernetes ecosystem, AWS-specific secret key-value stores etc.

But, if you are very sure you won’t need to fetch the value from the outside world then you might be better off served by using a module attribute because that makes the code a bit more readable and explicit:

```elixir
defmodule KV.Router do
  @routing_table [{?a..?m, :"foo@computer-name"}, {?n..?z, :"bar@computer-name"}]

  def route(bucket, mod, fun, args) do
    # ...
    @routing_table |> use_it_here()
    # ...
  end
end

```

Putting constant values _inside_ your code is a bad idea no matter the programming language. More often than not, you’ll find yourself needing to modify it down the line and then you’ll have trouble finding where did you put it exactly (trust me, we forget where we put stuff _all the time_). In Elixir, using module attributes clearly expresses your intent as “this is a constant value that I will use one or more times in the code of the module it’s defined in”.

---

_[View the full topic](https://forum.elixirforum.com/t/when-to-use-a-variable-vs-simple-value-returning-function/37190)._
