7 ms·
Static typing is an alternative to invariants in dynamic languages, not testing. Testing can't really substitute for types. They're two diametrically opposed a
by pydry 5y ago
Static typing is an alternative to invariants in dynamic languages, not testing.
Testing can't really substitute for types. They're two diametrically opposed approaches to impeding bugs.
- ninkendo 5y agoHow do you express invariants in dynamic languages? (In a way that doesn’t require test coverage to check those invariants?) I thought the only way would be to write some assertions in the prod code, and then write the unit tests to cover them… but you seem to be implying that you can “do” invariants in dynamic languages in a way that doesn’t require testing.
- eru 5y agoI guess you can sort-of use design-by-contract and call it an alternative?
- pydry 5y ago>In a way that doesn’t require test coverage They obviously work best when "combined with test coverage" but then again, I dont consider test coverage to be "optional" in any language. This idea that "if it compiles it works" that some haskellers seem to believe, for instance, is complete horseshit. If somebody argues that static typing works great because without tests it catches more bugs than dynamic typing does when you also dont write tests, it's notionally true, yes, but theyre telling you more about themselves than they are about types. Namely that they're not in the habit of writing tests. If you've got enough of the right kind of testing coverage to validate an app written in a statically typed language you will have enough to trigger invariants in a dynamically typed langauge. I have successfully had invariants usefully triggered in prod and by manual testers in big balls of mud without test coverage (failing fast isnt just for test environments) but I considered that to be a form of emergency programming CPR and damage limitation that isnt a substitute for fixing your testing "diet".
- shadowgovt 5y agoAgreed; "if it compiles it works" tends to tilt too far in the other direction. (... I once had a professor who believed that phrase as nearly a point of religion, and his project work was maddening. No documentation on any functions either, because "the types are self-documenting." Then you'd get hit with a 'division' function that takes (number,number) but he put the denominator as the first argument. Ugh.)
- ragnese 5y agoI'm not an "expert" at dynamic languages, but I've written small microservices in Clojure and Elixir, data crunching code in Python, and your typical junk in PHP and JavaScript. Clojure does have spec (https://clojure.org/about/spec https://clojure.org/about/spec) which is pretty cool and declarative. And, of course, your IDE can help you when writing code that obviously violates a spec declaration. But that's not fundamentally different from an IDE parsing the type-hints of Python, JavaScript, or PHP. It is more powerful than the aforementioned type-hints, though. At the end of the day, it blurs the line between static typing and dynamic typing. IMO it should be considered static typing because it serves that same purpose, works at "write time" to help the programmers, and can probably absolve you of writing your own type-checking tests. In some sense, it is all just asserts. Even statically typed languages have unsafe type casting, which can trigger an "assert" at runtime.
- ashtonkem 5y agoThey are not diametrically opposed, but rather they’re non-overlapping with a strong type system. In a dynamic language you end up writing a number of tests that would be covered by a static type system and writing a number of tests that would not be covered by that. Going from dynamic to static typing is a replacement for some of the former, and none of the latter.
- pydry 5y ago>In a dynamic language you end up writing a number of tests that would be covered by a static type system Not once have I done this. I've written plenty of invariants that would have been covered by a static type system but no tests. As far as I'm concerned if you had to write an extra test to "cover for a dynamic type system" that test is a bad smell that should be deleted. A test validates that a method raises an exception when being fed a null is pointless if it's just testing a one line assertion that does the same thing. It's a waste of keystrokes. It doesnt catch bugs. There may be some exceptions to this rule but they would be under very obscure circumstances, I think.
- camdenreslink 5y agoOne reason to have tests like that, is to cover regressions. Say somebody changes that method in the future and it doesn’t raise an exception anymore when passed a null (but it is still supposed to, the change introduced a bug). If you have that “pointless” test, you’ll find out about it. If not, you’ll probably only encounter it at runtime with a program that is subtly wrong.
- pydry 5y agoNo, you won't. If somebody removes that line they had a reason. That reason will make them remove the test that makes it fail as well. Whether it's a good or a bad decision is another matter and not something the test will help with. Tests that exist to check for the presence of a single line of code are deadweight.
- 5y ago