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: 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 :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 : https://github.com/rrrene/inch_ex
  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 https://pragprog.com/book/tvmelixir/adopting-elixir , 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

dom

dom

What do you mean? There is most certainly a compiler, and it warns about undefined functions…

jswny

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.

Where Next?

Popular in Questions Top

pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
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
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
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
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
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New

We're in Beta

About us Mission Statement