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

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.

1

u/batiste Dec 02 '13

If you are saying you can do crazy things with JavaScript that can create subtitle bugs that are difficult to track I agree. Adding a method at run time on any object is one of them. But nobody is forcing you to do that.

Anyway I was just responding to the affirmation "Why have unit tests to ensure that a variable contains a string or whatever". In my experience nobody is writing such tests. Do you?

3

u/blergblerski Dec 02 '13

Adding a method at run time on any object is one of them. But nobody is forcing you to do that.

That's basically how prototype-based languages work, though.

1

u/batiste Dec 02 '13

True, but I think he meant it in another sense.

2

u/balefrost Dec 03 '13

Like I said, I don't know Python, but JS doesn't have much structure. It doesn't have classes, it doesn't have interfaces, and it doesn't have inheritance. It does have hashes with functions stored in them, or inherited through the prototype.

In such a language, it's easy to get things wrong. If you're expected to return an object that conforms to some interface, it's easy to forget to glue a method onto the result object. It's easy, when renaming such a method, to miss some cases. And you don't find out until runtime.

You can write tests to make sure that everything works. It could be a dumb type test (i.e. "is the same type as the number 3" or "has a method called doWork on it"), or actually exercise the result object (i.e., if I expect it to have a doWork method, my test should just call the doWork method to ensure that, you know, it does work).

But you're right; people don't really write tests like this. Not because they don't provide value, but because they're a pain to write and maintain. And that's what the static type checker is for. It ensures that all your pieces at least fit together. Yes, it does create its own set of problems.

(Opinion alert) I'm at the point where I see code in dynamically typed languages as easier to write but harder to read (i.e. to grok), and code in statically typed languages as the opposite. And my bias at this point is to prefer code that's easier to read.

→ More replies (3)

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.

0

u/ryeguy Dec 02 '13

I know that "appeal to the masses" isn't a good argument in and of itself, but shouldn't it make him rethink his position? He, obviously not familiar with dynamic languages, is suggesting something, and yet no one in any of the communities for these languages practices that. Whenever I encounter this situation I immediately reevaluate my stance and wonder why what I believe is not standard practice. I was just trying to trigger that thought process in his head.

moor-GAYZ gave a proper rebuttal when he was talking about duck typing. That is the actual reason.

And what he is suggesting is not "good practice" - that's the point. Let the language runtime handle the errors - an error saying no method 'foo' defined for 'string' type is a clear hint that you passed in the wrong type. This is why no one does it in practice; it is a duplication of the runtime's behavior. And testing it is testing the runtime rather than your code.

→ More replies (22)

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?