What is the relationship between Phoenix module names and file structure of a project?

Going through the Programming Phoenix >= 1.4 and I’m a little confused if there has to be a mapping between a module name and project file structure.

Eg in the book (ebook version) there is there instruction to create file “controllers_views_templates/listings/rumbl/lib/rumbl/accounts/accounts.ex”. I’ll expect the module for such a file to be:

defmodule Rumbl.Accounts.Accounts do

but the code sample given is this:

defmodule Rumbl.Accounts do

In the same book the file with:

defmodule Rumbl.Accounts.User do

can be found in “controllers_views_templates/listings/rumbl/lib/rumbl/accounts/user.ex” which is what I’ll expect.

Any clarifications?

Don’t worry You are not the only one to have this question.

TLDR; There is no relation between filename, directory and module name. You should adopt your own convention. In the end, module names are just atoms, as You can see with i command…

iex> i Code.Formatter
Data type
Module bytecode
Compile options
  Call Code.Formatter.module_info() to access metadata.
Raw representation
Reference modules
  Module, Atom
Implemented protocols
  IEx.Info, Inspect, String.Chars, List.Chars

But of course naming is hard.


Coming from Java, I think it’s nice to make the directory structure match the module names, at least most of the time. It helps code navigation regardless of if you are using an IDE, or clicking through links on Github, or using Vim.

1 Like