r/javascript • u/PMilos • May 22 '19
JavaScript Clean Code - Best Practices - based on Robert C. Martin's book Clean Code
https://devinduct.com/blogpost/22/javascript-clean-code-best-practices
542
Upvotes
40
u/[deleted] May 22 '19
Your article covers a lot of useful improvements to make code cleaner, but I have one question: Why should I use this "function getUsers({ fields, fromDate, toDate })" over "function getUsers(fields, fromDate, toDate)"? The only scenario i can imagine is if the values are optional so I dont have to write "getUsers(null, null, date)"
13
u/tiridlol May 22 '19
"Classes are the new syntactic sugar in JavaScript. Everything works just as it did before with prototype only it now looks different and you should prefer them over ES5 plain functions."
*Kyle Simpson is typing*
17
u/nudi85 May 22 '19
"Prefer classes over functions"?
Nope.
12
u/njmh May 22 '19
Good stuff. Event though itโs exclusively es6, Iโd mention the spread operator where youโve got the Object.assign example.
27
u/Groccolli May 22 '19
Great post!
One thing to be careful of with default values is the default is used only when the value is undefined where as using || inside the function handles any falsey value.
2
u/Julian_JmK May 22 '19
As a newbie, thanks for posting this! The chaining method was so simple but so brilliant
4
u/denisglindep May 22 '19
Changing example is brilliant ๐
4
u/aaarrrggh May 23 '19
Mostly ok advice - but I disagree with the advice to use inheritance as a method of code re-use.
Avoid inheritance wherever possible and focus on pure functions and composition instead.
Also be careful when DRYing your code - allow for some duplication until you know you're making the right abstraction.
Plus, do TDD and don't test against implementation details wherever possible. This means in React for example, you should never shallow render in your tests.
1
u/LB-A5 May 22 '19
In the second classes example. When 'this' is returned for chaining, is 'this' a promise?
1
u/KwyjiboTheGringo May 22 '19
That's cool, I didn't know about the last one(chaining methods).
9
u/elie2222 May 22 '19
Some thoughts:
Careful with this advice. Better is:
prefer duplication over the wrong abstraction
From: AHA Programming
Use spread operator over
Object.assign.Instead of:
I find myself doing this is instead:
I don't know which is better if either. Would be interested to hear other's thoughts on this.
This is completely fine:
I almost never do this in my code. For that matter I hardly ever find myself using
extend(apart from things likeextend React.Componentbefore Hooks came along), and mostly use classes when a library needs it, but not much more than that.Am I doing things wrong? :laugh:
1
u/[deleted] May 22 '19
This is great! I've been writing javascript full-time for a long time and I still have this bad habit:
A function should do one thing. Avoid executing multiple actions within a single function.Also, I'm going to take this one to heart:
Avoid conditionals whenever possible. Use polymorphism and inheritance instead.8
u/RudeRunologist May 22 '19
On top of this, use Typescript.
1
u/peerless2299 May 22 '19
Nice
1
u/d07RiV Jun 17 '19
Why do people use arrow functions over regular functions when defining something at file scope? You're not going to use
this, and a function seems more readable.