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.
19
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 · GitHub

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.

Last Post!

grossvogel

grossvogel

Folks who would like a more aggressive tool for this might want to look into Styler. It is opinionated and explicitly doesn’t promise that it’ll never break your code, but IME it usually doesn’t and I really do value the automated fix-ups.

As a simple example, sometimes I start writing a pipeline and then realize there’s only one function in it… If I save the file with auto-format enabled in my editor, Styler will automatically un-pipeline the call.

Where Next?

Popular in Questions Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
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
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
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

Other popular topics Top

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
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
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 49134 226
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
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 44167 214
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