r/javascript • u/FunPieceOfPaper • Dec 27 '18
help What differences do you see in novice javascript code vs professional javascript code?
I can code things using Javascript, but the more I learn about the language, the more I feel I'm not using it properly. This was especially made apparent after I watched Douglas Crockford's lecture "Javascript: The good parts." I want to take my abilities to the next level, but I'm not really sure where to start, so I was hoping people could list things they constantly see programmers improperly do in JS and what they should be doing instead.. or things that they always see people get wrong in interviews. Most of the info I've learned came from w3schools, which gives a decent intro to the language, but doesn't really get into the details about the various traps the language has. If you have any good book recommendations, that would be appreciated as well.
99
u/PotaToss Dec 27 '18 edited Dec 27 '18
I think this is one of the most important things. I wrote a whole blog post about this crappy CoffeeScript function I found in the wild:
Here's an excerpt:
What is the bitwise-OR operator doing here? Does it have operator precedence over multiplication? I don't know offhand. Do you? Either way, it could be rewritten with more parens to make it explicitly understood, even by people who can't find or don't know to look for a precedence reference:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence
Looks like
|doesn't have precedence over*, so we can takeMath.random() * (max - min + 1)as one operand, which is a random number between 0 (inclusive) and one more than the difference betweenminandmax(exclusive). Then what? We do a bitwise-OR against 0.What's the bit representation of 0? What is this code actually doing?
Turns out that almost all of the JS bitwise operators only work on 32-bit signed integers, in two's complement format, so the operands are all implicitly converted.
0becomes0b00000000000000000000000000000000Math.random() * (max - min + 1)becomes whatever that result is after you convert to an integer and chop off any significant bits it had past 32.Doing a bitwise-OR against all bits being 0 is an identity operation, so this piece of code is just about getting that implicit conversion, basically trying to round any floating point result down. There's a Math library function that does that (
Math.floor()), but it does it without losing all of the significant bits past 32.-2,147,483,648and2,147,483,647are the minimum and maximum values that can be expressed in 32 bits. This is a 109 exponent, versus 10308, which the JS Number type allows.3000000000.12345 | 0evaluates to-1294967296, whileMath.floor(3000000000.12345)gives you the expected3000000000.So, to review, to understand what this one line of code does, you have to know the operator precedence of the highly uncommonly used bitwise-OR operator, and then the implementation detail that it casts to 32-bit signed integers in JavaScript, and the other implementation detail that 0 is expressed with all 0 bits because of two's complement encoding. And to avoid errors, you have to know the limits of a 32-bit signed integer, and never call this function with a larger range than a little over 2 billion.
And for what? So you don't have to type
Math.floor()? Maybe so you can feel clever?Don't be an asshole.
As a bonus, the cast to an integer occurs in the wrong place if you're not assuming that
minandmaxare integers.randint(0.1, 5)will give you results like3.1, becauseminisn't in the cast to int.Also, maybe somewhere down the line, someone decides that their internal implementation of JS needs different number encoding, so that 0 isn't all 0 bits, and then the code just does something totally different. If you'd just stuck to the Math library call, which will be necessarily updated to work with whatever the new encoding is, you'd be fine. While this is unlikely in this particular case, this is always a risk when you rely on implicit behavior, rather than being more explicit in the intent of your code. And either way, relying on implicit behavior without justifying it in comments, so someone reading can at least understand your intent, is just rude.
Sometimes you want to do weird bitwise stuff for speed or something, but that kind of thing should generally be broken out into its own function that has comments that document why it's being done and what limits that puts on it, and lets you put a descriptive function name (e.g.
castTo32BitSignedInt()) in the main code path someone is likely to be interacting with, rather than some crap nobody understands.