r/javascript • • Jun 17 '18

My most hated JavaScript features – Craig Taub – Medium

https://medium.com/@tabu_craig/my-worst-javascript-features-465378d5ad43
0 Upvotes

11 comments sorted by

19

u/notlmn Jun 17 '18

Why do people just hate at every OO part of JS?

  • null - How would someone represent emptiness

  • typeof - Yes, there are some standard issues

  • ASI - Semicolons should've been mandatory from the beginning

  • Floating point integers - What other standard would you choose, considering JS is loosely typed?

  • ES6 class binding - Its just one thing to remember in event based architecture (mostly backwards compat)

  • Pass-by-ref and Immutability - Your users memory is limited (they don't have infinite amount of RAM)

4

u/hansolo669 Jun 17 '18

null is the big one that bothers me ... Given other languages with actual optional types it feels so clunky. But you really need to structural the whole language and standardlib around optionals for it to make any kind of reasonable sense.

7

u/notlmn Jun 17 '18

null is not something new, it has been around for ages (from the origins of C).

undefined is actually the extra thing in JS, which again is pretty much self explanatory.

People think that they have a problem with something, where they actually don't.

2

u/hansolo669 Jun 17 '18

Sure, I'm not disputing any of that. However, just because something's been around forever doesn't mean there aren't better alternatives.

5

u/skerit Jun 17 '18

I don't understand his problem with null, though I could understand people confusing null and undefined, which is also a valid value.

The only real typeof quirk is typeof null. All the others make a lot of sense. A data, an array, a regex, ... they're all objects.

The pass-by-ref thing is interesting, because JS actually DOES NOT pass anything by reference, or at least not the way other languages do it.

In other languages: when an argument is passed by reference, the caller and callee use the same variable for the argument. If the callee changes the argument (by assigning a different value to it, for example), the same will happen to the variable in the caller.

So in that sense, JS passes by value, the values are mostly mutable objects though.

Maybe the confusion comes from the way PHP does this? By default it also passes values by value, but it always passes objects in a copy-on-write way.

2

u/[deleted] Jun 17 '18 edited Jun 17 '18

Call by value is not correct either. The term is call by sharing.

It's not call by value because that would mean that the function receives a copy with no relation to the original, and none of the changes would be visible in the caller's scope. That's obviously not the case, since changes to object properties are propagated to the caller.

It's not call by reference because that would mean you can assign to the variable in the function and the assignment would persist in the caller's scope. That's also not the case, since (re)assignment in a function has no effect on the variable in the caller.

Call by sharing works like this:

  • It passes variables by reference, but breaks the reference if the function attempts to reassign.

  • As long as you don't break the reference by reassigning, you can modify variables' properties.

    • Naturally, this only works for mutable variables, so not for any of the primitive data types.

    • Potentially confusing, in JS for most of the primitives there is also a related built-in fundamental object eg. number and Number. A variable created with new Number() is an object, not a primitive of type number, so it will behave like an object.

0

u/perpetuallyperpetual Jun 17 '18

null - How would someone represent emptiness

Maybe monad? Why do you need emptiness?

Floating point integers - What other standard would you choose, considering JS is loosely typed?

Big integer?

Pass-by-ref and Immutability - Your users memory is limited (they don't have infinite amount of RAM)

Still would be nice to have them by default instead of the other way. Something like Scala's val and var would've been really cool.

But, yeah, I agree that there are no good reasons to hate on OOP. But I guess going full FP makes you seem edgy but still mainstream.

0

u/Serializedrequests Jun 17 '18

null

- How would someone represent emptiness

Can't speak for the article, but JavaScript has TWO null types for some godforsaken reason. Obviously I understand the difference, but it makes for very poor developer ergonomics, and creates a pointless question of "which type of null should I use".

Now I prefer an optional type, but null can and should absolutely have better developer ergonomics than it does.

6

u/inabahare Jun 17 '18

I didn't really read more after the section about null. It just seemed so silly to me. Like this

Default arguments treat Null as valid, i.e. pass Null into someFunc(someParam = 'default value') and someParam will be Null.

Like what is this supposed to show? If you passed 1 as a parameter then someParam would be null.

And then this

For instance say you wanted to store if a person has hayfever, how would you store it on initialisation?

Which doesn't really seem like a good example since by the sound of it you would need a bool, and in the "Hence why so many languages (C/C++ and Java included) use it [null]" list you list languages which doesn't have implicit types, and for a bool the default value would be false rather than null in those languages.

4

u/[deleted] Jun 17 '18

"How to use a hammer as a toothbrush" - Craig Taub

2

u/Georiv Jun 17 '18

I read the entire article, but I found myself shrugging with almost every point. The only one I empathized with is the numbers.