r/ProgrammerHumor • • Aug 26 '20

Python goes brrrr

Post image
59.2k Upvotes

789 comments sorted by

View all comments

Show parent comments

121

u/[deleted] Aug 26 '20

Wondering though, why do people consider this a good thing in Python but a bad thing in JS?

0

u/[deleted] Aug 26 '20

[deleted]

15

u/Yskinator Aug 26 '20

This. In python everything you'd intuitively expect to work will work for the most part, and things that make no sense result in exceptions. JS, on the other hand..

12

u/g0liadkin Aug 26 '20

I mean, that's the biggest pillar of js: it will run most stuff and do its best or yield errors in the console

For some reason people love bringing up examples like adding objects to arrays and saying "omg jabbascreept so random lol"

There are some cases where it's REALLY annoying though — e.g. typeof null being object

→ More replies (8)

0

u/[deleted] Aug 26 '20

[deleted]

→ More replies (12)

66

u/Tarmen Aug 26 '20

I think the problem is more with the cases that make no sense but still don't error

> "b" + {}
"b[object Object]"

42

u/[deleted] Aug 26 '20 edited Aug 26 '20

Why does that not make sense? Adding an object to a string implicitly converts the object to a string and then concatenates the two strings, and the default conversion for object is "[object Object]" if .toString() isn't defined.

Next you're going to tell me that 5 + 1.0 should also error because it implicitly promotes an integer to a double.

Edit: so this comment is dragging out all of the butthurt python fanbois. Here's the deal: your pretty little scripting language was written with auxiliary operating system tasks in mind, and was later adopted by statisticians and mathematicians. Of course it has hard typing and extensive error handling.

But JavaScript was originally integrated into HTML. That's frontend. Frontend needs implicit string conversions, because typing str(some_var) over and over again gets real annoying, real fast. "10" == 10 is a bit more arguable, but I suppose it has its use in validating input. Also, when you have a user on your frontend, the last thing you want is everything to come crashing down because of some formatting error in a string that gets shown to the user maybe once in a blue moon. There's probably some performance reasons for the way things are as well, because V8 compiles hot code into machine code - I imagine it's cheaper to just have a toString() defined to return a constant somewhere instead of checking for nullptr and slinging errors around...

In any case, Lua is, objectively, the best scripting language.

38

u/mrchaotica Aug 26 '20

It's not that JavaScript has syntax that isn't able to be figured out, it's that it makes a bunch of bad design choices that aren't useful.

I mean, Malbolge or Brainfuck "make sense" in the same way too, but that doesn't make them good languages to use!

→ More replies (12)

5

u/marmoshet Aug 26 '20

That's it, I'm learning JS

→ More replies (1)

10

u/00PT Aug 26 '20

Honestly, that's an extremely non-specific and unhelpful representation of an object. I would like it much more if they converted it to JSON (shouldn't be that hard, since they already parse JSON as part of the language).

10

u/zedpowa Aug 26 '20

Not every object can be converted to JSON

3

u/_PM_ME_PANGOLINS_ Aug 26 '20

The majority of objects probably can’t be represented in JSON. You need a way to reference the prototype, function members, frozen attributes, etc.

→ More replies (3)

5

u/DeeSnow97 Aug 26 '20

The second "Object" is actually the class of the object:

"b" + (new Event('hello')) // "b[object Event]"

So there is some logic to it, it's just not the best logic there could be. Would it make more sense to JSON-stringify it? Yes, hella yes. But the problem is, JavaScript actually came first, not JSON, the object to string casting is a core part of the language, and JS is completely backwards-compatible (it kinda has to, a breaking change would potentially break the entire web). So, unfortunately, there is no way to change that now.

→ More replies (2)

7

u/maweki Aug 26 '20

You do know that JS only has a single number type, right?! There is no promotion going on.

→ More replies (7)

4

u/bra_c_ket Aug 26 '20

It makes sense in that it's a well-defined behavior, but you would never actually want that behavior. You'd be far more likely to do something like that accidentally, but since JS likes to do implicit type conversions you wouldn't necessarily notice that you'd made a mistake. Far better to get a type error and a stack trace so you know you've made a mistake and find the exact line it's caused by.

→ More replies (1)
→ More replies (6)

0

u/[deleted] Aug 26 '20

[deleted]

9

u/livedadevil Aug 26 '20

Those make sense though because the quotes define the number as a string? I don't see the problem

6

u/Traches Aug 26 '20

Because javascript isn't strongly typed, which means if a string sneaks in somewhere, this bug could easily bite you in the ass without raising any errors and being hard to track down.

→ More replies (2)
→ More replies (2)
→ More replies (6)

8

u/marmoshet Aug 26 '20

JS is not as pythonic as python

7

u/Thomasedv Aug 26 '20

I think js is more scary on where it implicitly changes one thing to the something else. Not that python doesn't have similarly grave issues, but some of the stuff it does by itself become issues that aren't immediately seen and breakes something else later on.

Python is a little more strict on what you can have interact, eg. string + number is a no go, but in JS, the string might be converted to an number and added to the number, if it's possible.

6

u/pslessard Aug 26 '20

I don't think anyone would have a problem with multiplying a string by a number in JS. That's a reasonable thing

6

u/Alikont Aug 26 '20

Maybe because you have a choice to not use Python. People who hate it just don't use it.

You don't have a choice to not use JS because it has monopoly on browsers.

0

u/[deleted] Aug 26 '20

[deleted]

→ More replies (2)

4

u/Pluckerpluck Aug 26 '20

I think others have answered, but the main thing people dislike in Javascript regarding this is the really aggresive string coercion that Javascript performs. It also happily doesn't error when things go wrong a lot of the time. Want to multiply a string by another string? Well that doesn't error, it just returns NaN.

This allows beautiful lines such as this one which I'll let you try to work out how it works.

Python doesn't allow any of this. Each interaction is explicitly defined.

[1] * 4 = [1, 1, 1, 1]
"a" * 4 = "aaaa"

You also see this in things like list/array addition:

# Python
[1, 2, 3] + [4, 5, 6] => [1, 2, 3, 4, 5, 6]

// Javascript
[1, 2, 3] + [4, 5, 6] => "1,2,34,5,6"

There's also some ridiculous errors to try and debug in rare situation:

let myVariable = null
let x = parseInt(myVariable, 24)

// x => 23

The above is a real interesting one. You are asking Javascript to convert a base 24 number (in string form) into a decimal number. In this case, N or n is a valid character in base 24 and it equals 23. null is transformed into the string "null" and then Javascript will just stop, without an error, when it hits the u which is a letter it doesn't recognise in base 24 (so again, no erroring).

Basically, that's why people dislike it in Javascript.

2

u/Al2Me6 Aug 26 '20

Because there’s no implicit type coercion involved.

A string is considered iterable in Python, and multiplication of an iterable by a number is explicitly defined as repetition of that iterable.

→ More replies (9)