How is static typing really helpful in bigger projects

dynamic-typingprogramming-languagesstatic-typing

While curiosing on the main page of a scripting programming language's site, I encountered this passage:

When a system gets too big to keep in your head, you can add static types.

This made me remember that in many religious wars between static, compiled languages (like Java) and dynamic, interpreted languages (mainly Python because it's more used, but it's an "issue" shared between most scripting languages), one of the complaints of statically typed languages' fans over dynamically typed languages is that they don't scale well to bigger projects because "one day, you'll forget the return type of a function and you'll have to look it up, while with statically typed languages everything is explicitly declared".

I never understood statements like this one. To be honest, even if you declare the return type of a function, you can and will forget it after you've written many lines of code, and you will still have to return to the line in which it's declared using the search function of your text editor to check it.

As an addition, as functions are declared with type funcname()..., without knowing type you will have to search over each line in which the function is called, because you only know funcname, while in Python and the like you coud just search for def funcname or function funcname which only happens once, at the declaration.

More over, with REPLs it's trivial to test a function for it's return type with different inputs, while with statically typed languages you would need to add some lines of code and recompile everything just to know the type declared.

So, other than to know the return type of a function which clearly isn't a strength point of statically typed languages, how is static typing really helpful in bigger projects?

Best Answer

More over, with REPLs it's trivial to test a function for it's return type with different inputs

It's not trivial. It's not trivial at all. It's only trivial to do this for trivial functions.

For instance, you could trivially define a function where the return type depends entirely on the input type.

getAnswer(v) {
 return v.answer
}

In this case, getAnswer doesn't really have a single return type. There is no test you can ever write that calls this with a sample input to learn what the return type is. It will always depend on the actual argument. At runtime.

And this doesn't even include functions that, e.g., perform database lookups. Or do things based on user input. Or look up global variables, which are of course of a dynamic type. Or change their return type in random cases. Not to mention the need to test every single individual function manually every single time.

getAnswer(x, y) {
   if (x + y.answer == 13)
       return 1;
   return "1";
}

Fundamentally, proving the return type of the function in the general case is literally mathematically impossible (Halting Problem). The only way to guarantee the return type is to restrict the input so that answering this question does not fall under the domain of the Halting Problem by disallowing programs that are not provable, and this is what static typing does.

As an addition, as functions are declared with type funcname()..., whitout knowing type you will have to search over each line in which the function is called, because you only know funcname, while in Python and the like you coud just search for def funcname or function funcname which only happens once, at the declaration.

Statically typed languages have things called "tools". They are programs that help you do things with your source code. In this case, I would simply right click and Go To Definition, thanks to Resharper. Or use the keyboard shortcut. Or just mouse over and it will tell me what the types involved are. I don't care in the slightest about grepping files. A text editor on its own is a pathetic tool for editing program source code.

From memory, def funcname would not be enough in Python, as the function could be re-assigned arbitrarily. Or could be declared repeatedly in multiple modules. Or in classes. Etc.

and you will still have to return to the line in which it's declared using the search function of your text editor to check it.

Searching files for the function name is a terrible primitive operation that should never be required. This represents a fundamental failure of your environment and tooling. The fact that you would even consider needing a text search in Python is a massive point against Python.

Related Topic