Dead code elimination and unused let expressions

Elm 0.19 does a great job of not compiling code in your Elm modules that you don’t actually use.

At the same time, there is a long convention of using let expressions with Debug.log to check the values of things during development:

let
    _ =
        Debug.log "what is this?" something
in
div [] []

The above code will render an empty div, but will also log the message to the browser console.

My question is: does Elm 0.19’s dead code elimination ever optimize out let expressions like the one above? Perhaps when building with --optimize (were Debug.log is not allowed, of course)?

I’m wondering if leaving unused let expressions lying around bloats the compiled output, or if the compiler cleans them up along with any other unreferenced functions in the module.

I think I’ve proven to myself by testing that unused let expressions are left in, at least in 0.19.0.

I’d love to know if this is likely to change in the future, as we’re building some tooling that might depend on this.

From Evan’s blog post (Small assets without the headache), dead code elimination in elm is function based. Let expressions not being a function, it makes sense that they are not eliminated.

1 Like

elm-analyze already detects and marks unused declarations inside let so, I would not be surprised if this kind of dead code will be removed in future versions of elm.

I wouldn’t rely on this existing forever.

There are no concrete plans to change the behavior at some specific time, but as the optimizations in the compiler improve, we will likely be able to strip out more and more. To enable those kinds of optimizations, I think it would be necessary to give a “unused variable” error in cases like this so that people do not spend an hour wondering why the code they wrote is not executing. This would need to be in a major release though, so anything in the 0.19.x series should be the same.

7 Likes

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.