Andrey (azangru) wrote,

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!

  • (no subject)

    (retweeted by a developer) How does she interpret what she has highlighted — "unlawful blocking of a public street" — as "pedestrians whose…

  • (no subject)

    Google has finally become ready to promote its Lit ecosystem properly. I thought they were going make their announcements at the coming Google IO;…

  • (no subject)

    Is it ... Bill Maher? Is he, in the penetrating eyes of this insightful lady, the emblem of white supremacy now? The comments to the tweet say it…

  • Post a new comment


    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.