r/ProgrammerHumor • • Aug 26 '20

Python goes brrrr

Post image
59.2k Upvotes

789 comments sorted by

View all comments

Show parent comments

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

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.

1

u/mr_tolkien Aug 26 '20

That’s what duck-typing is for.

1

u/00PT Aug 26 '20

Aren't javascript objects all just dictionaries essentially? You may not be able to represent the values exactly, but I don't see why you couldn't just do something like this: "{x:1,y:2.7,foo:function(bar)}"

→ More replies (1)

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.

2

u/[deleted] Aug 26 '20

I tried this with class Foobar{}; console.log(""+new Foobar()) in Firefox dev console, but that just gave me [object Object]. So it might be the built-in type of the object.

1

u/DeeSnow97 Aug 26 '20

It's actually an internal property that most JS classes don't modify, but there is a way to access it:

class Testing {}
Testing.prototype[Symbol.toStringTag] = "';DROP TABLE classes--"
console.log("robert" + new Testing())

This will log robert[object ';DROP TABLE classes--] to the console

7

u/maweki Aug 26 '20

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

1

u/[deleted] Aug 26 '20

Semantically, you are correct. However, if you go and read the V8 (or really, any performant implementation) source code, there is a separate integer type. Why? So that the JIT compiler can compile parts of the code to use integer arithmetic instead of floating point. Because it's just that much faster.

1

u/maweki Aug 26 '20

Yeah. We're not talking about an implementation detail of some VM or interpreter though. We're questioning whether some operator defined in the languages spec behaves as expected given the typing rules from the language spec.

And as you point out correctly, the semantics of the language should not depend on the implementation.

→ More replies (5)

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.

1

u/[deleted] Aug 26 '20

Well, I'm strictly in favour of statically typed languages, so IMO all this runtime dicking around is just a source for bugs and additional testing. Much better to have your compiler tell you that you're an idiot before the code ever runs.

3

u/not_perfect_yet Aug 26 '20

Why does that not make sense?

Python is very careful about which operations are supported by default.

Writing "five times the letter 'a'" as "5*'a'" or initializing lots of lists with "x * [1]" has use cases.

Allowing an implicit, warning less type version of a general object is stupid, because it will always pass when the user makes a typing mistake.

It makes syntactical sense, but one of the core themes of python is restricting the user in ways to prevent him from shooting himself in the foot by accident. Handing the user a supposedly strictly typed language, but allowing stuff like this make no sense.

→ More replies (5)

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)

2

u/Noisetorm_ Aug 26 '20 edited Aug 26 '20

That's when it's important to use the type checking operator though, when working between several datatypes like strings and numbers. If you did 8 + '8' === 88, it'd return false because it returns a string when expecting a number type with a value of 88.

It's also better practice to use template literals in Javascript than to use string concatenation though, which could make it easier for you to see where the error is, since it puts a dollar sign and brackets around the active parts of the expression.

EDIT: Although I'm kinda surprised that the language doesn't have a #warn mode or flag that tells you that type number is being converted to string on line 15 or whatever. 'Use strict' lets you catch some of these errors more easily but it's still not perfect.

→ More replies (1)

1

u/peduxe Aug 27 '20

that's why we're using TypeScript.

anyway I didn't know these were an issue, may be it's in my experience since i'm coming from languages that didn't allow this kind of operations and assignments having static and strong typing like C#/Rust so I'm always writing code to prevent my logic from having these errors beforehand.

Every language got it's pros & cons, it's a matter of playing with the language before you take on a big project.

JS might have these problems but there's some projects where having more liberty allows you to prototype solutions quicker.

I've been learning Rust and the compiler complains a lot regarding, mutable variables/immutable , borrows and lifetimes but you get used to it, same for JS and it's quirkiness.

→ More replies (4)