Fl4m3Ph03n1x

Fl4m3Ph03n1x

Why are module names in Elixir UpperCamelCase, while the file names are snake_case?

Background

I have recently found an old article about Elixir, where the author explains some conventions about Elixir, and in specific, naming and structural conventions for Phoenix projects.

In this article the author mentions:

Module names in Elixir follow the UpperCamelCase convention. (…) and the file name has the same name as the module but uses snake_case as a convention. Why? I don’t know.

(emphasis added by me)

The author mentions the possibility of a technical reason for this. I am unaware of what technical reason could exist, and my belief is that this was mostly a style decision (needs confirmation).

Question

So the question here is the title:

Why are module names in Elixir UpperCamelCase, while the file names are snake_case?

The article is from 2022, so there is a good chance someone (maybe even the author) figured this one out. But I couldn’t find anything regarding this, and curiosity did get the best of me.

  • Why do you think this is the case?
  • What technical reasoning do you think could be behind this?

Marked As Solved

al2o3cr

al2o3cr

Not all filesystems are case-sensitive, so trying to camel-case there will give you files with unwieldylongnameswithoutanypunctuation

I suspect that constraint in Elixir may have been more about “why are Ruby files named like this” and then migrated over, but I don’t have any evidence for that.

Also Liked

sodapopcan

sodapopcan

It’s just convention. This is how Ruby does it which of course inspired a lot of Elixir conventions. Also, Erlang modules names are conventionally snake case (and at least must start with a lowercase letter). There is no technical reason, but one thing to keep in mind is that file != module in Elixir, ie, you can have multiple modules in a single file. I often have some files that aren’t named after any of the contained modules, like errors.ex. For me (and this is just me), using camel case would suggest that the file is the module whereas snake case more correctly suggests there is no such correlation

gregvaughn

gregvaughn

Hehe. I once tweeted some “evil” Elixir in which I did defmodule :false do. Alas, someone reported it as a bug and it got fixed :grin:

edit: actually, I don’t think I needed the colon, but … details

sodapopcan

sodapopcan

Nitpick for clarity: module names are aliases which is why they must start with an uppcase letter. And to nitpick myself, of course you can use straight up atoms as well, ie defmodule :foo, do: (). But yes, always atoms.

Onor.io

Onor.io

Excellent point. Hairsplitting may be unwelcome in other arenas but in software development it’s essential! :slight_smile:

sodapopcan

sodapopcan

This is technically what you are doing when you do defmodule Foo do. Foo is like an “auto alias” as it expands to :"Elixir.Foo" so you’re really doing:

iex(1)> defmodule :"Elixir.Foo", do: (def hi, do: "hi")
{:module, Foo,
 <<70, 79, 82, 49, 0, 0, 5, 68, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 164,
   0, 0, 0, 17, 10, 69, 108, 105, 120, 105, 114, 46, 70, 111, 111, 8, 95, 95,
   105, 110, 102, 111, 95, 95, 10, 97, 116, ...>>, {:hi, 0}}
iex(2)> Foo.hi()
"hi"

Even though it’s pretty simple, I still get confused and I find it much easier to make the distinction when talking about them. And sometimes the difference is meaningful when a macro expects an (unexpanded) alias, like Phoenix.Router.scope.

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
Kagamiiiii
Student &amp; New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
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
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New

Other popular topics Top

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 30048 115
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
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 42633 214
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement