WestKeys

WestKeys

When to use a variable vs simple value returning function

Why would we use

def table do
  [{?a..?m, :"foo@computer-name"}, {?n..?z, :"bar@computer-name"}]
end

Rather than

table = [{?a..?m, :"foo@computer-name"}, {?n..?z, :"bar@computer-name"}]

In this example

defmodule KV.Router do
  @doc """
  Dispatch the given `mod`, `fun`, `args` request
  to the appropriate node based on the `bucket`.
  """
  def route(bucket, mod, fun, args) do
    # Get the first byte of the binary
    first = :binary.first(bucket)

    # Try to find an entry in the table() or raise
    entry =
      Enum.find(table(), fn {enum, _node} ->
        first in enum
      end) || no_entry_error(bucket)

    # If the entry node is the current node
    if elem(entry, 1) == node() do
      apply(mod, fun, args)
    else
      {KV.RouterTasks, elem(entry, 1)}
      |> Task.Supervisor.async(KV.Router, :route, [bucket, mod, fun, args])
      |> Task.await()
    end
  end

  defp no_entry_error(bucket) do
    raise "could not find entry for #{inspect bucket} in table #{inspect table()}"
  end

  @doc """
  The routing table.
  """
  def table do
    # Replace computer-name with your local machine name
    [{?a..?m, :"foo@computer-name"}, {?n..?z, :"bar@computer-name"}]
  end
end

Taken from docs

Marked As Solved

dimitarvp

dimitarvp

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:

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”.

Also Liked

LostKobrakai

LostKobrakai

There’s :math.pi() in erlang.

dimitarvp

dimitarvp

Well, look at your route function above. The table() call blends in perfectly with everything else. Now turn that into an inline variable. Imagine you work actively on something else for 3 months and then come back to this code with the requirement to make that table configurable. Would you immediately remember where you put the hard-coded variable?

Yeah, that’s a bit better, and closer to what I showed you. It’s a good compromise.

davearonson

davearonson

To clarify what I think you mean, inline inside the code with no name to them, such as c = 6.28318530718 * r.

As opposed to putting up top @PI 3.14159265359 (dunno offhand if Elixir provides this but anyway, it could be considered “putting a constant value inside your code”, as it’s in the module) and in the code, c = 2 * @PI * r, or better yet of course circumference = 2 * @PI * radius. That would be perfectly cromulent, in fact generally the recommended way to go.

Would that be a fair interpretation of what you’re trying to say?

Where Next?

Popular in Questions Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New

Other popular topics Top

lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 47930 226
New
Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 126479 1222
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement