Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

static analysis vs static typing

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?

like image 626
tldr Avatar asked May 09 '14 06:05

tldr


People also ask

What is the difference between static typing and dynamic typing?

First, dynamically-typed languages perform type checking at runtime, while statically typed languages perform type checking at compile time.

What is static type analysis?

Static Analysis is the automated analysis of source code without executing the application. When the analysis is performed during program execution then it is known as Dynamic Analysis. Static Analysis is often used to detect: Security vulnerabilities.

Is static typing faster than dynamic typing?

Dynamically typed languages must perform more operations to compute the result of an expression like “x+3” than statically typed languages do, so statically typed languages have better performance.

Is type checking static analysis?

Static Type Checking:Static type checking is defined as type checking performed at compile time. It checks the type variables at compile-time, which means the type of the variable is known at the compile time. It generally examines the program text during the translation of the program.


1 Answers

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.

like image 91
sasajuric Avatar answered Sep 27 '22 21:09

sasajuric