O'Reilly logo

Introducing Elixir, 2nd Edition by J. David Eisenberg, Simon St. Laurent

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 11. Static Analysis, Typespecs, and Testing

In programming, there are three major classes of errors: syntax errors, runtime errors, and semantic errors. The Elixir compiler takes care of finding syntax errors for you, and in Chapter 10 you learned how to handle runtime errors. That leaves logic errors, when you tell Elixir to do something that you didn’t mean to say. While logging and tracing can help you find logic errors, the best way to solve them is to make sure that they never make their way into your program in the first place—and that is the role of static analysis, typespecs, and unit testing.

Static Analysis

Static analysis refers to debugging by analyzing the source code of a program without running it. The Dialyzer (DIscrepancy AnalYZer for ERlang programs) is a tool that does static analysis of Erlang source and .beam files to check for such problems as unused functions, code that can never be reached, improper lists, patterns that are unused or cannot match, etc. To make it easier to use Dialyzer with Elixir, use the Dialyxir tool. We followed the global install path with these commands:

$ git clone https://github.com/jeremyjh/dialyxir
$ cd dialyxir
$ mix archive.build
$ mix archive.install
$ mix dialyzer.plt

The last command builds a Persistent Lookup Table (PLT) that stores results of Dialyzer analyses; the PLT will analyze most of the commonly used Erlang and Elixir libraries so that they don’t have to be scanned every time you use Dialyzer. This step ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required