joaoevangelista

joaoevangelista

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!

Showing Posts 1 to 10

idi527

idi527

Maybe dialyzer would help with

so we don’t leave undefined functions somewhere in the depths of our code

It certainly helps me with this.

Also tests.

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…

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:)

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.

joaoevangelista

joaoevangelista OP

That’s alot!
mix xref wil be the most powerful tool for doing that, I keep forgetting about it, dialyzer would help too, while coding Thanks Elixir-LS!. I think the problem with me is that I will need to change the way I iterate, even doing tests and using CI, I relied too much on IDEs to do that kind of stuff, just a shortcut and done! renamed.
credo will support most of inspections I’m used of.
Combining xref with a defined public API can be the way to go, also a more “flowable” dependencies between modules. Maybe it been different than other languages leads us to think about what we are coding more than simply throw code into an editor.

tme_317

tme_317

I was trying to figure out how to use mix xref today to find all unused (dead code) functions after a recent major refactoring. I know many stale functions in my project no longer have callers… is there an automated way to find them? I found GitHub - joshuaclayton/unused: Deprecated; see https://github.com/unused-code/unused · GitHub which looks promising also but couldn’t get it to install from homebrew.

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.

belaustegui

belaustegui

As far as I know the compiler will only emit warnings about private unused functions. Public unused functions will not be notified in any way.

I’ve been playing myself with the idea of building an elixir command that used mix xref underneath and report about unused functions. Turns out that the thing is really complicated because functions can be called dynamically using apply, Task, etc.

For the moment I’ve found that the best way to keep unused code out is to have proper interfaces between the application components (Phoenix Context are an example) and manually check (using the editor search across the project) that functions in that interface are being used from somewhere.

tme_317

tme_317

Thanks for the help… definitely I wasn’t expecting dynamic function calls (i.e. apply) to be easy to detect but thought the compiler or xref could maybe find unused functions referenced by static calls.

Agree also on your last point… just being a bit lazy. I will manually go through all public interfaces (contexts) and use VSCode’s “Find All References” to see where they are called and make sure all private functions are properly defp.

Finally definitely agree on having a good test suite to ease refactoring… then you can use ExCoveralls also to uncover functions that are not used (or at least not tested).

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New
dillonoconnor
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews