News for hackers. Stuff that matters.
Re:Diminishing returns of static typing by enraged_camel on Monday October 02, 2017 @01:24PM (#15385840)
>>There are a class of bugs you just never need to worry about when the compiler does some compile-time checks for you ... like worrying that you passed the wrong type into a function, or the wrong number of arguments.

People always say this and it baffles me. Bugs like that should be caught immediately by your test cases. You shouldn't rely on the compiler to catch them for you.

Re:Diminishing returns of static typing by rbobrowicz on Monday October 02, 2017 @02:11PM (#15386300)
Personal anecdote:

I worked on a small Python project some years ago and we had a type error in production despite having tests.

We traced it back to a call to a third party library. It was supposed to return a list of results, and all of the test cases around it worked and always got a list back. In production however we encountered an error because if there was only one value to return, the library would not return a list of one element, as we expected, but a scalar value. So the rest of the code was expecting a list and when it encountered a scalar it blew up.

You can blame it on us for having insufficient test cases, or not coding defensively enough, or not reading the source code of the library we used, or the author of the library for bad design, but ultimately, this bug would not have been possible in a statically typed language.

So just saying "have test cases" is not good enough. Your test cases can be not exhaustive, but a good static type system and type checker is.

Re:Diminishing returns of static typing by berdario on Monday October 02, 2017 @05:11PM (#15387999)
I cannot reply to cdoconnor (probably some downvotes), so I'll write here:

That's the whole point: as it's often said "type checking keeps you honest"

I stumbled time and time again upon badly designed libraries... With a static type system, the painfulness will be obvious and felt the first time you'll try to build your code

with dynamic types, the pain might not be felt at all, until a crazy bit of code will be invoked, sometimes at the most unfortunate of times

Re:Diminishing returns of static typing by crdoconnor on Monday October 02, 2017 @04:11PM (#15387494)
>or the author of the library for bad design

That's the one. That's a damned stupid decision.

Re:Diminishing returns of static typing by UncleMeat on Monday October 02, 2017 @05:43PM (#15388216)
Yeah and a static type system refuses to let you make such a stupid decision. When interoperating with code I didn't write, knowing that the code is guaranteed to conform to some specification is valuable.
Re:Diminishing returns of static typing by andrewaylett on Monday October 02, 2017 @01:34PM (#15385934)
Why would I write test-cases for something the compiler can catch for me? Yes, I need to write tests for all the correctly-typed cases, but there are a whole class of bugs that I don't need to test for any more because I can't even write the failing case.
Re:Diminishing returns of static typing by crdoconnor on Monday October 02, 2017 @04:23PM (#15387585)
>there are a whole class of bugs that I don't need to test for any more because I can't even write the failing case.

By adding sanity checking asserts in a dynamically typed language you can achieve more or less the same result.

Re:Diminishing returns of static typing by wstrange on Monday October 02, 2017 @04:48PM (#15387809)
If you are going to add a bunch of asserts, why not just use types?
Re:Diminishing returns of static typing by crdoconnor on Monday October 02, 2017 @05:14PM (#15388029)
I don't particularly see the need to extend this approach to every single variable in my code base. I usually just stick it on boundary code.
Re:Diminishing returns of static typing by enraged_camel on Monday October 02, 2017 @01:42PM (#15386009)
You don't write test cases specifically to check types. Your existing cases, if they are robust and thorough, will simulate your runtime and check those for you as a side effect.
Re:Diminishing returns of static typing by andrewaylett on Monday October 02, 2017 @05:12PM (#15388005)
My existing test cases, robust and thorough though they are, only test what happens when I feed in a correctly typed object. Because I can't write a test that exercises my code on an incorrectly typed input: the type system prevents that.
Re:Diminishing returns of static typing by metaphorm on Monday October 02, 2017 @01:49PM (#15386084)
you're already writing the test cases to ensure correct behavior with typical input, and predictable exceptional input. putting in one more assert for predictable exceptional input (wrong type) doesn't really add a noticeable amount of overhead to writing the tests you were already writing.
Re:Diminishing returns of static typing by macspoofing on Monday October 02, 2017 @02:52PM (#15386739)
>putting in one more assert for predictable exceptional input (wrong type) doesn't really add a noticeable amount of overhead

Yes. We call that 'typing'.

Re:Diminishing returns of static typing by discreteevent on Monday October 02, 2017 @05:58PM (#15388315)
And it takes less typing (keyboard) than the assert, and you get free documentation inline with the code!
Re:Diminishing returns of static typing by cm2187 on Monday October 02, 2017 @02:30PM (#15386464)
If you have to write a test for every line of code you write, the "chatty" argument against static typing goes away.
Re:Diminishing returns of static typing by senorjazz on Monday October 02, 2017 @03:03PM (#15386845)
and not just that, but for every possible case for the line you can think of. Like the post above, testing the results of a function returns a list for 1 item, or many items...

I see all too often, java bad, just look at how many lines you need to write to get "hello world". Who cares, the IDE generates that for you, but if you don't understand static void main as a beginner... again who cares, just put it in and ignore it until you need to understand it. Also read that dynamic languages are so productive, but they never mention you need to then write tests to detect what the compiler would catch for free. Yes good code needs tests, but the compiler IMO is a damn good built in test suite for catching many classes of errors

Re:Diminishing returns of static typing by Silhouette on Monday October 02, 2017 @04:02PM (#15387421)
Bugs like that should be caught immediately by your test cases. You shouldn't rely on the compiler to catch them for you.

What's wrong with relying on the compiler? Bugs like that will be caught immediately by a good static type system, so you don't have to rely on your test cases to catch them for you.

Relying on a test suite to verify basic properties that could be enforced automatically through a type system means you're only checking some cases instead of all cases. It also means you're cluttering your test suite with boilerplate about language mechanics instead of writing high value tests that verify the real operational behaviour of your system.

There are pros and cons to static vs. dynamic typing in general, but in this particular respect, static typing is strictly more powerful, less verbose and more efficient.

Re:Diminishing returns of static typing by dep_b on Monday October 02, 2017 @01:39PM (#15385985)
Well if you don't need to write that particular test you have more time writing tests that are actually useful, like validating your logic (and dare I say: more fun to write).
Re:Diminishing returns of static typing by AsyncAwait on Monday October 02, 2017 @06:44PM (#15388623)
Having a compiler catch this for you means having to write less test cases.
Re:Diminishing returns of static typing by 201709User on Monday October 02, 2017 @01:28PM (#15385871)
Yes, why do something automatically when you can do manual work!
Re:Diminishing returns of static typing by icebraining on Monday October 02, 2017 @01:32PM (#15385915)
I think the idea is that you have to write tests anyway.
Re:Diminishing returns of static typing by whyever on Monday October 02, 2017 @01:40PM (#15385992)
No, you don't have to write tests for invariants enforced by the type system.
Re:Diminishing returns of static typing by ubernostrum on Monday October 02, 2017 @02:05PM (#15386254)
I think the bigger idea is that people who've never actually written serious code in a dynamically-typed language assume that people who do always write a bunch of extra unit tests to assert correct types, assert behavior on incorrect types, etc. etc., and that programs crashing due to type errors is a super frequent occurrence.

None of those things are true.

Re:Diminishing returns of static typing by tome on Monday October 02, 2017 @05:01PM (#15387919)
And yet there are dynamic language proponents in this very subthread suggesting that's exactly what everyone should do.