Live data from Hacker News

How JavaScript works: 3 types of polymorphism

blog.sessionstack.com

21–30 of 52 posts

Re: How JavaScript works: 3 types of polymorphism

#21

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…

Using 'classes' doesn't imply OO. They're a perfectly good way to organize data, at very least for data objects in TS.

It's fine to want to go functionally heavy, but if you're not using classes - which are the foundation of 'types' - then why bother with 'type'script? (Unless you mean to say 'interface'? Which is obviously not 'class' but implies the same usage of OO-founded keywords)

Re: How JavaScript works: 3 types of polymorphism

#23

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…

> a friend of mine insists that I always use classes and inheritance, and that functional style / composition is bad

If that is really what your friend is saying, I would not listen to him. The world (including the world of programming) is bigger than "always use / do X".

Re: How JavaScript works: 3 types of polymorphism

#24
post #11

Earlier quoted context omitted.

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 i…

Okay, "You don't know what you're talking about." :) There is a clear judgement on your part.

For me my own recent DK-moment was I'm learning a foreign language and in a shop asked for a simple item with a simple phrase, sure I had it right. They had no idea what I meant and I got angry because I thought you were just trolling me. Relating situation to a friend who let me know my accent was just wrong, so hard to understand. What got me is sometimes my accent good, sometimes it's not, day by day. But I was so sure, because my level is quite low. Haha

For you, with your judgement, you don't know whether it's true or not they are in DK, additionally you yourself may be mistaken in your JS criticism, it could be you that has DK, and they are correct.

Also, "You don't know what you're talking about.", OK, so, " this is how it works": generally when you criticize someone as doing X, and then backpaddle on pushback to say you do the same, you should recount a specific instance where you did X "and quite badly" because that shows you're genuine, and willing to be vulnerable, as you have tried to make the other person vulnerable by invoking the criticism you, by that omission, seem reluctant to invoke upon yourself. :) ;p xx

For me, a recent one where I didn't do that was a friend was telling me about their job offer progress, and salary negotiation. They wanted Y, but didn't say, and company was offering less. I told them how they should just ask for what they want, and try to get it. Friend pushed back, and got upset. Fast forward to same friend, next job offer, this time I shared a story how I hadn't pushed for my preferred salary in a previous job, then told how I felt not enjoying the job and feeling I was not getting fair reward each work day. Much better conversational outcome, friend more receptive to advice this time. Friend ended up with high salary they were happy with.

Better yet lead with the story of your mistake, tho that's hard to do. Either way, sharing your own makes you seem more credible in trying to bring someone up not put them down, shows you speak from experience, and engages someone emotionally into feeling for you rather than just being defensive :) ;p xx

Re: How JavaScript works: 3 types of polymorphism

#25

Earlier quoted context omitted.

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…

Using 'classes' doesn't imply OO. They're a perfectly good way to organize data, at very least for data objects in TS. It's fine to want to go functionally heavy, but if you're not using classes - which are the foundation of 'types' - then why bother with 'type'script? (Unless you mean to say 'interface'? Which is obviously not 'class' but implies the same usage of OO-founded keywords)

In TypeScript classes are in no way foundational. At the type level they are roughly implemented as a combination of two types: the instance type and the "newable" constructor function type.

Re: How JavaScript works: 3 types of polymorphism

#26

Earlier quoted context omitted.

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…

Using 'classes' doesn't imply OO. They're a perfectly good way to organize data, at very least for data objects in TS. It's fine to want to go functionally heavy, but if you're not using classes - which are the foundation of 'types' - then why bother with 'type'script? (Unless you mean to say 'interface'? Which is obviously not 'class' but implies the same usage of OO-founded keywords)

Typescript works perfectly well without classes, even better sometimes (see decorators and Typescript).

The shape of data is more foundational than classes. An object without a class is a perfectly good way to organize data too, based on its shape.

Re: How JavaScript works: 3 types of polymorphism

#27
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

> I'd say inheritance is NOT a good use-case for Person / Employee

Good call. If I wanted Person distinct from Employee, how I'd probably start, in an analysis model, is have Employment as an association/relationship between Person and Company.

In my meta-model, associations like Employment can have attributes, and that might be where things like "employee number" and "salary" come in.

And, taking that a little further, we can represent the history of how things like salary change over time.

(Meanwhile, a project that went with only an Employee record/object might not have saved any coding time initially, and they're quickly piling on expensive, hard-to-maintain kludges, just to meet evolving business needs. Even the first time an employee got a raise, they might've already been hitting incorrectness cases, just trying to get two parts of the system to use the right salary for the right time.)

Re: How JavaScript works: 3 types of polymorphism

#28
post #7
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

I'd say that inheritance, with method overriding, is the worst anti-pattern of mainstream OOP, which contains a number of otherwise sound ideas, like encapsulation. I like the Go's approach to that (taken from Oberon), and the Rust's system of traits. In one large codebase I saw there was a rule to make classes either abstract (without implementation of key parts), or final. Everything else is done via interfaces and…

Hooray Traits! Love them, they are even in PHP so I can use them in these older apps I maintain.

Re: How JavaScript works: 3 types of polymorphism

#29
I've never developed for the web out of fear of talking employers into recruiting me as a UI/UX designer. I've also been pretty thrown off over Web Development being known as a massive mess.

I've worked on desktop applications in C# so far. Is web development anymore pleasant or is it all just muckwork?

Re: How JavaScript works: 3 types of polymorphism

#30
As someone quite in love with functional programming (background in Haskell and Scala), one thing that seems quite obviously missing here and in my Javascript experience so far is the use of typeclasses as a form of ad-hoc polymorphism.

Is there a nice (i.e. not too much boilerplate) way of using them in the land of Javascript/Typescript?

Post reply on HN