Elixir naming convention in Phoenix boilerplate


Hi there…

Just wanting to clarify my understanding of the Elixir naming convention after I was confused by some of the boilerplate generated by Phoenix.


mix phx.gen.presence



When looking at this file it is “namespaced” as:

defmodule HelloWeb.Presence

Shouldn’t it be?

defmodule HelloWeb.Channels.Presence

I thought the convention was to use the folder names as part of the module name. Maybe not?


This is the case for the whole myapp_web folder. Take a look at Views and Controllers, they’re all namespaced like that.



I can see that… but I thought the convention was to also use the folder names to form the module name. Or I am making up my own stories on that one? I have a vague memory of when I started learning elixir that this was the case.


This is indeed the general convention. However, it is sometimes useful to omit intermediary namespaces for the sake of brevity. I see this a lot when a folder is used for grouping modules that are similar in function, as this is not a runtime code organization construct so much as a source code location abstraction.

In particular when the module is already suffixed with the type, having the container name adds no information–like Web.Controllers.FooController for a lib/web/controllers/foo_controller.ex file. So long as the module name is unlikely to conflict with others in a higher namespace it can be much more practical to use a shorter form.


Thanks - makes sense…