WestKeys

WestKeys

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

Showing Posts 1 to 10

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

WestKeys

WestKeys OP

Awesome reply, thanks

Could you please elaborate on this? I come from jsland and in most codebases it appears the convention is to stick all variables at the top of the scope.

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.

WestKeys

WestKeys OP

Wonderful, no yeah I get it. So in this particular case module attributes would be equivalent to what I was asking

dimitarvp

dimitarvp

Yes, correct.

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?

LostKobrakai

LostKobrakai

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

dimitarvp

dimitarvp

Yes, that’s what I am saying. Lack of description of what the inline value is supposed to be is a very common problem in most codebases I ever reviewed and participated in.

davearonson

davearonson

Ah, yup. “Magic Numbers”, some folks call it.

John-Goff

John-Goff

Something that noone has mentioned yet, is that module attributes are only available in the module they are defined in. So if I have

defmodule A do
  @pi 3.14159
end

And I have

defmodule B do
  def pi, do: 3.14159 
end

Then I can do this:

B.pi() # 3.14159 

But I cannot get the constant value I’ve defined in A. Sometimes I’ll actually do something like

defmodule Example do
  @constant "..."
  def constant, do: @constant 
end

The reason for this is inside the module it’s defined in I’d use it as a module attribute to quickly tell myself that this is a constant value. And the public function tells me that this constant is used elsewhere in the code, not just this module.

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
psy-q
I’m trying to set up Emacs with elixir-ls via lsp-mode and credo via Flycheck. This should mostly be preconfigured as Flycheck picks up c...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews