dangdennis

dangdennis

What’s your 80/20 technique for maintaining a healthy Elixir codebase?

Hey, Elixir novice here coming from Typescript, Go, Ocaml.

What are your favorite techniques to get the most of your compile time checks, code readability and maintainability, and even testing strategies?

I just read this https://ulisses.dev/elixir/2020/02/19/elixir-style-for-maintanability.html, and I actually hadn’t thought to do more pattern matching on structs. Noteworthy to me because the code generated for Ecto’s CRUD functions often just pass along a generic map as the sole argument for parameters. Looking back at my project now, it would’ve served me better if I had used the specific struct as the argument.

Most Liked

zorn

zorn

I just posted a blog post on how I scope my use of typespecs, trying to find a a type safe balance while enjoying the dynamic nature of Elixir:

I also am really starting to fall into a groove with pattern matching and use it more and more – specifically I like making sure the variables I’m binding values into are the only ones required for a test or function. Trying not to make too many assumptions about the data (so it can hopefully evovle without breaking things).

Other final note, is I’m being much more mindful of how entities cross context boundaries. Still finding my way with LARGE code bases, but I feel like I’m starting to ask the right questions.

KristerV

KristerV

my number one rule is to just go back tomorrow and read your code. Is there any doubt in your mind that you’ll enjoy reading this code in 2 years? Or worse yet - is there a remote possibility that this code is clear only because of memory?

Other than that this article has a few nice points: http://blog.cretaria.com/posts/erlang-beauty.html

my favs are:

  • keep functions the size of 7 lines
  • turn case statements into functions
  • rename stuff after you’re done with the code
harmon25

harmon25

Maybe something to do with this: The Magical Number Seven, Plus or Minus Two - Wikipedia

I would imagine they mean 7 operations/assignments and for example a list that is split over n lines (for formatting sake) would just count as 1. Also comments and line breaks would not count, those are just there for context/formatting.

If your function is doing more than 7 things/operations inside of it, it becomes harder to keep all that in your head at once, and thus more prone to bugs…

Exadra37

Exadra37

Elixir has guard clauses, that I like to use heavily in conjunction with pattern matching:

defp _todo_hash(%{
          title: title, # pattern matching
          user_uid: user_uid,
          date: date,
        } = _attrs,
        action
      )
    when is_binary(title) # guard clauses
    and  byte_size(title) > 0
    and  is_binary(user_uid)
    and  byte_size(user_uid) === 64
    and  is_binary(date)
    and  byte_size(date) === 10
  do
  # your code here
end

Now that I discovered the Domo library I am using it to have typed structs and then pattern match on them:

defmodule Tasks.Todos.Types.Event do

  # @link https://hexdocs.pm/domo/Domo.html
  use Domo

  typedstruct do
    field :type, :todo | :backlog
    field :target, :todo | :backlog | :all
    field :action, :add | :update | :move | :duplicate | :delete
    field :origin, atom()
    field :broadcast_topics, list(), default: []
    field :context, map(), default: %{}
  end

end

that are then used like this:

def broadcast_change(
  {:ok, data} = result, 
  %Tasks.Todos.Types.Event{} = event
) do
  # your code here
do

Where Next?

Popular in Questions Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New

We're in Beta

About us Mission Statement