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: 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
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 : GitHub - rrrene/inch_ex: Provides a Mix task that gives you hints where to improve your inline docs. · GitHub
- 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
)
svarlet
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex









