4 ms·
A few more thoughts from my 2c coming based on my experience writing a lot of code in Elixir: 1 - avoid if else {} blocks, use `cond do .. end` blocks instead,
by princevegeta89 5y ago
A few more thoughts from my 2c coming based on my experience writing a lot of code in Elixir:
1 - avoid if else {} blocks, use `cond do .. end` blocks instead, they're more readable
2 - avoid importing modules, but rather use them directly wherever possible, this will help when refactoring code or to find all occurrences of a particular Module.method()
3 - when specifying keyword list options, explicitly supply [ ] in the arguments, example: Abc.method(a: 2, b: 3) may be clearly written as Abc.method([a: 2, b: 3]) so we always know it uses a keyword list and not map
4 - Use a file `.iex.exs` to specify all your aliases and macros you may need in your Elixir shell.
Helps a lot while you're playgrounding in the IEx shell. In my case, I have aliased all my Context modules and Model modules
5 - Pattern match everywhere. This is one of the powers of this language. Make your methods follow the {:ok, data} or {:error, message} pattern and it's already very maintainable.
- devoutsalsa 5y agoI've been using Elixir for over 5 years and didn't know about .iex.exs. Nice!
- innocentoldguy 5y agoHere's a handy thing you can do with .iex.exs: Have you ever tried to debug something in iex, but database calls and other log messages keep stomping all over your prompt? You can stop them temporarily calling the function Logger.remove_backend(:console) You can start the log message again by typing Logger.add_backend(:console) That's a lot of typing though. Here's where .iex.exs comes in handy. You can include the following code in your .iex.exs file: defmodule Clog do def stop do Logger.remove_backend(:console) end def start do Logger.add_backend(:console) end end and then from within your iex session, type Clog.stop to stop the log message flow and Clog.start to start it up again. You can call your module whatever you'd like. I just called mine "Clog" because the log messages were always clogging up my console. I hope this helps.
- dnautics 5y agothis is even better: if function_exported?(Mix, :__info__, 1) and Mix.env() == :dev do Logger.configure_backend(:console, device: Process.group_leader()) end It makes logger work like IO; where if it spits it out it doesn't disrupt the prompt. Don't run it in prod, though.