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.

Last Post!

Adzz

Adzz

I think the main thing is to make sure you don’t lose the ability to search (grep) for the full module name and have all the places where it is used to show up:

defmodule My.Module.Name do
end

defmodule My.OtherModule.Name do
end

defmodule Some.Other do
  # This is a bad alias because if I search for `My.Module.Name` this wont show up
  alias My.Module
  # This is fine, but not always neccessary - you can use the fully qualified name
  alias My.Module.Other
  # In the case of conflicting final names, this is preferred, because I can still search 
  # for `My.OtherModule.Name` and have this show up.
  alias My.OtherModule.Name, as: OtherModuleName
end

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36689 110
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
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31525 112
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44608 311
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

We're in Beta

About us Mission Statement