this post was submitted on 04 Jun 2025
1014 points (98.6% liked)

Programmer Humor

23855 readers
2537 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
 
top 50 comments
sorted by: hot top controversial new old
[–] avidamoeba@lemmy.ca 202 points 2 days ago* (last edited 2 days ago) (6 children)

This is too stupid so I had to check.

collapsed inline media

Fuck me.

[–] Gsus4@mander.xyz 113 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 40 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 (6 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.

load more comments (6 replies)
[–] 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

load more comments (1 replies)
[–] 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 38 points 2 days ago

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

[–] irelephant@programming.dev 29 points 2 days ago (2 children)
[–] wasabi@feddit.org 35 points 2 days ago (3 children)
load more comments (3 replies)
load more comments (1 replies)
[–] 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 27 points 2 days ago* (last edited 2 days ago) (4 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 12 points 2 days ago (1 children)

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

load more comments (1 replies)
load more comments (3 replies)
[–] Tb0n3@sh.itjust.works 11 points 2 days ago* (last edited 2 days ago) (3 children)

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

load more comments (3 replies)
[–] 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."

[–] RobotZap10000@feddit.nl 63 points 2 days ago (1 children)

[object Object][object Object]

[–] zqwzzle@lemmy.ca 12 points 2 days ago

The fun strings to enter in web forms once in a while.

[–] whaleross@lemmy.world 43 points 2 days ago (3 children)

People that try to do mathematical operations with strings blaming the programming language that had a stated design goal to do its best and try to keep running scripts that make no sense because they realized it would be used by people that have no idea what they are doing. Clearly they were right.

[–] grue@lemmy.world 37 points 2 days ago (1 children)

the programming language that had a stated design goal to do its best and try to keep running scripts that make no sense...

...itself makes no sense. It is wrong and bad that Javascript was ever designed that way in the first place.

[–] whaleross@lemmy.world 22 points 2 days ago (1 children)

It was never intended to run full applications but only the small business scripts and hobbyist homepage stuff that were the thing in the 90s, across inconsistent browsers that were a jungle of hit and miss behaviour where it was preferred that menus keep working even if the mouse effect was not. Anything of scale was expected to be done in Java. Dynamic web pages did not exist and as anything not static was generated server side into a static html file to be rendered on the client.

Anyway, back then it wasn't considered the job of the programming language to hold the hand of the aspiring developer as it is common today. It's not a bad thing that IDE and even compilers and preprocessors try to help you write better code today, but then it simply didn't exist.

JavaScript is from a different time and because it has the hard requirement or backwards compatibility there is no changing it and has not been for thirty years except to add stuff to it.

I think it's just silly to ask the past to keep up with the present. Bad code is not the fault of the language regardless, even though junior devs and even seasoned ones like to think so to protect their ego. I think it is better to accept it, learn from it and roll with it because every single platform and language has their weird quirks anyway.

Signed, old dude that learned programming in 8 bit BASIC and 6502 machine code without an assembler, where code bad enough would freeze your machine that required a cold boot and starting over from your last save that you didn't do.

Executing after undefined behavior is arguably worse than terminating with an exception. A terminated script can't leak data or wreak havoc in other ways.

[–] 0x0@lemmy.zip 11 points 2 days ago (5 children)

it would be used by people that have no idea what they are doing. Clearly

And so let's enable these people?
Let's add AI to the mix while we're at it.

load more comments (5 replies)
load more comments (1 replies)
[–] capybara@lemm.ee 32 points 2 days ago (1 children)

To start off... Using arithmetic operators on strings in combination with integers is a pure skill issue. Let's disregard this.

If you were to use + where one part is a string, it's natural to assume a string appending is desired since + is commonly used as a function for this. On the other hand, - is never used for any string operation. Therefore, it's safe to assume that it relates to actual artihmetics and any strings should therefore be converted to numerical values.

This is an issue with untyped languages. If you don't like it, use typescript. End of story.

[–] Jankatarch@lemmy.world 31 points 2 days ago (11 children)

Instead of trying to make it work, javascript could just say "error." Being untyped doesn't mean you can't have error messages.

load more comments (11 replies)
[–] REDACTED@infosec.pub 27 points 2 days ago (2 children)

Imagine doing math with strings and then blaming the language not yourself

[–] zagaberoo@sopuli.xyz 43 points 2 days ago (11 children)

The risk is when it happens unintentionally. The language is bad for hiding such errors by being overly 'helpful' in assuming intent.

load more comments (11 replies)
[–] gmtom@lemmy.world 15 points 1 day ago

The problem is consistency.

[–] kubica@fedia.io 24 points 2 days ago (2 children)

Lets fix it. I think that since we are removing the ones, then "11" - 1 should be equal to "".

[–] r00ty@kbin.life 15 points 2 days ago (5 children)

Should it, or should it be "1"? (just removing one, one)

load more comments (5 replies)
load more comments (1 replies)
[–] MinusPi@pawb.social 23 points 2 days ago (2 children)

If you mix types like that, it's your own fault

[–] raspberriesareyummy@lemmy.world 35 points 2 days ago (20 children)

BS. A language shouldn't have operators that allow non sensical operations like string concatenation when one operand is not a string.

load more comments (20 replies)
[–] Shanmugha@lemmy.world 9 points 2 days ago (2 children)

Lol. In a dynamically typed language? I will do this always, that's why I am using it

load more comments (2 replies)
[–] python@lemmy.world 20 points 2 days ago (1 children)
[–] masterspace@lemmy.ca 11 points 2 days ago* (last edited 2 days ago)

This is a really good interview, and does a good job highlighting Javascript's biggest strength: it's flexibility.

β€œIt was also an incredible rush job, so there were mistakes in it. Something that I think is important about it is that I knew there would be mistakes, and there would be gaps, so I made it very malleable as a language.”

He cites the β€œdiscovery” of asm.js inside of JavaScript, calling it β€œanother thing I’m particularly proud of in the last 10 years.” It uses the bitwise operators that were included in the original JavaScript which are now the basis for a statically-typed language with machine types for high-speed performance. β€œIf it hadn’t been in there from 1995, it would’ve been hard to add later. And the fact that it was there all along meant we could do incredibly fast JavaScript.”

He tells InfoWorld it’s β€œthis very potent seed that was in the original JavaScript from the 10 days of May in 1995.” JavaScript’s 32-bit math operators (known as bitwise operators) trace their lineage all the way back to the C programming language β€” and to Java. This eventually led to WebAssembly β€” a way to convert instructions into a quickly-executable binary format for virtual machines β€” and the realization that with a JavaScript engine, β€œyou can have two languages β€” the old language I did with the curly braces and the functions and the shift operators, and this new language which is a binary language, not meant for reading by humans or writing. But it can be generated by compilers and tools, and can be read by tools…”

[–] Kacarott@aussie.zone 20 points 2 days ago (6 children)
load more comments (6 replies)
[–] bradbeattie@lemmy.ca 19 points 2 days ago
[–] wreleven@lemmy.ca 13 points 2 days ago

Feels like it could be one of those facebook posts to test "smart" people. Only the top 1% of people can answer this simple math question: "11" + 2 * 2 - 3

[–] arc@lemm.ee 13 points 1 day ago* (last edited 1 day ago) (1 children)

Javascript is a dogshit language that everyone is stuck with. The best that we can hope for is the likes of typescript take the edge off of it. Even though it's like smearing marzipan over a turd. At least it's ok if you don't take a deep bite.

load more comments (1 replies)
[–] proctor1432@lemmy.world 12 points 2 days ago

Heck, I need to learn some new languages apparently. Here I was expecting an angry "CS0029 cannot implicitly convert type 'string' to 'int'!

[–] bss03@infosec.pub 11 points 2 days ago* (last edited 2 days ago) (6 children)

This is my favorite language: GHC Haskell

GHC Haskell:

GHCi> length (2, "foo")
1
load more comments (6 replies)
[–] yogsototh@programming.dev 10 points 2 days ago
load more comments
view more: next β€Ί