I note that if I write some lines like
:mnesia.create_schema([node()])
:mnesia.start()
:mnesia.create_table(TableName, [attributes: [:attribute_one, :attribute_two]])
This somehow works even though there is no module or variable of the name TableName
anywhere before here. It is possible to continue to use this TableName
just as written and access it this way going forward.
Similarly if you just type RandomWord
into iex, nothing bad happens, it repeats it like an atom.
I see this was discussed a bit here, and it was stated that Elixir is treating the CapitalWord as an “Alias”
The documentation on Aliases suggests:
Aliases are constructs that expand to atoms at compile-time. The alias
String
expands to the atom:"Elixir.String"
. Aliases must start with an ASCII uppercase character which may be followed by any ASCII letter, number, or underscore. Non-ASCII characters are not supported in aliases.
Multiple aliases can be joined with
.
, such asMyApp.String
, and it expands to the atom:"Elixir.MyApp.String"
. The dot is effectively part of the name but it can also be used for composition. If you definealias MyApp.Example, as: Example
in your code, thenExample
will always expand to:"Elixir.MyApp.Example"
andExample.String
will expand to:"Elixir.MyApp.Example.String"
.
Elixir’s naming conventions recommend aliases to be in
CamelCase
format.
So my presumption is writing TableName
then becomes :TableName
in compilation.
But I also suspect this is not good code design and one should not use Aliases unless there is a specific purpose for it. Would you then agree?
My suspicion is you should call it either :table_name
or :TableName
and be clear about it, since you want an atom here clearly and nothing else.
Is there any particular naming convention that would be suggested for something like a table name in this case? Ie. Be explicit about the :
or not? CamelCase or just stick to snake_case like every variable?
My inclination is :table_name
.
ElixirSchool surprisingly recommends this alias method for table naming with Person
in their example. But I am not sure it is “correct” here.
Thanks for any thoughts.