3 ms·
Bruce nails why strong typing is important, but misses the point. He says, "realizing that the compiler was just one (incomplete) form of testing," Strong typ
by kenjackson 16y ago
Bruce nails why strong typing is important, but misses the point. He says, "realizing that the compiler was just one (incomplete) form of testing,"
Strong typing is really just a form of testing. But it has a couple of advantages. It is a form of testing with strong mathematical underpinnings. If someone says their application is correct with respect to the type system, I know what that means in a pretty precise way.
This has a couple of side benefits. When working in modular units or decoupled teams, when one team gets some code they know that if type checking has passed a set of requirements have been met. They understand what that means. While unit tests are useful, you passing your unit tests means very little to me, until I spend a few days going through your unit tests.
Another benefit is that all of your code gets these tests run on them. One of the few tests that gets 100% code coverage all the time.
Examples of famous bugs that could have been avoided with a strong type system include the NASA Mars orbiter bug. If they used a language like Ada that supports having English and Metric integer types, they would have been sure to mix them properly. And of course the compiler could enforce this through all of the code.
While Bruce does point out that weak typing (or dynamic typing) may result in less typing, it generally doesn't result in easier thinking. For me that makes it a relative wash in that regard.
- auxbuss 16y agoThe difference with test-driven development and continuous integration is that everyone relies very heavily on the tests. So, there is no necessity to check the tests, you start from a position of knowing that the tests are complete. The NASA issue could equally have been caught by better testing. It's hard to debate that point, because we don't know the strength of the testing in that case. Other than it was clearly not adequate. Regarding your last point: I find that having excellent unit tests and coverage means that I can concentrate singularly on the design problem at hand and experiment very broadly without any fear of regression. That's an enormously liberating place to be.
- kenjackson 16y agoI should be clearer. Tests are great. Static strong typing is a built-in automatic test that hits all my code at compile time with known semantics. I like that testing also.
- bad_user 16y agoThe tests themselves can be broken too. Test-driven development also requires a sustained effort to not miss all the possibilities of something going wrong. Developers aren't wired like that, that's why many companies have QA engineers (i.e. people specialized in breaking things). And not everyone has the resources to do test-driven development. Sure it helps in the long run, just like eating healthy and going to the gym, but when your software was supposed to get released yesterday and you ain't done ... tell me you're thinking about branch / path coverage.
- msbarnett 16y ago> When working in modular units or decoupled teams, when one team gets some code they know that if type checking has passed a set of requirements have been met. They understand what that means. While unit tests are useful, you passing your unit tests means very little to me, until I spend a few days going through your unit tests. Except that "type checking has been passed" doesn't actually tell you anything about correctness either without spending a few days going through the types. It's very easy, and very common, for bad programmers to build a few BigBallOfStuff types and essentially re-invent an ad-hoc dynamic type system that will pass static analysis in a way that is utterly meaningless when it comes to behavioural correctness
- kenjackson 16y agoThere is a very big difference. It's that when I write code I need to use your types. If I'm getting a BigBallOfStuff I instantly know that your type checking is constrained based on the types. With virtually any external testing (unit, integration, UI, or most other) working with the product code is not sufficient to determine the quality of the testing. The tests must be looked at specifically. Type checking and other forms of static analysis run on the code themself, which means that I only need to inspect the product code -- which I pretty much have to do to write any code with it.
- silentbicycle 16y agoA BigBallOfStuff type should stick out like a sore thumb in a code review, though. Static typing is a DSL for specifying assumptions in a way the compiler can check. If you misuse it, it can backfire, but that's true of pretty much everything.
- msbarnett 16y agoThat's just a recasting of "if you rigorously specify all of your assumptions in your unit tests, you needn't worry about anything". You can take maximal advantage of the type system to encode assumptions, but it's still on you to do it. This isn't a silver bullet that makes static typing better than dynamic typing, because nothing forces you to be competent about specifying your assumptions here, either. You can take maximal advantage of the type system to specify as many assumptions as it can check or you can take maximal advantage of the unit tests to specify the same assumptions. You end up in the same place either way, and the OP's claim that you can "trust" the existence of passing type-checks without looking at the types to see what assumptions they encode in a way that you cannot trust unit tests doesn't hold up.
- archangel_one 16y agoBut if they had set up this specific metric integer (float?) type for measuring force in, that implies to me that they had already anticipated that the mix-up of units was likely to be a problem. From what I've heard about it, that wasn't the case - neither side thought about it until after they knew it had failed. It seems like it'd be great if the type system had caught that, but if they expected all software to be using SI units they may not have seen the necessity for it even if they were working in a language which permitted that kind of thing.
- kenjackson 16y agoSo this is one of those things where language features and language culture play a role (like unit testing in Ruby). In Ada, you attempt to capture the assumptions in either the type of the name of the type. In general, you don't ever use primitive types directly. If you're going to use integer types questions you, for example ask, what are the assumptions? Is the range of integer values from 1-100? If so, then you make an integer type of that range. When you say "50", it's "50 what"? "50 decibals", "50 meters", "50 days"? So then you have types with these names. If they followed standard Ada dev practice this bug would have probably been obvious. Probably less obvious in standard practice for many, if not most, other languages.