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

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
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
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews