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 will take ...
Get Introducing Elixir now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.