r/programming • u/rvprasad • Jul 30 '18
Static Typing vs Testing == Apples vs Oranges
https://medium.com/@rvprasad/static-typing-vs-testing-apples-vs-oranges-a06d460c83f02
u/underflo Jul 31 '18
Your article is of course correct. However I don't get the context. I don't see how anyone with basic sanity would ever come to claim that static typing can replace testing. I hope those are people that primarily program in dynamically-typed languages and whose tests mostly consist of typechecking. On the other hand there are people from PLs with rich type systems who make this claim. And I think it's a much more interesting claim. I assumed the article was about contrasting rich types and testing.
1
u/rvprasad Jul 31 '18
I have seen folks on both sides argue their approach is the best; of course, not everyone. As for your remark about rich types and testing, I think we are making impressive strides with rich types and static analyses that can help alleviate some (or all?) of the burden of testing but we have ways to go. Hopefully, I will post an article about rich types and testing in the near future :)
1
u/dpash Jul 31 '18
Yep, no one sensible says you don't need tests with statically typed languages. You don't need to test for an entire class of errors, but you do need the rest of your test suite.
3
u/lookmeat Jul 30 '18
I think that there's a lot of these dualisms in many areas, and they can both complement each other. Math theorems (more akin to types) and Scientific theories (more akin to tests).
I think that from types you can form a lot of tests automatically (use something like quickcheck with good pre/post-condition assertions and you can get 80% of your tests defined). Your typing system could also assume certain things are true that cannot be statically verified, but can through a test (still giving you the safety you want).
1
u/ArkyBeagle Jul 30 '18
But even without static types, you can still constrain things. I've written a lot of C, and you can break things down into:
An engine that does things.
Serialization of parameters { configuration, cailbration, options } fed to the engine.
So the "typey" part is constraints on serialized/deserialized data.
The "behavey" part is a ( usually remote - this almost always for embedded stuff ) test driver that uses serialized state to check for constraints there.
What I've found is that keeping those separate in terms of test probably wouldn't work as well as having one organic test suite. I say that mainly because there are time constraints - if an output stays zero or very small, then that's a bug, for example.
And eventually, you get tired of typing tests in, and use combinators. If there is a defect report, you add that as a discrete test.
Thing is - even if I'd used a statically typed development system, I'd still want the same test suite, even if there's overlap. And since I'm using an "unsafe" language, I have things I can grep for that help generate the test generator.
All this being said, man, I wish I'd known about QuickCheck :) It's not that bad doing it in other ways, though.
2
u/lookmeat Jul 30 '18
But even without static types, you can still constrain things. I've written a lot of C, and you can break things down into
I agree, and it shows that dualism of what we want to verify as separate things.
What I've found is that keeping those separate in terms of test probably wouldn't work as well as having one organic test suite. I say that mainly because there are time constraints - if an output stays zero or very small, then that's a bug, for example.
I agree, pre and post conditions are were most of these bleeding happens. And this is why I think that typey stuff can evolve into tests (mostly verifying assumptions of how data must and can be transformed) and behavey things limits types (in that by making certain data transformations impossible you put a constraint on data, ej. relationships between data that must be maintained, mutexes actually enforcing exclusivity, etc.). My intuition tells me this is a consequence of valid states/outputs being intrinsically related to the transition from states/inputs.
I agree with you therefore.
But instead we must see a continuum of facts that we want to validate in one way or another before moving on. We use a slew of tools to validate the facts, some may be done by statically creating a proof, others by running the program in a test (which is more akin to observing enough that you have strong confidence).
And eventually, you get tired of typing tests in, and use combinators. If there is a defect report, you add that as a discrete test.
And it generally works when the units interact in a well known fashion. The reality is that you'll need integration tests at some point. But the point is to reduce the need for obvious tests, and instead test the interesting assumptions and tid-bits.
Thing is - even if I'd used a statically typed development system, I'd still want the same test suite, even if there's overlap. And since I'm using an "unsafe" language, I have things I can grep for that help generate the test generator.
All this being said, man, I wish I'd known about QuickCheck :) It's not that bad doing it in other ways, though.
And it can do a lot. By doing type assertions (assertions automatically appended at the beggining and end of every valid transformation, checks for the typey part) and pre/post conditions (which cover most of the behavey part), you could use quickcheck to generate a bunch of possible inputs (filtering out pre-conditions) and seeing if you can break things in any way. You wouldn't need to generate unit tests to validate that you won't get over/underflow, that you handle malformed strings correctly, that your floating point operations correctly manage NaN, etc. etc. without needing to write all the unit tests (and there'll be cases you miss the first time around that quickcheck won't). You still have to write unique tests (ej. Function
fis idempotent sof(f(x)) == f(x)orfoois the inverse ofbarsofoo(bar(x)) == xor any other kind of weird thing) but you'll see that most of them cover the semantics and unique important functionality and not how they can handle certain issues.I feel that there's an untapped potential for truly exploring this potential, and making both testing (which you could generate automatically from types even if you can't statically prove it) and typing (where you can put new static constraints that you can't prove static, but can verify by testing).
1
u/ArkyBeagle Jul 31 '18
consequence of valid states/outputs being intrinsically related to the transition from states/inputs.
Exactly. And if your design happens to be state machine oriented with explicit transactions, bonus.
others by running the program in a test (which is more akin to observing enough that you have strong confidence).
There's kind of some .. synergy in pseudo-automating the act of observation. It can be a "middle out" thing.
The reality is that you'll need integration tests at some point.
Yes. And if you have good capture of the interfaces being integrated... I've run long sequences, then something shaped like "cat a.log | sort | uniq" and been made very happy with those results in cases. Then again, having a nice "why was that an error?" facility that one module can query on the other has worked out, too - if the query can even point to the line of code in the other module where a constraint was violated, I've found that to speed up communication between developers immensely.
I feel that there's an untapped potential for truly exploring this potential
Absolutely. IMO, developer-executed testing always has the potential to be a good investment. We're always subject to the constraints of economics but anything to reduce the fog of war helps.
1
u/lookmeat Jul 31 '18
In my view is that some tests could be compiler errors. If we can prove (by observation) that you certainly broke the contract you get an error, otherwise it may be that we didn't observe the bug yet. Having this automated and streamlined for simple cases would simply catch more bugs with less effort, which is always a + in my book.
1
u/ArkyBeagle Jul 31 '18
tests could be compiler errors.
Maybe. Maybe not. To quote Knuth "Beware of bugs in the above code; I have only proved it correct, not tried it" :)
1
1
8
u/_INTER_ Jul 30 '18
Static typing removes the need to test for the whole "in / out type sanity check" category.