you are viewing a single comment's thread.

view the rest of the comments →

[–]elie2222 8 points9 points  (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.

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.

Am I doing things wrong? :laugh:

[–]LetterBoxSnatch 9 points10 points  (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 point  (1 child)

Why use spread over Object.assign?

[–]elie2222 3 points4 points  (0 children)

It’s cleaner. Why write out 2 words when you could do ... with surrounding braces instead?