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
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
@joaoevangelista
I would vote for:
- use ex_unit tests for testing your code. ( then you can refactor easy)
- add coverage tool: https://github.com/parroty/excoveralls for spotting uncovered zones.
( to be safe: codecoverage run code but it’s up to human to have the right tests
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 ) - use Typespecs and dialyzer for automating ( see book ref at the end for this)
- for doc coverage : https://github.com/rrrene/inch_ex
- 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 https://pragprog.com/book/tvmelixir/adopting-elixir , it adress your question and more . ( i’m currently reading and i really like it
)
svarlet
Mix xref may help a bit too
dom
What do you mean? There is most certainly a compiler, and it warns about undefined functions…
jswny
Yeah the compiler will definitely warn you about a function that doesn’t exist if you are trying to call it, or an unused function.
However, besides running Dialyxir on my codebase, I find that testing is the best way to approach this. Unit test all of your functions. Write good integration tests as well. That way, if you change anything, your tests will tell you what’s wrong, or at least what has changed so that you can fix it. Testing is important to make sure your code works as you intended but really, in my experience, a well-written test suite is extremely good at telling you what to fix after a refactoring.
My test suite gives me confidence that after I refactor something I know exactly what I missed.







