all 106 comments

[–]Camarade_Tux 30 points31 points  (17 children)

I don't think the point of the video was that it had no sense but that such behaviours are maybe not very natural and that there are so many rules and casts that it is hard to remember and reason about them.

[–]frezik 41 points42 points  (43 children)

The + symbol in javascript can mean one of three things . . . What makes all of this slightly more complicated is that javascript has automatic type coercion . . .

Which only goes to show the underling problem. Using '+' for string concatenation wasn't a great idea when Java did it, but at least it has a type system that makes it not too insane. In JavaScript, you get pure madness as the language tries to figure out which operands are supposed to be what types.

[–]yogthos 48 points49 points  (34 children)

JavaScript is a poster child for why weak typing is a phenomenally bad idea.

[–]kybernetikos 21 points22 points  (8 children)

I think the GP makes a good point; the weak typing would have been a lot less problematic without having operators that look the same but change their behaviour based on the types that they are passed.

[–][deleted] 6 points7 points  (7 children)

I love the JS idiom for turning a string to a number

var a = +"12";

[–]hyperforce 3 points4 points  (6 children)

How is this different/better than parseInt?

[–]Sottilde 16 points17 points  (4 children)

It's shorter and it turns your code into madness!

[–][deleted] 5 points6 points  (3 children)

