this post was submitted on 05 Aug 2025
121 points (98.4% liked)

Programmer Humor

37622 readers
24 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 6 years ago
MODERATORS
 

That's it. That's the meme.

top 44 comments
sorted by: hot top controversial new old
[–] kureta@lemmy.ml 2 points 1 day ago

why convert boolean to integer instead of converting the other operand to boolean? it doesn't make sense.

[–] Pieisawesome@lemmy.dbzer0.com 19 points 2 days ago (1 children)

So JavaScript type coercion is odd, but if you understand it and the WHY, it isn’t that bad.

Complain about this instead: https://jsdate.wtf/

[–] itslilith@lemmy.blahaj.zone 4 points 1 day ago

God, this is cursed

[–] jedibob5@lemmy.world 13 points 2 days ago

Javascript's type coercion is rather insane, yes, but there is an actual, practical reason it's done. JS, having been designed to be run in web browsers, wants to avoid blowing up and crashing at all costs. If it gets an unusual type comparison, usually the result of a bug, it tries to return something, such that the script can continue running if at all possible. In JS' mentality, keeping a page running, even if it might not completely function properly, is preferable to throwing an unhandled exception and completely crashing it.

Whether or not that is the right approach is debatable, but there is at least some logic to it. Personally, I think that the proliferation of Node letting JS run outside of browsers exacerbates a lot of JS' issues, but TypeScript does a lot to make it look like a more sensible language.

[–] yetAnotherUser@lemmy.ca 5 points 1 day ago (1 children)

Hey OP, do you mind checking if your book explains the type coercions that are used with the + operator? I remember it also being mind-boggling, so I was hoping you book could demystify it too.

[–] sleeplessone@lemmy.ml 3 points 1 day ago

I don't recall if it covers that sadly. I read it months ago and this part stood out to me.

[–] hex@programming.dev 4 points 1 day ago

I mean, it is the basic concept of truthiness. At least they let you use strict equal.

[–] darklamer@lemmy.dbzer0.com 22 points 2 days ago

So this is what insanity looks like.

[–] Mr_Fish@lemmy.world 18 points 2 days ago (2 children)
[–] Telemachus93@slrpnk.net 14 points 2 days ago

Mh, '0' is a nonempty string, so !'0' returns false. Then of course !(!'0') would return true. I'd absolutely expect this, Python does the same.

And the second thing is just JavaScript's type coercion shenanigans. In Python

bool('0') # returns True because of nonempty string
bool(int('0')) # returns False because 0 == False

Knowing that JavaScript does a lot of implicit type conversions, stuff like that doesn't strike me as very surprising.

[–] lime@feddit.nu 3 points 2 days ago

wait hang on...

[–] TropicalDingdong@lemmy.world 12 points 2 days ago
[–] PostaL@lemmy.world 2 points 1 day ago (1 children)

Is that a tear on the "again"?

[–] markstos@lemmy.world 1 points 1 day ago

Or sweat. Either is appropriate there.

[–] markstos@lemmy.world 1 points 1 day ago

Hello from Perl! Looks reasonable to me!

[–] sorter_plainview@lemmy.today 7 points 2 days ago (1 children)

This is Haverbeke's book, right? Eloquent JavaScript?

[–] sleeplessone@lemmy.ml 7 points 2 days ago (1 children)

It's from Javascript: The Definitive Guide 7th Edition by David Flanagan. It's the O'Reilly book with the rhinoceros on it.

[–] Midnitte@beehaw.org 6 points 2 days ago

It's the O'Reilly book with the rhinoceros on it.

Honestly O'Reilly should just remove the titles and leave just the animals to describe the books.

[–] semperverus@lemmy.world 6 points 2 days ago

This makes it make so much more sense...

Its not really insanity, just a lot of hidden function calls

[–] Natanox@discuss.tchncs.de 4 points 2 days ago

The description in the first photo about int - steing comparison is incomplete though, right? Wasn't there also a rule anout which one of then comes first (the second parameter gets converted?), and what happens if a string contains non-numeric values?

It's all so confusing…

[–] retrolasered@feddit.uk 3 points 2 days ago (3 children)

But then why 2 == true if true is converted to 1

[–] Midnitte@beehaw.org 7 points 2 days ago* (last edited 2 days ago)

3 - 1 // -> 2

3 + 1 // -> 4

'3' - 1 // -> 2

'3' + 1 // -> '31'

[–] Telemachus93@slrpnk.net 8 points 2 days ago (1 children)

It's not. Just tried in my Browser Console:

2 == true // returns false
[–] retrolasered@feddit.uk 6 points 2 days ago (2 children)

I genuinely wasn't aware of that. I must be getting javascript confused for almost any other language. I wonder how many times ive !!'d a value to make that work without actually absorbing that into my head now..

