[erlang-questions] Why should you ever use atoms?

Joe Armstrong erlang@REDACTED
Sun Oct 11 20:01:08 CEST 2015

Using atoms is fine. The dangerous primitive is list_to_atom(X)
if your program *never* calls list_to_atom then don't worry.

If you call list_to_atom a lot then worry. Note "a lot" means tens of
millions of times on a machine with GBytes if memory. There is your
friend list_to_existing_atom(X) which exits if X represents a non
existent atom.

You can, or course, use strings or binaries instead of atoms but your
programs will run slower and you'll put more pressure on the garbage

Rule of thumb: don't worry, use atoms until you run into problems.
Write your code in such a way that swapping from atoms to binaries is
no big deal.

This is actually about structuring your code - if you use atoms and
get problems and have to edit for a hour or so to fix the problem then
this is no big deal - if this involves weeks of work then you have a
bad design.

Make it work - them make it fast/stable/whatever


On Sat, Oct 10, 2015 at 5:46 PM, Thomas Gebert <thomas@REDACTED> wrote:
> I know this is probably kind of a newbie question, but I figured this would
> be the place to ask it: if atoms aren't garbage collected, why should I use
> them? For example, it's a common pattern to have something like:
>     myFunction({user, "tombert","eats pizza"}) -> %% do something
> When I could easily do something like:
>     myFunction({"user", "tombert", "eats pizza"}) -> %% do something
> ----
> I could be way off here, but wouldn't the string be garbage collected?  Is
> there a benefit to atoms that I'm missing?
> -Tombert.
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions

More information about the erlang-questions mailing list