static analysis vs static typing
Asked Answered
H

3

5

I'm learning Elixir, and the tool 'dialyzer' lets you do static analysis - annotate the function definition with the type specification of the parameters it expects and the output it returns. It's completely optional, but if it were to be used to the full extent possible, how does it match up to good 'ol static typing?

Hufford answered 9/5, 2014 at 6:3 Comment(0)
A
7

My impression was that dialyzer is not as exact as static typing, meaning that it sometimes doesn't report an error, although it should.

On the plus side, if a dialyzer complains, it's almost always my fault. More often than not, errors are usually due to incorrect typespec.

So, while I don't think dialyzer is as good tool as static typing, it still helps. In particular, I find typespecs very useful, since they can serve as a documentation. Recently I've switched job, and the project I joined is a complex Erlang project. Owing to typespecs it was easy to find my way around the codebase.

So my advice is to use typespecs in larger projects. We write them only for exported (public) functions and records, and it's a big help, without taking up too much time. I usually first make the code work, and when I'm happy with it, add specs, and run dialyzer to verify all is fine.

Ares answered 10/5, 2014 at 7:7 Comment(0)
C
2

While static typing takes care of a whole class of bugs, static analysis tools like dialyzer can tell you a lot more about potential pitfalls in your code. Assuming you used specs to their fullest extent, dialyzer would probably be more useful than static typing would be on it's own, at least compared to languages like Go, C#, etc. Something with a much more powerful type system like Haskell still can benefit from static analysis, but less so than a language with a more naive type system like Go. Static analysis is most useful when combined with a static type system though, and since Erlang and Elixir are both dynamic languages, static analysis can only do so much. That said, dialyzer is extremely powerful and useful, and if used consistently, should offer at least the same level of protection, if not more, as the type systems you are probably already familiar with.

I would take a look at the dialyzer docs (http://www.erlang.org/doc/man/dialyzer.html), they can tell you a lot more about what you can expect from the tool with respect to Erlang and Elixir. Hopefully that helps!

Comitative answered 10/5, 2014 at 6:38 Comment(2)
I've heard that Haskell has one of the most robust type systems, so I agree on that. But could you elaborate how Dialyzer (if used to the full extent possible) can provide the same level of protection as static typing in Go?Hufford
Dialyzer's typespecs allow you to define the exact constraints of a function's arguments and return value. You can define your own types to use in the typespecs, and those types are patterns, just like you would write for a case statement or destructuring function params. This is more powerful than Go's type system, because it allows you to define not just the structure of a type, but constraints of that type, such as a Date type that only allows valid dates. Dialyzer also analyzes your code for dead or unreachable regions, and other mistakes that a type system alone may not catch.Comitative
N
1

For one thing static typing is built into the compilation phase--sort of impossible to miss. Static analysis on the other hand is something that a developer has to run voluntarily.

Likewise, one doesn't have to annotate anything in Elixir; it's entirely down to programmer discretion. In statically typed languages it's impossible to avoid.

I would say your question is sort of broad and therefore hard to answer with any rigor. You might want to put this over on Programmers.Stackechange.

Neglectful answered 9/5, 2014 at 11:54 Comment(3)
I should've clarified, I meant in terms of functionality. I'm sure one could create a lint that checks for type specifications for each function, and set it as a pre-commit hook. Assuming static analysis used to the full extent possible, how does it match up to static typing?Hufford
And, again, broad question and hard to give a definite answer. Since Elixir doesn't use static typing but does use Dialyzer, which static typing versus Dialyzer?Neglectful
If you've used Golang, I'd be interested in a comparison with that.Hufford

© 2022 - 2024 — McMap. All rights reserved.