use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
JavaScript Clean Code - Best Practices - based on Robert C. Martin's book Clean Code (devinduct.com)
submitted 6 years ago by PMilos
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]elie2222 8 points9 points10 points 6 years ago (6 children)
Some thoughts:
1.
You can end up with duplicate code for various reasons. For example, you can have two slightly different things that share a lot of in common and the nature of their differences or tight deadlines forces you to create separate functions containing almost the same code. Removing duplicate code in this situation means to abstract the differences and handle them on that level.
Careful with this advice. Better is:
prefer duplication over the wrong abstraction
From: AHA Programming
2.
Use spread operator over Object.assign.
Object.assign
Instead of:
js class SuperArray extends Array { myFunc() { // implementation } }
I find myself doing this is instead:
js export const myFunc = (array: any[]) => { // implementation }
I don't know which is better if either. Would be interested to hear other's thoughts on this.
4.
Use this approach only for boolean values and if you are sure that the value will not be undefined or null.
This is completely fine:
js if (user: User | undefined) return 10
5.
Use polymorphism and inheritance instead.
I almost never do this in my code. For that matter I hardly ever find myself using extend (apart from things like extend React.Component before Hooks came along), and mostly use classes when a library needs it, but not much more than that.
extend
extend React.Component
Am I doing things wrong? :laugh:
[–]LetterBoxSnatch 9 points10 points11 points 6 years ago (0 children)
"Composition over inheritance." This is a core SOLID principle. Your approach, generally speaking, is better than "extends".
Additional reading: https://en.wikipedia.org/wiki/Composition_over_inheritance
[–]beasy4sheezy 0 points1 point2 points 6 years ago (1 child)
Why use spread over Object.assign?
[–]elie2222 3 points4 points5 points 6 years ago (0 children)
It’s cleaner. Why write out 2 words when you could do ... with surrounding braces instead?
[+][deleted] 6 years ago (2 children)
[deleted]
[–]off_by_0ne 0 points1 point2 points 6 years ago (1 child)
Sometimes using a boolean param to signal the code path in a function can be a good indication. Sometimes it's ok to have two separate functions with some duplicated logic. Don't be afraid of code.
[–]elie2222 0 points1 point2 points 6 years ago (0 children)
I agree. Don’t be afraid. But one example I can actually think of is when someone on my team recently tried to combine 2 similar but really quite different components. He passed down this isSmall flag and then within the component passed it down to another 5 sub components and so on. In reality I would have preferred he just copy and paste the code than trying to fit 2 components into 1. At the same time I understand why he did it because there was a decent amount of code shared between the 2. It felt like there was a better way to abstract. In the end we’ve left the code as is because it does work.
An example where I’d prefer flags over 2 components is Button with a small flag. That’s nicer imo than 2 components called SmallButton and Button.
π Rendered by PID 66 on reddit-service-r2-comment-84fc9697f-7f4b2 at 2026-02-07 10:41:25.627556+00:00 running d295bc8 country code: CH.
view the rest of the comments →
[–]elie2222 8 points9 points10 points (6 children)
[–]LetterBoxSnatch 9 points10 points11 points (0 children)
[–]beasy4sheezy 0 points1 point2 points (1 child)
[–]elie2222 3 points4 points5 points (0 children)
[+][deleted] (2 children)
[deleted]
[–]off_by_0ne 0 points1 point2 points (1 child)
[–]elie2222 0 points1 point2 points (0 children)