gerbal

gerbal

Ordering Use, Import, Alias, Require?

Is there a recommended ordering of require, use, import, and alias? Does the style guide have anything to say about the preferred ordering of special forms?

Marked As Solved

moxley

moxley

Note that the order of some items have been changed:

  1. @moduledoc
  2. @behaviour
  3. use
  4. import
  5. require
  6. alias
  7. @module_attribute
  8. defstruct
  9. @type
  10. @callback
  11. @macrocallback
  12. @optional_callbacks
  13. defmacro, defmodule, defguard, def, etc.
18
Post #3

Also Liked

gerbal

gerbal

Nevermind, the styleguide is pretty explicit about this: GitHub - christopheradams/elixir_style_guide: A community driven style guide for Elixir

List module attributes and directives in the following order:

  • @moduledoc
  • @behaviour
  • use
  • import
  • alias
  • require
  • defstruct
  • @type
  • @module_attribute
  • @callback
  • @macrocallback
  • @optional_callbacks

Add a blank line between each grouping, and sort the terms (like module names) alphabetically.

16
Post #2
LostKobrakai

LostKobrakai

It won‘t ever do that. One premise of the core formatter is that it won‘t change the underlying AST by applying formatting. That ensures that formatting will never introduce a change in behavior of the code and therefore won‘t introduce unintended bugs. Reordering expressions means changing the AST however.

lud

lud

I wonder if anyone is using the following:

  • alias
  • import
  • require
  • use

So you can just sort the whole thing alphabetically, without empty lines.

An only after that module attributes, including @moduledoc, as in the doc I found quite often useful to be able to reuse another attribute, or even call a remote function.

NobbZ

NobbZ

At my place we do not have a strict order, as long as they are grouped accordingly.

Though due to the way our set up is structured, we often have calls to use/2 in a certain kind of modules that set up “dependencies” to other modules of the same kind input and output events. Those uses were really ugly to write with the regular “use first” rule, and we eventually got into the habbit to generally have aliases first, uses second. require just follows that. And we generally avoid import.

sodapopcan

sodapopcan

Depending on what they are and the “type” of module, I put them close to where they are first used. If there is a single errant function that needs a macro, I require or import it right in that function.

Where Next?

Popular in Questions Top

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
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
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
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
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
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
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

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
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
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
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
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
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
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New

We're in Beta

About us Mission Statement