this post was submitted on 04 Jun 2025
974 points (98.5% liked)

Programmer Humor

23844 readers
3768 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 2 years ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[โ€“] avidamoeba@lemmy.ca 196 points 2 days ago* (last edited 2 days ago) (6 children)

This is too stupid so I had to check.

Fuck me.

[โ€“] Gsus4@mander.xyz 106 points 2 days ago* (last edited 2 days ago) (3 children)

Hm, playing devil's advocate, I think it is because the minus has not been defined as a string operation (e.g. it could pop the last char), so it defaults to the mathematical operation and converts both inputs into ints.

The first is assumed to be a concat because one of the parcels is a string...

It's just doing a lot of stuff for you that it shouldn't be in first place ๐Ÿคญ

[โ€“] avidamoeba@lemmy.ca 39 points 2 days ago (1 children)

Yup. It's completely inconsistent in its interpretation of the + operator.

[โ€“] Gsus4@mander.xyz 20 points 2 days ago* (last edited 2 days ago) (1 children)

Yeah, I actually had to try 1+"11" to check that it didn't give me 12, but thankfully ~~it commutes~~ it's consistent ๐Ÿ˜‡

[โ€“] palordrolap@fedia.io 19 points 2 days ago (1 children)

it commutes

Maybe the behaviour with regard to type conversion, but not for the operation itself.

"13"+12 and 12+"13" don't yield the same result.

[โ€“] raspberriesareyummy@lemmy.world 6 points 2 days ago (1 children)

Nor would I expect "1312" to equal "1213".. Still that operator with these operands should just throw an exception

[โ€“] palordrolap@fedia.io 4 points 2 days ago (2 children)

Given it's JavaScript, which was expressly designed to carry on regardless, I could see an argument for it returning NaN, (or silently doing what Perl does, like I mention in a different comment) but then there'd have to be an entirely different way of concatenating strings.

expressly designed to carry on regardless

I'm surprised they didn't borrow On Error Resume Next from Visual Basic. Which was wrongly considered to be the worst thing in Visual Basic - when the real worst thing was On Error Resume. On Error Resume Next at least moved on to the next line of code when an error occurred; On Error Resume just executed the error-generating line again ... and again ... and again ... and again ...

[โ€“] raspberriesareyummy@lemmy.world 4 points 2 days ago (1 children)

Why would you need an entirely different way of concatenating strings? "11" + 1 -> exception. "11" + to_string(1) = "111"

[โ€“] palordrolap@fedia.io 1 points 2 days ago (1 children)

You're right. I've got too much Perl on the brain and forgot my roots. There is a language that does what you're talking about with the '+' operator: BASIC

Good luck getting the same thing retrofitted into JavaScript though. I can imagine a large number of websites would break or develop mysterious problems if this (mis)behaviour was fixed.

I don't think there's a way to retrofit JS - but php versions are deprecated all the time. Why not do the same with client-side script versions? :)

[โ€“] dalekcaan@lemm.ee 12 points 2 days ago (1 children)

Yeah, this looks dumb on the surface, but you've got bigger problems if you're trying to do math with strings

[โ€“] Klear@sh.itjust.works 4 points 1 day ago

Better than doing physics with strings

[โ€“] 0x0@lemmy.zip 12 points 2 days ago

It's just doing a lot of stuff for you that it shouldn't be in first place ๐Ÿคญ

Kinda like log4j!

[โ€“] jol@discuss.tchncs.de 35 points 2 days ago

From all the Javascript quiks this is the least stupid and the most obvious.

[โ€“] irelephant@programming.dev 24 points 2 days ago (2 children)
[โ€“] wasabi@feddit.org 29 points 2 days ago (2 children)
[โ€“] ChickenLadyLovesLife@lemmy.world 5 points 2 days ago (1 children)

That is absolutely 2 * ("ba" + 0/0 + "a")

[โ€“] Matombo@feddit.org 7 points 2 days ago

(n > 1) * ("ba" + 0/0 + "a")

Uncaught ReferenceError: n is not defined

?

[โ€“] Gsus4@mander.xyz 1 points 2 days ago
[โ€“] Alexstarfire@lemmy.world 21 points 2 days ago (2 children)

Unfortunately, it makes sense if you know what + means, which is concatenate. - is strictly a math function though.

Not saying that makes this better. It just makes sense.

[โ€“] grue@lemmy.world 25 points 2 days ago* (last edited 2 days ago) (3 children)

It is 'comprehensible' in the sense that it's possible to figure out how it happened, but it absolutely does not "make sense" in terms of being a reasonable language design decision. It's 100% incompetence on the part of the person who created Javascript.

[โ€“] Alexstarfire@lemmy.world 8 points 2 days ago (1 children)

I mean, I'd never try to do this anyway because if the types aren't the same unexpected things can happen. That's like programming 101.

[โ€“] grue@lemmy.world 11 points 2 days ago (1 children)

Exactly, which is why designing the language to allow it is incompetence.

[โ€“] fushuan@lemm.ee 2 points 2 days ago (1 children)

It makes perfect sense if the Lang objective is to fail as little as possible. It picks the left side object, checks if the operand is a valid operand of the type. If it is, it casts the right variable into that type and perform the operand. If it isn't, it reverses operand positions and tries again.

The issue here is more the fact that + is used both as addition and as concatenation with different data types. Well, not an issue, just some people will complain.

[โ€“] grue@lemmy.world 2 points 1 day ago

Computing a nonsensical result is itself a failure. Continuing to run while avoiding giving an error in that case accomplishes nothing but to make the program harder to debug.

Thanks for saving me the typing.

[โ€“] Tb0n3@sh.itjust.works 11 points 2 days ago* (last edited 2 days ago) (1 children)

It's an issue with most if not all languages that aren't strongly typed.

[โ€“] palordrolap@fedia.io 5 points 2 days ago (1 children)

Perl is an old but notable exception. + is purely for addition in the base language.

If you try to add two strings with it, they'll be converted to numbers based on any number-like characters they have at their left hand ends, and, if warnings are enabled (and you should definitely do that), you'll get runtime warnings about it if there's even anything vaguely non-numeric about them.

e.g. "1"+"11" will get you 12 with no complaint, warnings or otherwise. Not even the string "12" either, although it's hard to determine one from the other in Perl. It's a need-to-know kind of thing. And you generally don't.

"a"+"bb" gives 0 as the result because they're not numbers and "1a"+"11bb" will give 12, but these latter two will give warnings. Two each, in fact, one for each dodgy parameter.

String concatenation is done with the dot operator instead. "1"."11" gives "111". This comes with it's own minor problems, but at least + is safe.

[โ€“] nyan@lemmy.cafe 3 points 1 day ago (1 children)

That's because Perl doesn't do operator overloading in general. Even the equality operator is different for strings (eq instead of ==). As a language, it may look pretty weird and lack some modern features, but the underlying design is surprisingly intelligent and consistent in many ways.

[โ€“] palordrolap@fedia.io 1 points 1 day ago

Not strictly true.

Perl's default bitwise operators do differentiate between numbers and strings that look like numbers in a way that addition doesn't*, and the readline/glob operator <> does different things depending on what (if anything) is between the signs.

There's also the whole overload pragma for objects, which doesn't affect default data types, but if you're sufficiently perverse, you can define a String class that uses '+' like JavaScript.

* in 2015, they added new operators so that those and the original operators don't overload and have only one specific purpose if the bitwise ~~pragma~~ Edit: feature is turned on. You might know all this already though.

[โ€“] CidVicious@sh.itjust.works 12 points 2 days ago

I think I'm on the side of "if you do this in your code, you deserve what you get."