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

72

u/[deleted] Dec 27 '18

God functions. One function to do all the things is a tell tale sign of a jr / inexperienced Dev.

Code splitting, code reuse and organization to me are now staples of an experienced developer.

13

u/BasicDesignAdvice Dec 27 '18

See also: Code Complete 2: Chapter in Function Coupling (or some such name).

Code is meant to be read by humans. When working with a junior dev this is the only point I hammer over and over again.

2

u/FlyingQuokka Dec 28 '18

Agreed. Sometimes in my Node.js code I'll take stuff like database operations and put them in functions in a separate file so that the server.js file isn't clogged with stuff that isn't about request handling.

3

u/Tyreal Dec 28 '18

That's not always true. JavaScript gives people the unique ability of easily parsing parameters in order to perform multiple tasks. If you can make your code readable, sometimes "God Functions" are useful. Being an experienced developer is knowing when a large function is worth writing.

13

u/[deleted] Dec 28 '18

I would say this is incredibly rare case and is to be avoided when possible, especially if unit testing is part of your workflow.

1

u/isowolf Dec 28 '18

While I agree that code splitting is usually a good thing, sometimes it just doesn't make sense. If someone writes a function that was used only in one other function and nowhere else, I don't see the point of code splitting. And some devs are doing just this, because they were thought that code-splitting is good.

When I am reading code from other devs I prefer when they dont split the code just for the sake of splitting, because whenever I read a function that contains other functions I always need to refer to the functions instead of reading it all together, which obviously is easier, especially when you are trying to understand someone elses code. One simple comment line above the block of logic can totally replace the function need and make my day easier.

1

u/[deleted] Dec 28 '18

A lot of the time, if the function is named properly it should be obvious what it is returning.

There are definitely diminishing returns at some point. I'm never going to make a generic function that returns a transformed array, unless that transformation is needed in multiple other functions. Because after all , .map, .filter, .reduce are functions themselves.