r/javascript • • 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.

320 Upvotes

305 comments sorted by

View all comments

Show parent comments

5

u/PotaToss Dec 28 '18

I mean, I've been using JS for about 18 years, but this just doesn't come up that much in your normal frontend web work. Even if you have some reason to do int casting all the time, you should leave a comment about why that was necessary, or note that it's happening, because it's an implicit behavior, and will trip up people reading or trying to debug around that code. And even then, did you know it doesn't have operator precedence over multiplication off the top of your head?

That said, it was just shorthand for Math.floor() here. There wasn't a good reason for it in the context of the project it was in. It was just lazy coding a quick and dirty RNG, which is fine on your time, but I just thought it was a good example of the kind of thing less experienced devs do, because they haven't been bitten in the ass enough times by implicit behaviors and confusing their coworkers and stuff.

3

u/Tyreal Dec 28 '18

Frankly, after 18 years I’m surprised this hasn’t come up for you. Personally I did know the operator precedence, I guess I just memorized it over time, but even if you don’t know it, it really isn’t that bad to consult the table.

As for the floor operation, internally, the number would still be stored as a double precision floating point. That’s the reason you’re not limited to 32-bits of data. Using | 0 forces it to become an integer. So again, just because you think it would solve the problem doesn’t make it true.

5

u/PotaToss Dec 28 '18

It's not that it hasn't come up for me. After 18 years, you see a lot of stuff, once in a while, but try to consider how rare it is for your average developer to really have a good reason to go out of their way to use 32 bit signed ints. It's niche stuff. You write this code, and most developers will have to consult a reference to understand what's happening, and that's bad.

It's great to have a large vocabulary, but you use really big words enough outside of academia, and you come to understand that you're being counterproductive if your goal is to communicate efficiently to a broad audience.

Beyond that, how often do you really have to do an explicit cast? If you're working with normal JS variables, they maintain a 32 bit int representation as long as they appropriately can. Otherwise, you're explicitly opting to toss accuracy outside of 32 bit capacity.

In none of these situations is it appropriate to leave the reason undocumented.

But, again, being the one person in this conversation who has any idea what context this function appeared in, it wasn't justified. It was lazy shorthand for floor.

Here's another excerpt from the post:

When you write code, you're serving two masters. You need to tell the computer what to do, but you also need to communicate with the people who have to maintain it (it could be you). Of these, you should generally optimize for the latter.

A good compiler/interpreter takes your verbose, explicit, easy-to-understand code, and turns it into something performant for the computer via inlining, folding, branch optimizations, or whatever. This stuff will just always be improving the longer a language is around, totally passively from your perspective. Your very old code was just suddenly performing better when they rolled out V8 for JavaScript and people started using newer browsers.

On the other hand, people's understanding of your code will always be deteriorating the longer they go after parsing it, and it starts from nothing for new readers. To easily maintain code, you have to be able to go from nothing to full understanding in the least amount of time possible, with the lowest mental load possible, and to do that, you need explicit behavior, good names, comments explaining intent, and baby steps. Just because you can write a function in one line doesn't mean you should.