Is there a better place to get a library I wrote reviewed? I’m trying to write better code, but I would like someone to give me some input on how I can improve what I’ve written.
It’s a heroku-like name generator (i.e. “hairy-monkey-123”), which I’m aware is basically as overdone and simple as “Hello, world”, but my goal was to learn to write and deploy a hex lib. The other twist to my library is that you can generate predictable/consistent names, so that instead of generating a random name each time, if you wanted to generate a name for a Post/User/etc. for your site, you could generate a name, and as long as you pass the same two integers to the lib (i.e. ID, and inserted_at as an int), it will always return the same name.
If there’s a better place to post, please let me know. Thanks in advance.
First thing that stands out to me: use @moduledoc false at the top of Adjective and Noun unless you intend for users to be calling those modules directly.
I see Noun and Adjective are basically data in code. In my opinion, there are more optimal formats for data. I would probably create a priv/nouns.txt and priv/adjectives.txt with one word per line. Then I’d read the files at compile time to set a module attribute that could be used in the functions. You can use @external_resource to tell Elixir that when one of those files change, it needs to recompile your module.
What would be the best way to do this? The only way I was able to get it to compile was by creating a macro in a separate module, and using that to define the noun/adjective functions, but that broke all my module attributes, aswell as the moduledocs that interpolated using functions/module attrs. Is there a cleaner way to do what you suggest?
Enum.at might end up being quite slow for a name generator (I assume you’d have to run it often), so I’d make a lookup table with functions. Try checking out my suggestions on https://github.com/heresydev/email_guard/pull/1, most of them would apply to your project as well, I think.
@nouns[3] or any other form of a lookup in a map is usually several times slower than a function head pattern match (at least in my benchmarks with benchee).