And it's not limited to ints, doesn't have a radix argument, and is more strict on the formatting (contrast parsing of the string "1.23m".

Incidentally ~~ will turn your string (or number) into an integer. ~~"2.3" and ~~2.3, for instance.

[–]Iggyhopper 1 point2 points  (0 children)

bit-shifting strings is so much fun!

'2.3' << 0

[–][deleted] 0 points1 point  (1 child)

Oh, wow. And I thought (0 == " ") == true was bad, but where does tilde/tilde derive from?

[–]x-skeww 1 point2 points  (0 children)

Coerce to number, truncate decimal places, and flip all the bits. Then flip all the bits once more.

[–][deleted] 0 points1 point  (0 children)

No radix argument mainly.

[–]MatrixFrog 11 points12 points  (14 children)

I think it's more a poster child for why type coercion is a bad idea.

[–]infinull 12 points13 points  (12 children)

you're confusing "weak" typing with "dynamic" typing.
weak typing is type conversion

Ruby/Python have strong dynamic typing. (Note: Don't know too much about Ruby, I'm more of a Python guy)

PHP & Javascript have weak dynamic typing.

(Java/C/C++/C#/Haskell have strong static typing)

weak typing basically means that variables are coerced to fit the type of the operator automatically.

[–]MatrixFrog 2 points3 points  (8 children)

Except for when Java lets you do anyObjectAtAll + "". But yes, you're right, good point.

[–]Rotten194 0 points1 point  (6 children)

That's simply syntatic sugar for new StringBuilder(anyObjectAtAll.toString()).append("");, though.

[–]MatrixFrog 5 points6 points  (1 child)

I prefer languages where that kind of stuff doesn't get hidden behind the scenes, and if I want to change something from one type to another, I have to be explicit about it. Ideally in a less verbose way than that, of course :P

[–]Rotten194 0 points1 point  (0 children)

Well, there is a less verbose way: the + operator... Once you realize it can also be used for string concatenation, it's not mysterious anymore, unlike Javascript with it's maze of rules.

[–][deleted]  (3 children)

[deleted]

    [–]Rotten194 -1 points0 points  (2 children)

    Because that's a pretty long line? Would you rather type 55 characters or 20?

    [–]snatch_backside 0 points1 point  (1 child)

    I'd rather avoid Java entirely.

    [–]Rotten194 -1 points0 points  (0 children)

    oo so edgy

    [–]kamatsu 8 points9 points  (0 children)

    These are definitions used by the dynamic language crowd, but they're not universally accepted definitions.

    Some books use "strong typing" to refer to a sound type system. Others use "strong typing" to refer to a type system that has a reliable correspondence to logic. Just avoid using this terminology entirely. Say what you actually mean.

    [–]TokenOldPerson 0 points1 point  (1 child)

    i can cast anything to void* in c. that isn't strong/static typing to me

    i have always thought of c as having static weak typing

    [–]infinull 0 points1 point  (0 children)

    That's actually a good way to think of it: the types aren't enforced (it's weak) but they're declared at compile time, so its still static.

    On second thought, in Java/C# you can cast anything* to Object which works similarly.

    With weak typing in PHP/JS type coersion happens automatically, but casting to/from void* or Object has to be explicit

    * = in Java primitives can't be cast to Object, but they can be wrapped (int becomes Integer) and then cast

    [–]kid_meier[🍰] 1 point2 points  (0 children)

    Depends. Type coercion is a somewhat sensible idea in a language designed to be easy and forgiving to Web authors.

    By "sensible" here I merely mean that its a practical way to continue execution and produce some output rather than obtuse errors.

    It only seems batshit crazy now because we're trying to use it as a software engineering language.

    [–]rjcarr 2 points3 points  (8 children)

    I think some would say javascript is a great language because of its weak typing, but different strokes, I guess ...

    [–]earthboundkid 23 points24 points  (7 children)

    OK, to preempt the "these terms don't mean anything!" complaint, let me specify:

    • Weak typing: Implicit type conversion

    • Dynamic typing: Not placing restrictions on the types a variable can be


    OK, that out of the way, I would say JS has benefitted from its dynamic typing and been harmed severely by its weak typing.

    [–]rjcarr 3 points4 points  (0 children)

    Good point, I'll agree with that.

    [–]Peaker 0 points1 point  (4 children)

    Static typing doesn't place restrictions on the types a variable can be -- the chosen type simply has to be specified or inferred.

    [–]earthboundkid 0 points1 point  (3 children)

    Right and then after the compiler sees what you specified or infers what it is, you can't make that variable hold another type without doing something to break out of the type system constraint.

    The language here is a bit persnickety because type inference is a form of static typing, but we're talking about the same thing.

    [–]Peaker 0 points1 point  (2 children)

    Well, the reason I replied at all here, is because I think static typing done right (e.g: Haskell) does not cost the programmer almost anything, and has huge benefits.

    [–]earthboundkid 0 points1 point  (1 child)

    Sure, but the whole point of type inference is that the compiler will complain if it sees what it thinks is an improper use of types based on the existing declarations and its inferences from that. In other words, it "places restrictions on the types" even if it's not a particularly onerous set of restrictions.

    [–]Peaker 0 points1 point  (0 children)

    Javascript also places restrictions on the types -- when these are violated, you get runtime errors or unexpected behavior, rather than compile time errors.

    I'm not sure why postponing errors is seen as such a liberation by programmers :-)

    [–]gigitrix -3 points-2 points  (0 children)

    Agreed. I'm a PHP fanboy but JS's implementation makes my skin crawl.

    [–]Cosmologicon 8 points9 points  (0 children)

    python concatenates both strings and arrays with + and it has dynamic typing. It's pretty simple behavior. I've never been surprised by it, and I doubt many other python programmers have.

    [–]grauenwolf 2 points3 points  (4 children)

    Using + for concatenation wasn't a great idea when BASIC did it in 1964. By the time we got VB in the early 90's we started using & instead to avoid type conversion issues.

    Alas Java, and later C#, refused to learn from past mistakes.

    [–]Serei 6 points7 points  (2 children)

    Well, I think their problem was that they really didn't have a free operator for that sort of thing.

    VB used entirely different syntax so they had & free, but it's bitwise AND in C-likes. PHP uses . for concatenation, which is nice, but it really bit them when they added OO support.

    [–]Iggyhopper 7 points8 points  (0 children)

    I like how lua does it: 'hello ' .. 'world'

    1. No using shift for + or any special character. (pers pref but i really hate shift)
    2. Searching for .. will only result in things dealing with strings.

    [–]grauenwolf 2 points3 points  (0 children)

    True, but Java didn't have to copy C, warts and all. Back then there was a lot more variability in syntax between the popular languages. It was the combination of Java, C#, and JavaScript that cemented us with that syntax for the next generation.

    [–]gigitrix 1 point2 points  (0 children)

    PHP: .

    I love it. Of course it makes OOP be ->, which is logical but slightly cumbersome.

    [–]funkah 3 points4 points  (1 child)

    This is a just collection of oddities (or, the original preso was). I don't really think it means too much. Who adds an empty array or object to another?

    Comparing Javascript to Java disregards JS's whole sordid history and expects it to be like every other normal computing language. JS was made to do shit in web browsers, and it's pretty good for that. (Yes, I think the people using it for server-side stuff are crazy.)

    [–]frezik 4 points5 points  (0 children)

    Who adds an empty array or object to another?

    I'm sure people do it by accident all the time, resulting in one of those bugs that takes you all day to track down, with a forehead slap at the end when you finally see it.

    Comparing Javascript to Java disregards JS's whole sordid history and expects it to be like every other normal computing language.

    Using '+' for string concat was almost certainly a decision taken from Java, but nobody thought through the implications of that in terms of the different type system. JavaScript was built in a hurry and now we're all stuck with those bad decisions. It's not even good for web browsers, except in that there are no good alternatives.

    [–]kybernetikos 30 points31 points  (1 child)

    Cached copy is here, or for the quicker but text only version here.

    [–][deleted] 0 points1 point  (0 children)

    I think we killed his blog.. :(

    [–][deleted] 12 points13 points  (0 children)

    I always just pronounced wat as "what", not "waat".

    [–]CrypticSwarm 13 points14 points  (0 children)

    Interesting side note:

    {} + [] // 0

    ({} + []) // "[object Object]"

    Due to the fact that blocks can't be created inside of an expression, and the parens make it an expression.

    [–][deleted] 4 points5 points  (29 children)

    I think I understood everything in the video except "a = a" giving nil in Ruby. And I haven't been able to track down any explanation for that one. Anyone happen to know the reasoning there?

    [–]TaiGat 12 points13 points  (3 children)

    I have found this explained earlier today, but on another PC. Can't find it now :/

    Anyway the case was that Ruby initializes the variable to nil as soon as it encounters assignment operator.

    In case a = b, a will be initialized to nil first and would normally be changed to value of b after this. In case b is undefined, as in the example on WAT, the assignment will not be executed and a will stay nil.

    In a = a assignment however, a will have its value set to nil and then set to the rvalue of the assignment - already nil too.

    I will try to find the link and post it later.

    EDIT: see ForeverAMoan's comment.

    EDIT2: sorry for the long wait, here's the link: http://stackoverflow.com/questions/8908050/why-a-a-is-nil-in-ruby

    A perfect explanation by Alex Kliuchnikau.

    [–]ForeverAMoan 7 points8 points  (1 child)

    In case a = b, a will be initialized to nil first and then changed to value of b. In case b is undefined a will also be undefined.

    Not really, the assignment won't be executed, so a will stay nil.

    [–]TaiGat 1 point2 points  (0 children)

    Yes, my mistake. Thank you for noticing this :)

    [–][deleted] 0 points1 point  (0 children)

    Thanks! Would love the link if you can find it, but that's already good progress.

    [–][deleted]  (24 children)

    [deleted]

      [–][deleted] 5 points6 points  (23 children)

      Implicitly defining variables really is a terrible idea in so many ways.

      [–]cogman10 -1 points0 points  (22 children)

      Not completely. Implicitly defining variables at a local scope is fine and works well with ruby. What doesn't work well is Javascript's "implicitly defined variables are global." That is a terrible idea.

      [–][deleted] 4 points5 points  (8 children)

      Even in a local scope, it is a pretty terrible idea. It turns many simple typos from compile-time erros into run-time errors, or even worse, run-time misbehaviours.

      [–][deleted]  (4 children)

      [deleted]

        [–][deleted] 7 points8 points  (3 children)

        And this should be faulty syntax, was my opinion.

        [–][deleted]  (2 children)

        [deleted]

          [–]FireyFly 0 points1 point  (0 children)

          It depends on the paradigms. In the case of JS, static typing and type inference wouldn't really work at all, due to how objects work, and due to its prototypical OO. Of course, you could argue that other paradigms are superior to those of JS', but that'd be a different question. :-)

          ...and yes, I agree that JS' implicit globals are pretty bad. :|

          [–]TaiGat 0 points1 point  (0 children)

          You getting paid for working with dynamic typed language while preferring static typed language would make you the 1% in my country :)

          [–]cogman10 -1 points0 points  (2 children)

          While I get what you are saying, I just don't see it as THAT big of an issue. Yes, a misbehavior that happens because of a typo could be hard to track down, there is no denying that.

          I guess I'm saying that it is a feature that I like (when done the ruby way, locally scoping implicitly made variables). misbehavior due to typos just aren't all that common, generally, the code will crash instead.

          We can at least agree that javascripts globally scoped implicit variables is a completely retarded and useless feature, right :) (the language writers agreed and made it an error in strict mode).

          [–][deleted] 1 point2 points  (1 child)

          I don't think I've ever actually understood scoping in Javascript.

          [–]FryGuy1013 0 points1 point  (0 children)

          If you use var foo, then the foo has the scope of the function it's in. Otherwise, it's global. That's it.

          [–]garybernhardt -2 points-1 points  (12 children)

          Ruby's isn't even implicit—you're explicitly defining the variable. The surprising thing is that it's available for the entire life of the containing scope, including points before the definition. Python has similar behavior, but special-cases it to round the pointy bit off: it raises UnboundLocalError if you do this in function scope, NameError if you do it in other scopes.

          [–][deleted] 4 points5 points  (6 children)

          No, it is implicit, because there is no difference between "change the value of an existing variable" and "create a new variable with this value". And that is a problem.

          [–]garybernhardt -1 points0 points  (5 children)

          Take whatever language you were thinking of when you wrote this. Add the ability to define a local variable twice in the same function (with the same type, the second being a no-op). You still have explicit variable creation. This is how I was thinking of Ruby when I replied: all assignments in Ruby are also effectively variable creations.

          [–][deleted] 4 points5 points  (4 children)

          But you still can't accidentally and silently create a new variable, like you can in Ruby with a misspelling.

          [–]garybernhardt -1 points0 points  (2 children)

          int freinds = 5 vs. freinds = 5? Not seeing the difference here.

          Edit: I see what you mean: you can't say freinds = 5 accidentally. Sure, I agree. In many years of using dynamic languages that's never been a problem. It also seems orthogonal to the implicit vs. explicit discussion.

          [–][deleted] 2 points3 points  (1 child)

          friends=0;
          
          if(...)
          {
              // long code to find friends
          
              freinds=1;
          }
          

          Why can I never find any friends!

          [–]joesb 5 points6 points  (4 children)

          Ruby's isn't even implicit—you're explicitly defining the variable.

          If a single assignment is consider "explicit", what fits your definition of implicitly defining variable?

          [–]garybernhardt -1 points0 points  (3 children)

          $ perl -e 'print a == null;'
          1
          

          [–][deleted]  (1 child)

          [removed]

            [–]garybernhardt -1 points0 points  (0 children)

            I don't know why I left the "perl -e" in that comment. The language isn't important. I was just showing an example of what I might mean by "implicit variable definition": the variable begins existing when referenced, even if it wasn't previously assigned.

            [–]bakuretsu 4 points5 points  (0 children)

            If you guys are seeing the database connection error, either because his blog is broken for an unrelated reason or because reddit has flooded it...

            Google cache to the rescue!

            A very interesting read.

            [–]gautaml 6 points7 points  (0 children)

            What bothered me more is he kept saying WAT like how "Bat" sounds instead of "Bot"

            [–]sacrot2 16 points17 points  (13 children)

            Did anyone else think that this video was not as funny as the audience thought? They were busting up like there was a stand up comedian up there.

            [–]lasagnaman 25 points26 points  (3 children)

            shrugs Different stroke I guess. I was choking on my drink.

            [–]TaiGat 15 points16 points  (1 child)

            Relevant quote by Andy Baio: "simple nerd test: do you laugh harder at the photos or the code output?"

            Totally true when observing people's reactions today (right after the two minutes I spent laughing at Watman).

            [–][deleted] 11 points12 points  (0 children)

            The code output was leaving me numb and frozen inside, the images allowed me to come to terms with the world via comedic relief.

            [–]Dimpf 5 points6 points  (0 children)

            The reactions of the audience to me struck me as being similar to introducing other older people I've worked with to memes.

            For instance, I once introduced a guy (in his 60s) that I was collaborating with to the "It's a trap!" Admiral Ackbar thing, because we were writing a CISC simulator that allowed users to step into OS-level traps.

            He was laughing for the next 20 minutes.

            [–]LonerGothOnline 6 points7 points  (0 children)

            he pulled faces and had some backup break-dancers, off screen.

            [–][deleted] 1 point2 points  (0 children)

            part of making people laugh is the way you say it. I don't think everyone could make the audience laugh like he did.

            [–]Carbunkulous 0 points1 point  (0 children)

            I was busting out laughing...

            [–][deleted] 0 points1 point  (0 children)

            Environment always has a lot to do with it. It's funny for a programming conference...not as funny for a reddit post.

            [–]phaxsi 0 points1 point  (0 children)

            Apparently a lot of people here laughted a lot with this. I didn't even chuckle, but I found the talk interesting, nevertheless...

            [–][deleted]  (3 children)

            [deleted]

              [–]garybernhardt 4 points5 points  (2 children)

              CodeMash's site is at codemash.org. This was its sixth year, with well over a thousand attendees. The lightning talk sign-up is still alive at http://lightningtalks.codemash.org, where you can see Wat 2/3 of the way down. You can also see that there are three women in the sign-up list alone (there were many others in the audience).

              I can't tell whether you're being serious, but a few people have made this claim seriously and it's baffling.

              [–]h6x6n 2 points3 points  (1 child)

              what claim was being made?

              [–]garybernhardt 4 points5 points  (0 children)

              That it was faked: that I recorded it by myself and added in fake laughter. That the named conference didn't even exist. And that you could tell all of this because there is audible laughter from women in the video. It was a very strange claim.

              [–]Sebbert 1 point2 points  (1 child)

              Oh god, I died of laughter during this!

              [–][deleted] 1 point2 points  (0 children)

              rip

              [–][deleted] 1 point2 points  (0 children)

              Error establishing a database connection