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

262

u/ForScale Dec 27 '18

Bad naming.

Not keeping things DRY.

Massive functions that do many things.

Not caching static values but instead querying them over and over again.

133

u/cynicaloctopus Dec 27 '18

I agree with everything except the last point. It can be tempting to pre-optimize, but caches tend to be bug farms. Instead focus on getting performance data, then act on that if you need to.

50

u/robotmayo Dec 27 '18

They might mean doing object.value or function(sameValue) 10x instead of just storing it into a variable. Is it optimization? Yes. But its easy and free so why not.

55

u/brtt3000 Dec 27 '18

You see this a lot in bad jQuery or DOM querySelector code, where instead of assigning the result of the query to a variable and reuse that for different operations they keep querying the same thing over and over.

18

u/ForScale Dec 28 '18

Precisely what I was referring to.

→ More replies (1)
→ More replies (8)

9

u/cm_yoder Dec 27 '18

DRY??

52

u/mindonshuffle Dec 27 '18

Not sure if sarcasm, but "Don't Repeat Yourself." Basically, if there's code you're repeating more than couple times in different places, you should probably refactor that into a named variable or function you can call.

→ More replies (21)

13

u/Mettalicron Dec 27 '18

Do not Repeat Yourself

14

u/robotomatic Dec 27 '18

Do not repeat yourself

13

u/musicnothing Dec 27 '18

Do not repeat yourself

→ More replies (3)
→ More replies (1)
→ More replies (9)

25

u/Pstrnil Dec 27 '18

Most jquery code I see suffers from the last point. Thankfully it's easy to fix.

Overall good points

0

u/[deleted] Dec 28 '18

[deleted]

10

u/ForScale Dec 28 '18

Lol! Don't think I'd heard that before. :)

13

u/enkideridu Dec 28 '18

I've also heard of WET as "Write Everything Twice"

21

u/qudat Dec 27 '18 edited Dec 28 '18

Not keeping things DRY.

It's funny because I would consider prematurely DRYing every piece of code a sign of an engineer who is following "standard practices" without weighing the pros/cons.

I saw DRY on the RoR site therefore everything must be DRY

I think "always DRY" is a sign of someone who isn't thinking for themselves. For me, DRY should only happen after ~3 instances of duplication and certainly never before that.

Write code that is easy to delete, not easy to extend.

I think this quote rings quite true for me. As engineers we always see the abstractions and I fear we seek it out when it isn't necessary.

11

u/mypetocean Dec 27 '18 edited Dec 28 '18

This is a rarer problem among junior devs in my experience than the lack of concern for such best practice rules of thumb.

And of the two, I'd rather have a developer who has habitualized thinking about best practices and architectural generalization opportunities (who therefore often over-engineers) than a developer who lacks that metacognitive discipline.

The former is learning where to stop generalizing. The latter hasn't begun to learn to generalize.

→ More replies (3)

14

u/jonny_eh Dec 27 '18

This guy is actually senior. Knowing when to ignore "best practices" is the sign of an expert.

17

u/littlebluebrown Dec 27 '18

The beginner does not know about best practices. The intermediate will follow them too mechanically. Only the master knows when to break the rules.

3

u/ForScale Dec 28 '18

What are the pros of writing the same code over and over again?

I dont know who you're quoting with he "always dry ror" thing, it's not me.

It's cleaner to cache and more performant in certain situations. Store the value of querySelectorAll, don't query the dom and rebuild an identical array like html collection every time.

5

u/qudat Dec 28 '18

What are the pros of writing the same code over and over again?

Abstracting a piece of code has maintenance burden. If that code is never used more than once or twice, it's more maintainable to copy/paste.

https://programmingisterrible.com/post/139222674273/write-code-that-is-easy-to-delete-not-easy-to

Building reusable code is something that’s easier to do in hindsight with a couple of examples of use in the code base, than foresight of ones you might want later. On the plus side, you’re probably re-using a lot of code already by just using the file-system, why worry that much? A little redundancy is healthy.

It’s good to copy-paste code a couple of times, rather than making a library function, just to get a handle on how it will be used. Once you make something a shared API, you make it harder to change.

The code that calls your function will rely on both the intentional and the unintentional behaviours of the implementation behind it. The programmers using your function will not rely on what you document, but what they observe.

→ More replies (2)
→ More replies (4)

0

u/[deleted] Dec 27 '18

[deleted]

3

u/MonkeyNin Dec 28 '18

one char names

Maybe he doesn't know how to use autocomplete.

If you want to mess with him, rename a 1 letter variable with a similar looking Unicode character.

Even worse, something like this

https://i.imgur.com/CBPYPpM.png

→ More replies (8)

3

u/jasofalcon Dec 28 '18

These points are good but I would just note that those are overall good principles of software design, not only JS

→ More replies (13)