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)

    Sanjay Gupta on Joe Rogan: yes, they shouldn't have said horse dewormer; yes, they shouldn't have done that; yes, I will talk to them. Sanjay…

  • (no subject)

    To the future me: I keep returning in my mind to the simple idea in this talk about containing React component's logic in a custom hook and having a…

  • (no subject)

    I've been listening to The Boys produced by the Graphic Audio. Better than the film; more irreverent; the Butcher character is even more profane and…

  • 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