Avoid namespace trespassing in library modules
mainWhen authoring a package or library, avoid defining modules inside the namespace of another library. Because the Erlang VM can only load one instance of a module at a time, defining a module like Plug.Auth for a package named :plug_auth can cause fatal conflicts if the Plug library later introduces its own Plug.Auth module.
Best Practice:
Always use your library's name as a prefix for all modules. For example, a package named :plug_auth should define modules as PlugAuth.User or PlugAuth.SubModule instead of Plug.User.
Exceptions:
- Protocol implementations: These are intentionally defined under the protocol namespace (e.g., using
Kernel.defimpl/2). - Mix tasks: Custom tasks are defined under the
Mix.Tasksnamespace (e.g.,Mix.Tasks.MyTask). - Maintainer ownership: If you maintain both the parent namespace and the extension, you may define modules within that namespace, but you assume responsibility for managing future conflicts.
# Bad: Trespassing into Plug namespace
defmodule Plug.Auth do
# ...
end
# Good: Using the library's own namespace
defmodule PlugAuth do
# ...
end