# Good tool to find dead code?

**URL:** <https://discuss.ocaml.org/t/good-tool-to-find-dead-code/2497>\
**Category:** Ecosystem\
**Created:** [August 29, 2018, 8:44pm UTC](https://discuss.ocaml.org/t/good-tool-to-find-dead-code/2497 "2018-08-29T20:44:30Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![perry](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/perry/32/518_2.png) [@perry](https://discuss.ocaml.org/u/perry)\
**Post date:** [August 29, 2018, 8:44pm UTC](https://discuss.ocaml.org/t/good-tool-to-find-dead-code/2497/1 "2018-08-29T20:44:30Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![lindig](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/lindig/32/532_2.png) [@lindig](https://discuss.ocaml.org/u/lindig)\
**Post date:** [August 29, 2018, 9:04pm UTC](https://discuss.ocaml.org/t/good-tool-to-find-dead-code/2497/2 "2018-08-29T21:04:33Z")

</div>

I haven’t used it but this looks promising: [Dead Code Analyzer](https://github.com/LexiFi/dead_code_analyzer).

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

---

<div class="post-metadata">

**Author:** ![UnixJunkie](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/unixjunkie/32/638_2.png) [@UnixJunkie](https://discuss.ocaml.org/u/UnixJunkie)\
**Post date:** [August 30, 2018, 3:40am UTC](https://discuss.ocaml.org/t/good-tool-to-find-dead-code/2497/3 "2018-08-30T03:40:09Z")

</div>

We need a refactoring tool for OCaml…

---

<div class="post-metadata">

**Author:** ![emillon](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/emillon/32/1206_2.png) [@emillon](https://discuss.ocaml.org/u/emillon)\
**Post date:** [August 30, 2018, 7:52am UTC](https://discuss.ocaml.org/t/good-tool-to-find-dead-code/2497/4 "2018-08-30T07:52:15Z")

</div>

- 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](https://github.com/aantron/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](https://github.com/LexiFi/dead_code_analyzer) can find some problems as well: dead code, but also optional arguments that are always or never passed.

---

<div class="post-metadata">

**Author:** ![zozozo](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/zozozo/32/59_2.png) [@zozozo](https://discuss.ocaml.org/u/zozozo)\
**Post date:** [August 30, 2018, 8:47am UTC](https://discuss.ocaml.org/t/good-tool-to-find-dead-code/2497/5 "2018-08-30T08:47:30Z")

</div>

> [@UnixJunkie](#):
>
> We need a refactoring tool for OCaml…

There was a presentation at ICFP17 about a refactoring tool for ocaml : [ROTOR: First Steps Towards a Refactoring Tool for OCaml (OCaml 2017) - ICFP 2017](https://icfp17.sigplan.org/event/ocaml-2017-papers-rotor-first-steps-towards-a-refactoring-tool-for-ocaml)

---

<div class="post-metadata">

**Author:** ![UnixJunkie](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/unixjunkie/32/638_2.png) [@UnixJunkie](https://discuss.ocaml.org/u/UnixJunkie)\
**Post date:** [August 30, 2018, 8:58am UTC](https://discuss.ocaml.org/t/good-tool-to-find-dead-code/2497/6 "2018-08-30T08:58:49Z")

</div>

> [@UnixJunkie](#):
>
> We need a refactoring tool for OCaml…

We need a production-ready refactoring tool for OCaml… 😉

---

<div class="post-metadata">

**Author:** ![perry](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/perry/32/518_2.png) [@perry](https://discuss.ocaml.org/u/perry)\
**Post date:** [August 30, 2018, 12:40pm UTC](https://discuss.ocaml.org/t/good-tool-to-find-dead-code/2497/8 "2018-08-30T12:40:32Z")

</div>

> [@emillon](#):
>
> 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.

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.
