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

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)

20

u/tr14l Dec 27 '18

Bad jQuery? There's good jQuery? :P

12

u/brtt3000 Dec 28 '18

Sure, it is a just a tool to manipulate DOM.

2

u/CrimsonXFrost Dec 28 '18

I try not to write jQuery, but when I do, I make a mess of it. I’ve been doing this 15 years, and jQuery is still tough to get right the first time.

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

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.

24

u/djhalon Dec 27 '18

I like what I call "the rule of three". On the third time writing the same (or very similar) code, time to DRY it up. It's just a recommended rule of thumb that helps define that line for myself and the team to prevent early optimization and also help make sure you really understand what the code is doing and how it is being used before trying to abstract it.

There are definitely times where I know I will be reusing code on the first draft, so I just write it that way. The flip side is the dreaded "it almost looks the same" and when you try to DRY it up and it either becomes a configuration nightmare passing in all kinds of options or the code is branching/switch hell. Sometimes duplicated code really isn't that bad.

5

u/Rumicon Dec 28 '18

W.E.T -> write everything (only) twice.

0

u/[deleted] Dec 27 '18

[deleted]

3

u/AndrewGreenh Dec 28 '18

Just a small addition: you can accomplish the same by using interfaces. I think what one must get in Javascript or node is the usage of implicit (or explicit when using typescript) interfaces, but not only for objects but also for functions. Instead of having 3 different database implementations, why not have one generic one, that you can pass functions for saving/reading or something like that (strategy pattern).

→ More replies (3)

2

u/TheStonerStrategist Dec 28 '18

Sorry for the aside, but if you don't mind my asking, where/how did you learn the theory behind this sort of thought process? I don't mean this specific thing you're talking about, I just mean that it seems like you have a solid grasp of high-level programming concepts that are still pretty beyond me, and I'm wondering if this is just the type of understanding that only comes through many years of experience, or if I should be doing more reading/studying/video watching, etc. Like, I know what inheritance is, in the sense that I could provide a coherent answer if it was a question on an exam, but I don't think I could really enunciate its advantages/disadvantages or identify its ideal use cases like you just did. I think this sort of ability to reason about programming tactics and strategies is an area where I'm probably lacking.

5

u/oorza Dec 28 '18 edited Dec 28 '18

Mostly I've been doing this well over a decade now. When I was 18 or 19, someone told me a proverb that I have stuck to religiously and the older I get, the more I become a zealot for it: "Good developers read 10 lines of code for every 1 that they write." Reading, in this context, means reading and understanding, of course. I read almost all of the code of almost all of the dependencies that I use. I would read all the code that's consumed anywhere in my apps, but there's only so many hours in the day.

That said, it doesn't help to read code if you can't figure out why the right decisions were made or that the decisions made were sub-optimal. I didn't learn most of this shit in college because my family's finances collapsed in the 2000s and I didn't finish, so I read a ton of books. A lot of it is being able to read code, figure out what you can't understand, and figure out how to get help learning, so get in IRC or discord/slack/glitter and talk to the people that write the software you use the most and figure out why they do things the way that they do things.

Here's some books that I think are worth recommending. A lot of them are written against Java because there is almost nothing to the language itself, it's a very bare set of syntactical features, which makes it well suited for teaching concepts and abstractions.

  1. The Gang of Four is arguably the most important book in software architecture. Many of the patterns in the book are very common and will be very familiar to you, even if you've never seen them formalized before, because the book has had such an impact on the way people design software.

  2. Head First Design Patterns, despite the cringiest cover, is more or less the same information as the gang of four except that it's written to be read. If I had to do it all over again, I'd probably read this one really thoroughly to get an understanding of the concepts, and then hit the gang of four.

  3. Thinking In Java is ostensibly a book about learning how to write Java, which it is, but it's also the book best suited for teaching OOP thinking. OOP isn't just a set of tools, it's a set of tools that's best applied under certain assumptions about the way that data is structured and signals are communicated across an application. TIJ does a better job teaching the OOP way of modeling software than any other book I've come across. Java itself has changed quite a bit, and is largely superceded by Kotlin these days, but even still I'd recommend reading through the book if not working through the book.

  4. Java Concurrency in Practice is probably the least relevant think I'm gonna recommend, because again it's for java, but it starts with the assumption that you have a decent knowledge of Java and absolutely no knowledge of concurrency. It starts with primitives like mutexes and moves through them and into concurrent data structures, locking strategies, etc. Concurrency is a feature that's often omitted from high level languages (it's completely absent in JS, for instance) because it's difficult for developers to learn and understand because it's the most difficult thing in writing software. Learning at least the basics of how concurrent programming can work is helpful in architecting and designing every piece of software, because it makes you start thinking about stuff like the difference between calling setInterval and setTimeout to loop code on a timer, properly gating http requests so their responses get resolved in the order they were requested, etc.

  5. Clean Code and Clean Architecture assume that you're already a fairly competent programmer, which is why I listed them last, but they're also the most directly applicable to your question. "How do I know what bad code looks like versus good code? How do I refactor bad code to good code?" are the types of questions these books answer fairly thoroughly.

  6. Martin Fowler's Refactoring needs no blurb. Buy it, read it, grok it.

Honestly though, if you want to write good software, get the hell out of Javascript as fast as you can. There are no senior engineers anywhere in the Node community.

→ More replies (2)
→ More replies (1)
→ More replies (3)
→ More replies (3)

14

u/cm_yoder Dec 27 '18

No sarcasm just curiosity. DRY makes sense.

9

u/mindonshuffle Dec 27 '18

It's an important concept because repetition becomes a liability for maintenance. If you have something written five times in five different places, if you ever need to change it it's way too easy to miss an occurrence or even just introduce a bug when changing (especially if they aren't EXACTLY identical).

→ More replies (1)

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

0

u/[deleted] Dec 27 '18

[deleted]

0

u/[deleted] Dec 27 '18

[deleted]

→ More replies (1)

8

u/Ashanmaril Dec 27 '18

Do not repeat yourself

→ More replies (9)