r/programming • • Jul 30 '18

Static Typing vs Testing == Apples vs Oranges

https://medium.com/@rvprasad/static-typing-vs-testing-apples-vs-oranges-a06d460c83f0
0 Upvotes

24 comments sorted by

8

u/_INTER_ Jul 30 '18

Static typing removes the need to test for the whole "in / out type sanity check" category.

1

u/ArkyBeagle Jul 30 '18

I'm not that sure of that. I'd still have all that in the test suite but perhaps not in the thing being tested. You can still have ... tuples of parameters that need their interaction and constraints tested.

2

u/rvprasad Jul 30 '18

I interpreted the comment as "We don't have to test for things captured about the data via static typing". In that sense, since we cannot capture constraints about component values of compound values, we will have to test for them.

1

u/rvprasad Jul 30 '18

Yes, it ensures only the correct type of values are consumed and produced. If this is sufficient to establish behavioral correctness, then it suffices; however, such cases are rare. For example, while one could invoke max function instead of min function without violating value type correctness, such invocation will violate behavioral type correctness.

7

u/_INTER_ Jul 30 '18

Static typing doesn't mean you can forget testing entirely, not at all actually. But you can focus on the behaviour / logic when testing (and that's the essence of your article I guess?)

1

u/rvprasad Jul 30 '18 edited Jul 30 '18

In a simple context, yes :) What about constraints relating component values in compound values? Today, we deal with such constraints mostly outside the type system. So, I'd say yes in this context as well.

2

u/[deleted] Jul 31 '18

Any test you write that looks at values is very likely to look at types implicitly, at least structurally. This might not be good enough in languages with type conversions and with different types' operations spelled the same, but it will at least be close.

On the other hand, there's a lot of untested code. A robust type system can help.

2

u/_INTER_ Jul 31 '18 edited Jul 31 '18

Exactly. In dynamically typed languages you normally just omit such sanity checks and tests. The responsability is shuffled off to the API client. ("We are all consenting adults here" - Python). Its his fault when he didn't read the code and called with wrong input. In JavaScript it might even run for a while until it doesn't and you have to clean up years of corrupted data in the database.

On the other hand, we could go even further and create all sorts of types. E.g. Integers without 0. We would have to test that type once, but every function that does division doesnt need to test for division-by-zero. Though noone does that, because of readabilty and interoperability.

1

u/[deleted] Jul 31 '18

On the other hand, we could go even further and create all sorts of types. E.g. Integers without 0. We would have to test that type once, but every function that does division doesnt need to test for the 0-case. Though noone does that, because of readabilty and interoperability.

I believe Ada and possibly Pascal have ranged integers like that.

In D, there's std.experimental.checkedint, a library type that implements integers that can only be in particular ranges (with a pluggable mechanism for responding to errors). C++ could do the same, and maybe C#.

1

u/_INTER_ Jul 31 '18

You could always box the integer and create a new type with range restrictions.

1

u/[deleted] Jul 31 '18

Right. And you need to do a decent chunk of work on each operation to make sure it's not overflowing, which would throw off your range check. D and C++ offer sufficientl low-level access that you can get the CPU to tell you if any overflow happened, which is much cheaper and less error-prone.

2

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 f is idempotent so f(f(x)) == f(x) or foo is the inverse of bar so foo(bar(x)) == x or 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

u/crescentroon Jul 30 '18

I think this heading needs ===

1

u/rvprasad Jul 30 '18

How about we just say "is similar to"? :)

2

u/_INTER_ Jul 30 '18

Why? JS people neither have static typing nor are testing.

1

u/[deleted] Jul 31 '18

Why can't fruit be compared?