joaoevangelista

joaoevangelista

How to refactor Elixir with confidence?

Is there some tool that can help us refactoring our code base so we don’t leave undefined functions somewhere in the depths of our code?
Or if anyone has some tips on how to optimize it. Since we don’t have types and a compiler to check on those things, and I’m kind lost on how to approach it.

Thanks in advance!

Most Liked

belaustegui

belaustegui

I can not stress the usefulness of mix xref enough. I use it alout to find unused functions and modules. mix xref callers MODULE_OR_FUNCTION provides very useful information about which file and from which line is calling the given module or function.

Apart from the mandatory tests, I would also recommend using credo to get insights about possible improvements, code duplication, function nesting, etc.

If you have a CI setup, I would advice to use mix compile --warnings-as-errors to cause any builds with comilation errors to fail and to include mix format --check-formatted to ensure that the source is formatted properly.
We can agree or disagree with Elixir default formatting settings, but having a common formatting accross all the source files makes them much more easier to work with.

MalloZup

MalloZup

@joaoevangelista
I would vote for:

  1. use ex_unit tests for testing your code. ( then you can refactor easy)
  2. add coverage tool: GitHub - parroty/excoveralls: Coverage report tool for Elixir with coveralls.io integration. · GitHub for spotting uncovered zones.
    ( to be safe: codecoverage run code but it’s up to human to have the right tests :smiley: meaning you can run code without really testing it sometimes. All coverage tools need also to be reviewed by humans, but they are incredibly awesome )
  3. use Typespecs and dialyzer for automating ( see book ref at the end for this)
  4. for doc coverage : GitHub - rrrene/inch_ex: Provides a Mix task that gives you hints where to improve your inline docs. · GitHub
  5. from my personal pov and experience:
    when do unit_test focus on public interface tests. Don’t test private function to much because this will change . The public function should not change much, so you need to test this good, and if you test public you test private function that compose your public function/module.
    this is needed when you want to spare cost maintenance of tests.

If you do this i think you can refractor with confidence

I would also suggest you the great book Search , it adress your question and more . ( i’m currently reading and i really like it :love_letter:)

svarlet

svarlet

Mix xref may help a bit too

Where Next?

Popular in Questions Top

_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
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
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New

Other popular topics Top

axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 48475 226
New
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
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
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
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
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

We're in Beta

About us Mission Statement