Good tool to find dead code?

I’m cleaning up an older piece of OCaml code as I work on it. The author included a huge personal library (basically their own giant stdlib++) and it isn’t obvious what parts of it are and aren’t relevant to the code I’m actually using.

Are there any good tools for figuring out what code is and isn’t live in a program?

I haven’t used it but this looks promising: Dead Code Analyzer.

Addendum: according to it’s opam entry, it does not work on OCaml 4.06 and higher.

2 Likes

We need a refactoring tool for OCaml…

1 Like
  • within a module, the ocaml compiler itself can find code (values, modules…) that is defined but not exposed in the interface if you enable the corresponding warnings. If the build system is a bit custom, the flags might be hardcoded to something that hide these low hanging fruits.
  • if you have a good test suite, bisect_ppx will help you find what code is not executed from your tests. That can point you to some dead utility functions.
  • as mentioned in another comment, DCE can find some problems as well: dead code, but also optional arguments that are always or never passed.

There was a presentation at ICFP17 about a refactoring tool for ocaml : https://icfp17.sigplan.org/event/ocaml-2017-papers-rotor-first-steps-towards-a-refactoring-tool-for-ocaml

We need a production-ready refactoring tool for OCaml… :wink:

1 Like

I’ll have a look at that, but most of the dead code is code not being used between modules. I’m almost certain that most of the code in question is unused but I’m not sure how to find all of it easily. I’ll have a look at the LexFi tool though.