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/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.

2

u/ForScale Dec 28 '18

Yeah, repeating code once or twice is fine.. but dear lord I'd hate to write the same 5 lines of code over and over beyond that. And I used to do that when I was starting out.. just didn't occur to me to put those five lines in a function and use that function instead of writing those 5 lines. Makes life a hell of a lot easier for me.

2

u/ScientificBeastMode strongly typed comments Dec 28 '18

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.

Yeah, you have to careful. I find it’s better to just keep a “utils” folder and add a few small abstractions as I work. Then as broader patterns emerge, it becomes more obvious which abstractions ought to be included in a library and shared.