Skip to content

Instantly share code, notes, and snippets.

@susilolab
Created July 3, 2020 14:51
Show Gist options
  • Select an option

  • Save susilolab/01b4f7e6ebff7822015ac398b70a653d to your computer and use it in GitHub Desktop.

Select an option

Save susilolab/01b4f7e6ebff7822015ac398b70a653d to your computer and use it in GitHub Desktop.
Two ways of dealing with modules
# Option 1
# Imagine that the implementations of these three functions span ~100 LOC each, including
# private functions
defmodule Users do
def create(params) do
# this and all private functions for this are like 100 LOC
end
def update(user, params) do
# this and all private functions for this are like 100 LOC
end
def get_posts_for(user) do
# this and all private functions for this are like 100 LOC
end
end
# Option 2
# Instead of 1 300 LOC module, we now have 3 100 LOC modules, and we just delegate to
# the smaller modules in our original module.
defmodule Users do
defdelegate create(params), to: Users.Create
defdelegate update(user, params), to: Users.Update
defdelegate get_posts_for(user), to: Users.GetPostsFor
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment