4 ms·
Unfortunately, the author seems to be on the verge of falling into one of the fallacies he rightly points out: assuming that one of the main purposes of unit te
by ubernostrum 17y ago
Unfortunately, the author seems to be on the verge of falling into one of the fallacies he rightly points out: assuming that one of the main purposes of unit testing is to act as a surrogate for static analysis. I've seen code written in dynamically-typed languages which tries to do this, and such code pretty much universally falls into the category of "using dynamically-typed languages poorly" (particularly since pretty much every popular dynamically-typed language has lint tools which will catch the sorts of things paranoid static-typing aficionados try to test for).
What unit tests are good at, and what they should be used for, is verifying behavior and integration, something that static type systems notably still aren't particularly good at (and likely will never be good enough at for practical purposes -- I believe quite firmly that there's a diminishing-returns effect from ever-more-powerful type systems).
In general, though, I like to think of static type systems existing in relation to their programming languages in much the same way that metalanguages exist in relation to object languages in logic: the type system is essentially a separate language in which you can express and then verify statements about the program, just as a metalanguage allows you to express and verify statements about its object language.
This carries with it, of course, various implications for the limits of type systems (particularly since metalanguages are notoriously prone to infinite regress -- eventually you need a metametalanguage to make statements about the metalanguage, then a metametametalanguage and so on); I'm fairly certain that any sufficiently powerful type system would need to end up being a Turing-complete programming model in its own right, and since I've already got one of those (I'm writing my program in it) I generally pass on that and just write the unit tests :)
- camccann 17y agoI'm fairly certain that any sufficiently powerful type system would need to end up being a Turing-complete programming model in its own right, and since I've already got one of those (I'm writing my program in it) I generally pass on that and just write the unit tests :) Programming language designers are way ahead of you on that one! If memory serves me, C++ templates and type inference in Haskell and ML are all Turing complete. Then again, in a world where XML transforms and typesetting are Turing complete, this isn't too shocking, I guess...
- kingkilr 17y agoC++ templates are turning complete.
- silentbicycle 17y agoThe flip side of this, I think, is that you may get 90% of the overall benefit of static typing from just checking boundaries between modules and other fundamental static analysis, not the really far out stuff that makes for cutting-edge research papers. While it can do some really impressive things after that, there may be diminishing returns. I'm not a type system expert by any means, and I didn't really see what the point of static typing was until I learned OCaml, but I wonder if there's a practical middle ground here. Perhaps dynamic typing with optional inferred types or declarations.
- kscaldef 17y agoOCaml and Haskell seem to strike a pretty good middle ground to me. Powerful, but generally inferred static types. Haskell is a bit more concise, so I think it can be more appealing to the dynamic language crowd. The major objection people to have to HM-style type systems seems to boil down to "the compiler is forcing me to fix my bugs before it will run my code", which seems a bit silly. It seems doubly silly when it comes from people who advocate TDD or other variations on tests-first.
- silentbicycle 17y ago> The major objection people to have to HM-style type systems seems to boil down to "the compiler is forcing me to fix my bugs before it will run my code", which seems a bit silly. I disagree - immediately requiring types to check can add a bit too much friction for exploratory programming, and types aren't a perfect design tool for everything. It isn't "wah, I have to fix bugs before my program works" so much as, "translating my entire design into types upfront is slowing down my brainstorming too much". I'm not convinced waiting ten minutes before the code has to get past HM would cause a big increase in bugs slipping through, but it really does help make the language feel more flexible. I think a better time for such static analysis (not just type-checking) would be when the code becomes a module and the programmer is already thinking about what to export, etc. Requiring a chunk of code in a scratch buffer or REPL to fully type-check is probably a bit too strict, except checking calls to existing libraries. (Optional type-checking is a different matter, of course.)
- kscaldef 17y ago> Unfortunately, the author seems to be on the verge of falling into one of the fallacies he rightly points out: assuming that one of the main purposes of unit testing is to act as a surrogate for static analysis. I've seen code written in dynamically-typed languages which tries to do this, and such code pretty much universally falls into the category of "using dynamically-typed languages poorly" Do you think that tests that check if a return value was null are "using dynamically-typed languages poorly"? Because that's very much an example of using tests to verify something the compiler could have proved for you.
- ubernostrum 17y agoDepends on why you have that test. If you have a function that's supposed to return a null value under certain conditions, then by all means test for it as part of verifying that the code behaves correctly. But if you're obsessively adding "assert the return value is not null" tests for every function, then yeah, you're doin it wrong.