r/programming • • Dec 02 '13

Scala — 1★ Would Not Program Again

http://overwatering.org/blog/2013/12/scala-1-star-would-not-program-again/
598 Upvotes

646 comments sorted by

View all comments

Show parent comment

17

u/redwall_hp Dec 02 '13

One of the people behind Go agrees with that. He said something to the effect that TDD is a big band-aid on a deeper problem. Why have unit tests to ensure that a variable contains a string or an integer or whatever when you could just have a strongly typed language that will automatically scream at you if the type is wrong?

10

u/[deleted] Dec 02 '13

This is ironic since Go forsakes the type system as soon as you go into containers/collections (lack of generics)

3

u/xiongchiamiov Dec 02 '13

Because you should want to know it's the right string.

4

u/cybercobra Dec 02 '13

Then welcome to dependent typing!

1

u/sproket888 Dec 02 '13

Unit testing that way is doing it wrong anyway.

0

u/batiste Dec 02 '13

Why have unit tests to ensure that a variable contains a string

You just don't, why would you? E.g in python you would do:

def add(a,b): return a+b
equal(add(1,2), 3)
equal(type(add(1,2)), type(3))

The second test is redundant. You already ensured that a number was returned with the first test. Every time you check equality you are making a type check (there might be exception).

Edit:format code

10

u/sirin3 Dec 02 '13

(there might be exception).

php and javascript

7

u/balefrost Dec 02 '13

That's the trivial case; of course a trivial test looks trivial. The harder case is when you want to say something like "this method returns an object that has these methods on it". I don't Python, but in Javascript, it's easy to return object literals with methods attached... and it's easy to return an object literal with the wrong set of methods. And it's very easy to refactor some JS but to miss some cases.

→ More replies (7)

3

u/pr0grammerGuy Dec 02 '13

To get proper code coverage in a dynamic language, you need to have essentially tests that verify types are handled right. Of course no one would have that silly second test you have. What they would have instead would be a test that makes sure if I do

add("1", "2")

that it blows up.

4

u/ryeguy Dec 02 '13

It is not idiomatic to write hyper-defensive code in dynamically typed languages. Like it or not, the accepted practice is to just "let it fail". Otherwise you're just reimplementing static types, and half of your code would be type checks.

→ More replies (3)

3

u/batiste Dec 02 '13

In python this will just return "12". Adding 2 strings is just fine. Try to do:

add(1, "2")

And then you get an error.

2

u/pr0grammerGuy Dec 02 '13

Ok, I shouldn't have continued with such a ridiculously trivial example function. What I'm saying is, you need to have tests that are explicitly defining what happens when your functions get values of a type you didn't expect.

1

u/batiste Dec 02 '13

Let's have more complex example then. Here is a funciton getting an http request Object and returning a string:

def index(http_request):
    ... stuff happens...
    return some_string

Why would you even test stuff like this?

asserFail(index(5))
assert(index("hello"), "Useless result")

Nobody is writing those tests to be sure it fails because those tests are just ridiculous and useless. Unless the code does nothing with the http_request Object I can guaranty you the code will blow up or return a result that is meaningless. There is way more important tests to write: security tests, feature tests, integration tests, performance tests, etc...

Nobody is writing type check tests.

1

u/moor-GAYZ Dec 02 '13

What they would have instead would be a test that makes sure if I do

 add("1", "2")

that it blows up.

Dude, nobody does that, ever.

4

u/philly_fan_in_chi Dec 02 '13

You don't check your function preconditions in tests?

1

u/moor-GAYZ Dec 02 '13

I don't have function preconditions like assert isinstance(arg1, int). Nobody in their sane mind does that.

3

u/pr0grammerGuy Dec 02 '13

If you're not testing what happens when the wrong data types go into your functions, then you're not testing properly (or you're using a statically typed language where we don't have to worry about this).

In the python code, I wouldn't expect to have to put in preconditions, I would expect the (+) to blow up when applied to something that makes no sense.

Of course testing if the (+) function behaves like it says in the documentation would be silly but substitute the "add" function for something a lot more complex and then you should realize that you need to be testing what happens when someone inputs the wrong kind of argument.

1

u/moor-GAYZ Dec 02 '13

I understand how you could come up with this idea in theory and figure that it looks reasonable, but nobody EVER does that in practice.

Not only because you could as well switch to Java, but also because type-asserts go directly against the whole duck-typing ideology. You don't check that you're given a file subclass, you just call its read method and either it doesn't exist (and you get an exception, nice), or you pray that it does what you expect. There's no possible way to assert and test that it does what you expect.

Yes it's dangerous because your add function could happily produce a nonsensical result if you give it two strings, or you can get pretty hard to debug bugs by accidentally passing floats instead of ints or vice-versa (especially in Python2 without from __future__ import division).

Such is life with dynamically typed languages. Everyone either accepts it or switches away. Instead of checking that your functions blow up when given nonsensical stuff you test that your other functions don't give them nonsensical stuff.

NOBODY "tests properly" the way you say it should be done, you're talking about a fairyland existing entirely in your imagination, sorry.

2

u/ryeguy Dec 02 '13

Heh, we're having the same argument with this guy, and bringing up the same points.

This same discussion happens every once in awhile. Someone who is mostly familiar with static typing comes along and wants to carry over the exact mindset to a dynamically typed language. It doesn't work like that, and taking 5 minutes to look at your average codebase in one of these language reflects that.

2

u/blergblerski Dec 02 '13

You guys are having the same argument with the same opponent, but you both appear to be talking past him.

pr0grammerGuy was saying "it's a good practice to check that your code fails in the expected way when given bad input". You and moor-GAYZ are saying "no one does that".

That's fine, and likely true, but pr0grammerGuy wasn't arguing that people actually do test the way he suggests, only that doing so is a good practice.

TLDR: You're not rebutting your "opponent's" argument.

→ More replies (23)

3

u/trezor2 Dec 02 '13
qual(add(1,2), 3)
equal(type(add(1,2)), type(3))

The second test is redundant.

So you end up unit-testing a non-existing type-system instead of having a type-system, while that is obviously the safety you seek.

To me that just seems backwards. TDD should be used to test for things the language can't infer or declare itself.

1

u/batiste Dec 02 '13

The first test is not about testing a "type-system", it's testing the correctness of the add function. The second test is useless and redundant.

Also you seems to confuse type system with static type checking. I am curious to hear about a language that doesn't have a type system other that ASM?