by mpweiher · 3y ago · 0 comments
Fact is: the “types lead to fewer bugs” claim is just a feeling, it is “truthy”.
https://blog.metaobject.com/2014/06/the-safyness-of-static-t...
And it’s not for lack of trying. You might say the claim has been disproven.
That said,I personally like static types, but primarily for the documentation effect that is, maybe not entirely coincidentally, the only positive effect for which there actually is solid empirical evidence.
Concretely-speaking, an example of a bug being preventd by a typing system: Removing a field from an object in a large vanilla JavaScript project is inherently a minefield that has caused many bugs in the past, whereas the same alteration in a full Typescript project can be made with confidence.
But not in the way you fervently want to believe.
Once again: people have tried to show the truth of this for a long time, and they have consistently failed.
> This paper presents an empirical study with 49 subjects that studies the impact of a static type system for the development of a parser over 27 hours working time.
There are some studies like that, but there are others as well.
And remember that all these studies were trying to find a positive effect, but failed to do so.
What I meant to convey is that the matter is settled enough from my point of view that there needs to be a very strong case being made for me to be willing to engage in that conversation.
Thanks for pointing this out, I've amended the comment to clarify a bit.
Based on what evidence?
Apart from emotion?
Removing a field in a data structure is such a big problem, even in typed systems, that the most ubiquitous serialization IDLs, and customer-facing API conventions, don't even allow it.
As far as serialization IDLs and API conventions go, they have the very special constraint of having to provide both forward and backwards compatibility across multiple applications, making them a very poor universal example.
As an example, I was once refactoring a Java system. The compiler was happy after about a day, the unit tests after three days.
So if you claim your type system is going to keep your refactoring safe, I'd say that I'm not the one being delusional.
And of course unit tests also tend to, in practice, catch the errors that are caught by types. If you check that a value is equal to 10 rather than 9, you've also implicitly checked that it is not of type PinkElephant.
Now I perfectly understand the feeling that people who are used to the safety-net that static typing appears to give have when encountering a code-base without types. I have the same feeling when encountering a code-base without reasonably comprehensive unit tests (that fail when I poke into the code-base).
The feeling is absolute terror.
I just contend, with reasonably good justification, that this feeling is not particularly justified in the case of static types, because the evidence is just not there.
I haven't checked what the evidentiary status is for unit tests. But then again, I don't make these sorts of categorical claims for unit tests. I just make claims for my experience with unit tests.
Its like a grep, except it can, for example, differentiate between "name" (of user) and "name" (of project)
I don't think you even understood the point at all here. "Types let me make guided and exhaustive changes to code that's much easier to understand due to the types and compiler support"
I believe this would count as empirical evidence of very strong static typing reducing bugs. I think this points to what many other comments have been getting at, which is that typing is not a binary yes/no question, but a large, multi-axis (static/dynamic, strong/weak) spectrum with lots difference between type systems, as well as big differences in how people apply those type systems to their problem. You could still work in a static, strongly typed language and represent everything as strings, converting back and forth as necessary, but that's essentially working in a dynamically typed language. You could also take advantage of the tools the type system gives you in order to reap the benefits, creating classes representing the legal values and asserting important invariants.
It makes me so much more productive, by orders of magnitude. And I say this as someone who has worked extensively in several different languages which covers large parts of this spectrum, like C, C++, Java, Python, JavaScript, TCL and others.
It makes it much easier for me to reason about code I have not recently worked on, be it in the current project or in dependencies. It also means I can be way more focused on the problem at hand, since I'm not constantly having to go on tangent goose chases to figure out what exactly I can do with this object that some function returns.
Sure there's the good, fuzzy feeling when the compile doesn't error out. But that's secondary.
is exactly the reason why you shouldn't be use static typing. We have a developer here who doesn't document any of their code and thinks that a good thing. Whereas in the dynamic typing world, they would be forced to use the good practice of documenting their code.
Code is never self-documenting, there are research studies on it.
But the types provides for let's say code-level self-documentation.
I can look at a function definition and see it returns an IReadOnlyStream. Well if I'm familiar with IReadOnlyStream I already know what I can do with that. If not I can quickly find the declaration, and based on that I can get a good overview of what I can do. If not I have something explicit to search the documentation for.
With languages such as JavaScript I learn nothing by looking at the function definition, and almost always have to dig deeper. If it's an unfamiliar code-base, then often much, much deeper.
You would be better off using dynamic typing and returning an Iterator.
The dynamic typing route of a small number of language defined types is far better than any developer declaring their own types whenever they want.
Why? Because it's simpler and simpler code is faster to write and easier to test.
The objection is not that all bugs aren’t eliminated. The objection is that there is no empirical reduction in bugs.
You should have spent the time statically typing your code on writing more tests.
How would you feel if I claimed that you could make emotional arguments faster than you could make rational arguments?
Because that is how this debate feels!
I want the simple rationality of my compiler saying "no" if I try to treat my hashmap like an Apple or a String.
Rust has a more powerful type system that actually fixes MOST of the memory bugs in production
¯\_(ツ)_/¯
I’ve worked on reliability at a company everyone has heard of for software that billions use daily. So many bugs would have been blocked by having a type system at all (dynamic languages on backend or JS on web) or by having a less bad type system (changing ObjC to Swift and Java to Kt on mobile). Null pointers/references alone have created so many bugs for us. Bugs that could have been compiler errors.
Why this is we do not know. But we do know that it is true.
Not to mention that range checking is also fixable by using optionals rather than assuming the existence of a value.