How should I name related modules with the least repetition possible?

Not sure if there’s an official answer for this in Elixir land, but it’s not the first time I’ve encountered this. I want to name a bunch of related components that are each a part of some bigger component. Let’s call the big one Session. What do you think of these three examples, and can you think of another way to do it?

# Inside of /lib/app_web/components/session/
AppWeb.Session.SessionStartedComponent
AppWeb.Session.SessionPendingComponent

or

# Inside of /lib/app_web/components/session/
AppWeb.Session.StartedComponent
AppWeb.Session.PendingComponent

or without the intermediate module name part, but still in own directory

# Inside of /lib/app_web/components/session/
AppWeb.SessionStartedComponent
AppWeb.SessionPendingComponent

What is the preference and what is most standard in the Elixir world? I know that directory names usually reflect the segments of a module name, but is it obligatory?

Not obligatory, no.

When it comes to this stuff Phoenix is very un-opinionated. There aren’t any real technical merits from one of these over another (other than simpler aliases with Session. if you’re into the {} syntax) and no community standard—it’s up to whatever you like best!

Another option:

AppWeb.Components.Session.Pending

or even:

AppWeb.SessionComponents.Pending

I like option 2, unless you also have

AppWeb.PostDrafts.StartedComponent
AppWeb.Countdown.StartedComponent
...etc

:sweat_smile: then probably option 1 would be better imo

(not a big fan of diverging folder structure from module namespacing)

1 Like

I used to feel this way and quite strongly so. But due to BEAM not caring about file names and not having to import based on paths, I’ve actually found it super liberating to keep the file system for organization and using different naming schemes for some modules. It means I don’t need to alias so much which I try and avoid. It’s not to say that I don’t alias, but my default is to not do it until it becomes a readability problem.

1 Like