[–] Excel@beehaw.org 3 points 2 days ago

2 is not == true, but !!2 is true

[–] Telemachus93@slrpnk.net 3 points 2 days ago (1 children)

In other languages that shouldn't be equal either though, right?

Maybe you meant

if (2){
console.log("nonzero ints are truthy")
}
else {
console.log("no they're not")
}

Which would output

nonzero ints are truthy

and that would actually work in all languages I know. But that's different from being equal.

[–] retrolasered@feddit.uk 2 points 1 day ago

Yeah its checking for not null with if isnt it. Maybe thats what has me confused

[–] NewDark@hexbear.net 5 points 2 days ago

2 is 'truthy', or rather, not 'falsy'.

I'm not even kidding.

[–] tetris11@lemmy.ml -5 points 2 days ago* (last edited 2 days ago) (4 children)

Yes, we should all use rigid types. Name me one language you actually like writing quickly with that has types?

Pyth-oh. Bash-oh. Lisp-oh. Perl-oh. Oh yeah... typed languages suck because of all the boiler

Edit: Fine, Python / Lisp / Perl are all technically "typed" languages, but I ask you what's point of throwing type errors at runtime. Javascript and Rust actually have it right here that the code is either going to run, or it simply isn't. No pussyfooting letting it run first to throw complaints

[–] neshura@bookwyr.me 13 points 2 days ago

Python, uh, has types tho? Python just doesn't engage in the same brainless type coercion that JS does, instead type coercion is much more limited and sensible.

[–] barubary@infosec.exchange 9 points 2 days ago (1 children)
[–] tetris11@lemmy.ml 1 points 2 days ago

Never got into Haskell, but I was taught Miranda at school and thought it was pretty cool

[–] cornshark@lemmy.world 3 points 2 days ago

Kotlin is pretty good

[–] alsimoneau@lemmy.ca 5 points 2 days ago (2 children)

Python. Don't know why you excluded it.

[–] tetris11@lemmy.ml -2 points 2 days ago* (last edited 2 days ago) (1 children)
[–] alsimoneau@lemmy.ca 7 points 2 days ago

I don't see the image and the last paragraph sais nothing about types.

But Python is a strongly typed language. It's right there in the info box.

[–] barubary@infosec.exchange -3 points 2 days ago (1 children)
[–] alsimoneau@lemmy.ca 4 points 2 days ago (1 children)

It's not. It's a strongly typed langage.

[–] barubary@infosec.exchange -3 points 2 days ago (1 children)

"Strongly typed" is meaningless. (Or nearly so; in practice it means "I like this language" and "weakly typed" means "I dislike this language".) The point is that Python has no type system.

[–] alsimoneau@lemmy.ca 4 points 2 days ago (1 children)

You're spewing nonsense.

There absolutely is a type system in Python. The fact that you have dynamic types doesn't preclude having also strong types and certainly doesn't mean you don't have types at all. Try to do 2+"a" in Python and you'll get a TypeError. The thing with Python is that values have a type, not variables. Because all variables are essentially pointers.

[–] barubary@infosec.exchange -1 points 2 days ago (1 children)

Again, "strong types" doesn't mean anything.

But from a type theory perspective, having "dynamic types" absolutely means you don't have a type system. All Python has is runtime exceptions. The fact that one of them is named TypeError doesn't make it a type error in the formal sense.

The point of a type system is not that variables have types, but that types are assigned to expressions (i.e. pieces of code in your source file), not to values (i.e. pieces of data). This is important because it guarantees that certain errors cannot occur in a program that passes the type checker (assuming you have a sensible/useful type system). And you get this assurance without running a single line of code.

To get a similar guarantee from Python, you need to write exhaustive tests, just as with any other runtime error.

[–] alsimoneau@lemmy.ca 1 points 1 day ago (1 children)

That's a very narrow definition of limited usefulness, and in practice it means your code is overly verbose and inflexible. You get stuck with polymorphism everywhere or you're explicitly converting data all the time for nothing.

Plus, if you try to process some data from an external source (which you have to if you want to do anything useful) you don't have any way to test if it's the right type before execution, so you're back at the same place that Python is, without the ability to cleanly recover.

[–] barubary@infosec.exchange 0 points 1 day ago (1 children)

I believe now it is my turn to say you're spewing nonsense. Have you ever used a language with a type system? Because this whole idea of "testing external data to see if it is the right type" doesn't really make sense.

At the lowest layer, generally, external data is an array of bytes. It has no other type you could "test". However, you can decode or parse it into a form that makes sense for your program. And why couldn't I cleanly recover from parse errors? (And how do you think Python does it?)

[–] alsimoneau@lemmy.ca 1 points 1 day ago

Clearly you've never programmed anything outside of theoretical computer science or heavily enclosed and controlled systems.