Live data from Hacker News

How JavaScript works: 3 types of polymorphism

blog.sessionstack.com

11–20 of 52 posts

Re: How JavaScript works: 3 types of polymorphism

#11
post #9

There is a lot of wrong in this article: inability to articulate what polymorphism is, misunderstanding of prototypes, bad JS practices (`var`, `.__proto__`), falsehoods on programming languages including JS itself, confusion around ECS, unfinished examples... I suspect the author is in the early stages of their learning journey and Dunning-Kruger is in full effect.

You might be right but your last sentence is the reason why people are apprehensive about sharing their work and writing more articles. There's no need for it.

There is no judgment in what I said. DK is a fact of life and we will all experience it at some point. I know I have, and quite badly.

I wish when I was younger someone had told me "look, you don't really know what you're talking about, here's how things really work". If no one tells you, how long does it take for you to realize and how much time do you waste thinking you know what you don't know? Again, the author is at the start of their learning journey. I'm not saying they're a bad person or a hopeless case.

And finally, people should not be apprehensive about sharing their work in general, but I think they should be when writing articles that purport to teach others.

Re: How JavaScript works: 3 types of polymorphism

#12
As someone new to JS, a friend of mine insists that I always use classes and inheritance, and that functional style / composition is bad. However, most of the newer libraries I read through all seem pretty functional, whereas the older ones are full of classes. Personally, I find myself writing everything with factory functions and composition because it feels natural to the way I think about code.

Am I wrong, and should I be listening to my friend? He has much more experience than me.

Re: How JavaScript works: 3 types of polymorphism

#13

As someone new to JS, a friend of mine insists that I always use classes and inheritance, and that functional style / composition is bad. However, most of the newer libraries I read through all seem pretty functional, whereas the older ones are full of classes. Personally, I find myself writing everything with factory functions and composition because it feels natural to the way I think about code. Am I wrong, and sh…

Pure functions, polymorphism, inheritance, composition, factory functions, classes... All of those are tools. They have benefits and drawbacks that are circumstantial (they depend on the language, the problem, the time pressure to ship, the skill level of the team, and many more other factors). Extremely successful codebases have been written using almost any combination of those tools. What those codebases have in common is that the people who wrote them understood their tools. So if I were you I would strive for understanding.

In that spirit, it's good that you question what experienced people tell you. What about asking your friend to explain why, at a fundamental level, their recommendation is good? "Always use classes and inheritance" as a hard-and-fast rule is not very helpful.

Re: How JavaScript works: 3 types of polymorphism

#14

As someone new to JS, a friend of mine insists that I always use classes and inheritance, and that functional style / composition is bad. However, most of the newer libraries I read through all seem pretty functional, whereas the older ones are full of classes. Personally, I find myself writing everything with factory functions and composition because it feels natural to the way I think about code. Am I wrong, and sh…

I’m mostly the opposite: classes in JavaScript feel like a kludge invented to pretend JavaScript’s version of OOP is what people were familiar with in languages like Python, Java and C++.

If I’m going to write OOP JS, I generally prefer to use object literals and Object.create. However, I’ve mostly switched to the “Unix philosophy” style that libraries like Ramda promote: lots of sharp tools that are composed to build your programs.

Re: How JavaScript works: 3 types of polymorphism

#15

As someone new to JS, a friend of mine insists that I always use classes and inheritance, and that functional style / composition is bad. However, most of the newer libraries I read through all seem pretty functional, whereas the older ones are full of classes. Personally, I find myself writing everything with factory functions and composition because it feels natural to the way I think about code. Am I wrong, and sh…

In my personal experience, the class syntax specifically gets used very infrequently. Proper prototypal inheritance was more common “back in the day” but my own preference and I _think_ what a lot of others are leaning towards these days is composition.

Learn to use both and make up your own mind!

Re: How JavaScript works: 3 types of polymorphism

#16
post #3

I'd say inheritance is NOT a good use-case for Person / Employee A person can play many roles, at the same time. Composition is better in this case. A person has many roles Inheritance is better for types of legal parties though (see The Party Model). A person is a legal party. An company is an organisation is a legal party

[deleted]

Re: How JavaScript works: 3 types of polymorphism

#17

As someone new to JS, a friend of mine insists that I always use classes and inheritance, and that functional style / composition is bad. However, most of the newer libraries I read through all seem pretty functional, whereas the older ones are full of classes. Personally, I find myself writing everything with factory functions and composition because it feels natural to the way I think about code. Am I wrong, and sh…

I've been getting paid to write JS/TS for about 10 years. I think I've used the `class` keyword less than 5 times in the last 5 years and feel better off without it. I've found that a more functional style leads to better separation of state/data from any logic you have to write, allowing for a better testing experience and an easier time refactoring. Plus it means I have to think way less about what `this` means.

At the end of the day, features of languages are just tools, and sometimes they fit the problem you're trying to solve. I think it's also important to decide what tools to use in the context of what experience you and your team already have.

Also, it's hard to have small .js files when you use classes, and I love small files. I think Go made a great design decision to allow functions on structs to be implemented in separate files.

Re: How JavaScript works: 3 types of polymorphism

#19
post #9

There is a lot of wrong in this article: inability to articulate what polymorphism is, misunderstanding of prototypes, bad JS practices (`var`, `.__proto__`), falsehoods on programming languages including JS itself, confusion around ECS, unfinished examples... I suspect the author is in the early stages of their learning journey and Dunning-Kruger is in full effect.

The problem is JS - it's been a fuzzy bit of confusion since inception, it was never remotely intended to do what it's doing, and nary a tiny fraction of JS developers could authoritatively explain the differences between prototype __proto__ etc. - I keep a chart handy as a reminder and much like C++ stick to a set of rules to avoid unknowns.

There's a reasonable chance that with a much more succinct and clean '20/20 hindsight' the articles like this would be either clear, or frankly unnecessary.

Re: How JavaScript works: 3 types of polymorphism

#20

As someone new to JS, a friend of mine insists that I always use classes and inheritance, and that functional style / composition is bad. However, most of the newer libraries I read through all seem pretty functional, whereas the older ones are full of classes. Personally, I find myself writing everything with factory functions and composition because it feels natural to the way I think about code. Am I wrong, and sh…

I've been getting paid to write JS/TS for about 10 years. I think I've used the `class` keyword less than 5 times in the last 5 years and feel better off without it. I've found that a more functional style leads to better separation of state/data from any logic you have to write, allowing for a better testing experience and an easier time refactoring. Plus it means I have to think way less about what `this` means. At…

I once heard someone tell me that good OOP ends up looking very functional. I've taken that to heart, and I believe the code I write is better as a result.

I do use the `class` keyword in my js, but I'm also on a team of C++/Ruby programmers who are forced to write js, so the more I can do to make it more familiar, the better. As you said, it's just a tool. Some people like nails and some people like screws; there are objective advantages to each, but either one will hold a house together.

Post reply on HN