Andrey (azangru) wrote,
Andrey
azangru

This is a moment that makes me want to scream.

An instructor is explaining a bit of code written using D3:



She is plotting a diagram of temperatures in a given city over a year:



She chose San Francisco, the data for which is stored as numbers (as opposed to strings for other cities).

When asked about the double plus in the last line of the loop (line 28):



she says that its purpose is to cast strings to integers (no, floats, she corrects herself).

This is wrong on so many levels!

Firstly, the ++ operator is shorthand for adding 1 (as anyone who has a passing acquaintance with a C-like language will attest).

Second, it is indeed customary to cast JavaScript strings to numbers (JS wouldn't care whether it's an integer or a float) by prepending a single plus to the string. But this is an ugly hack not immediately obvious to those unfamiliar with this unfortunate idiom. It is much cleaner (and self-explanatory) to use the Number constructor for this purpose (e.g. Number('1')).

Third, the operation on line 28 does not have any effect whatsoever, because its result is not assigned to anything (had it been assigned, it would have produced incorrect results because of the wrong operator). This line might not have even been there, and there wouldn't have been any difference.

How, how, how generally reasonable people make such brain farts?! Arrrgh!
Subscribe

  • (no subject)

    Someone's comment on Twitter reminded me how differently we interpret what we experience: My impression from that interview was that Yuri was…

  • (no subject)

    Via Twitter. From the book The Gray Lady Winked, photographed by Michael Shermer: Here's the tweet that the paragraph references (took a while…

  • (no subject)

    To unscramble an egg, by the way, would be a closer idiomatic parallel to ungrinding ground meat, and in spite of Julia Ioffe, is just as dark, blunt…

  • Post a new comment

    Error

    default userpic
    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 4 comments