Fl4m3Ph03n1x

Fl4m3Ph03n1x

Nested modules suggestion?

Background

Recently I decided to do a Mix Task to run some script files in the project. Being my first Mix Task, I followed a tutorial and got the following structure:

defmodule Mix.Tasks.Deploy do
  use Mix.Task
  

  @shortdoc "Compiles the app for PROD and starts a Rolling Release."
  def run(_) do
    #do really awesome stuff here
end

Problem

We are using credo and one of the warnings it gives me is the following:

┃ [D] :down_right_arrow: Nested modules could be aliased at the top of the invoking module.
┃ lib/mix/tasks/deploy.ex:21:19 #(Mix.Tasks.Deploy.run)

Now, I checked the project dos referring to this error:

And from my understanding I take it I should use a alias somewhere, but I don’t see how.

Question

I can I fix the problem and make the warning go away?

Marked As Solved

Matt

Matt

It sounds like you are calling a function like this:

defmodule Mix.Tasks.Deploy do
  use Mix.Task

  def run(_) do
    # doing really cool stuff
    # ...

    Comeonin.Argon2.hashpwsalt("some password")

    # ...
    # more cool stuff
  end
end

What credo is suggesting is to do something like this:

defmodule Mix.Tasks.Deploy do
  use Mix.Task
  alias Comeonin.Argon2

  def run(_) do
    # best programming you have ever seen
    # ...

    Argon2.hashpwsalt("bestpassword")

    # ...
    # more cool programming
  end
end

So, credo is complaining that you are calling a function with nested module names, Comeonin.Argon2.hashpwsalt/1, for example. It wants you to call a function with a singular module name, like Argon2.hashpwsalt/1. So you can use alias for this purpose. The cool think about alias is that it is lexically scoped. This means you can use it within a function :exploding_head: if you need.

Also Liked

axelson

axelson

Scenic Core Team

Although do note that the “[D] :arrow_lower_right:” part of the “warning” indicates that is is a very low severity “issue”, really more of a suggestion. I don’t think it’s idiomatic Elixir code to alias every single module you reference. In fact I tend to only alias modules that are used very often in the module, leaving a full module reference to indicate that longer module reference is not used/tied as deeply with the rest of the containing module (sorry this is a little hard to follow).

Matt

Matt

I think your reply is meant for @ Fl4m3Ph03n1x?

I do the same, I actually rarely use alias. In fact, I tend not to like using alias often, because I like to know the origin of the functions I am calling. Right or wrong, that is just personal preference on my part though.

OvermindDL1

OvermindDL1

I alias out parts that are ‘obvious’, like the main namespace at the very least, sometimes down to the specific function call (import) if perfectly obvious, otherwise I keep a name mapping or 2 or 3 worth of dots.

Where Next?

Popular in Questions Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2, I would like to get: ...
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
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
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
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43622 214
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New

We're in Beta

About us Mission Statement