by sdevlin · 13y ago · 0 comments
I'm a big fan of TypeScript's approach, which gives you the best of both worlds. Start with fully dynamic code to explore the problem space, then add more static guarantees as you firm up your design.
I haven't had occasion to write a big web app recently, but I'm itching to find one so I can put TypeScript through its paces.
Don't everybody mob him at once :-)
"What's true of all bugs? They passed a type checker and they passed the tests!" - Rich Hickey
Does it catch everything? Can you stop thinking critically? No, but it's still nice to have.
This sort of goes hand in hand with IDE tools (e.g. "change method name" sorts of things), so I don't think we necessarily disagree.
> I've never seen a bug in production that could have been prevented with static analysis.
That seems unlikely to me.
I do application penetration testing for a living, and I'll often use static analysis tools in my work. (Both robust, established tools and ad hoc scripts.) For example, check out Brakeman for Rails apps. These tools find actual bugs in actual production software.
Do they find everything? No, you can't rely on them completely. But they're still nice to have.
1. Static type compiler verification saves tonnes of problems. Try managing a 2MLOC dynamically typed solution and you'll get what I mean.
2. Static typed languages are way easier to refactor as more metadata is available to the tooling.
3. Static typed code is easier to test. The type contract is available over the boundary between the implementation and the test cases.
4. Defined interfaces without leaky abstractions are easier to produce when you have static types.
5. Static types allow the compiler to infer more information about how to compiler your code resulting in faster, more efficient code and lower memory usage plus you don't have to compile an instance of a function with every possible type consideration at runtime.
6. Statically typed data serialises more reliably i.e. goes over the wire easier without behemoth contracts and parsers at each end.
7. Statically typed languages tend to have better numeric accuracy as differences between decimal, floats and integers are always deterministic and there are defined casts and conversions between each during operations.
I could go on...
C is hardly a good representative for an statically typed language. Read Luca Cardelli's "Typeful Programming"[1] to get an overview of what typed programming is all about, and see for yourself how brain-dead C and C++ are in comparison. After that go to Benjamin Pierce's canon, "Types and Programming Languages".
C has some static type safety, and virtually no run-time type safety. Java on the other hand, has a lot of static type safety, is completely memory safe (barring bugs in the implementation), and checks all coercions dynamically. Python has what Java has dynamically, and nothing statically, but it is still 10 times better than what C has!
In general, C's type system is so weak as to be both useless and a hindrance.