this post was submitted on 05 Oct 2023
1097 points (96.8% liked)

Programmer Humor

37300 readers
68 users here now

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

Rules:

founded 6 years ago
MODERATORS
 
top 50 comments
sorted by: hot top controversial new old
[–] kibiz0r@midwest.social 111 points 2 years ago (3 children)

One of the most frustrating things about null is that it has so many possible meanings:

  • We don’t plan to provide a value here, so use a default instead
  • We plan to provide a value, but memory for this value hasn’t been allocated yet
  • The memory has been allocated, but we haven’t attempted to compute/retrieve the proper value yet
  • We are in the process of computing/retrieving the value
  • There was a code-level problem computing/retrieving the value
  • We successfully got the value, and the value is “the abstract concept of nothingness”
  • or the value is “please use the default”
  • or the value is “please try again”

And so on. “Null” probably has more different meanings based on context than the word “fuck”.

[–] Gork@lemm.ee 52 points 2 years ago (3 children)

Ooh and the edge cases. Like if your last name is Null.

[–] Omega_Haxors@lemmy.ml 9 points 2 years ago

Bobby Tables would be proud.

[–] gornius@lemmy.world 5 points 2 years ago* (last edited 2 years ago) (2 children)

The way I use it is 'undefined' is literally undefined (not set), but null means no value - explicitly.

[–] FiniteLooper@lemm.ee 5 points 2 years ago

I used to ban null usages with ESLint rules for this exact reason. If it’s there use a value, if not use undefined

load more comments (1 replies)
[–] MonkderZweite@feddit.ch 2 points 2 years ago

It's a non-zero string tho?

[–] Badland9085@lemm.ee 21 points 2 years ago (2 children)

Many of these meanings seem to be captured in some modern solutions already:

  • We plan to provide a value, but memory for this value hasn’t been allocated yet.
  • The memory has been allocated, but we haven’t attempted to compute/retrieve the proper value yet
  • We are in the process of computing/retrieving the value

Futures?

  • There was a code-level problem computing/retrieving the value

Exception? Result monads? (Okay, yea, we try to avoid the m word, but bear with me there)

  • We successfully got the value, and the value is “the abstract concept of nothingness”

An Option or Maybe monad?

  • or the value is “please use the default”
  • or the value is “please try again”

An enumeration of return types would seem to solve this problem. I can picture doing this in Rust.

[–] david@feddit.uk 7 points 2 years ago (2 children)

Don't call it a monad, call it a structured data type or something, that's what it is! Calling it a monad is like saying that you're using a curve of constant normal intersection point. Why not just say it's a wheel?

Yes, it's mathematically true that you're having a smooth ride precisely because the normals have a constant intersection point, but it's also true to say that it's a wheel and it goes round and isn't bumpy and doesn't scrape, and people can get a handle on that.

So yeah, use a Result or Option or Maybe structured data type because it keeps explicit track of whether there's a value or not, and yeah, you can change or combine them and preserve the tracking, but there's no point calling it a monad unless you're trying to make people believe that avoiding the $1bn mistake of allowing/using null requires category theory. It doesn't, it's just a structured data type. It's simpler than an array! Stop calling it a monad.

[–] kogasa@programming.dev 5 points 2 years ago
[–] Spzi@lemm.ee 3 points 2 years ago (1 children)

"Monad" is a shorter term though. "Structured data type" reads almost as bulky as "Curve of constant normal intersection points".

[–] david@feddit.uk 3 points 2 years ago

True. But the word Monad has done more harm to the accessibility, popularity and reputation of pure functional programming than pretty much anything else.

Yeah, I could have said circle rather than curve of constant normal intersection points, but that word is very commonly understood, so it's not that same as unnecessarily calling something a Monad. Maybe it's the equivalent of calling it a 2-manifold instead of a wheel.

Perhaps just ditch the generalisation, then, and just call them Result or Maybe. After all, circle is a short word, but we just call them wheels.

[–] Camilo@discuss.tchncs.de 6 points 2 years ago* (last edited 2 years ago)

Yeah, for this reason null shouldn't be part of any production code. If there's the possibility of having a null value, you need to check every variable or returned value to be safe.

These monads tell the consumer of your functions to do something (a check for emptiness or wait for it to be ready, or iterate it) to access the value inside. In a safe language, if the value is not wrapped by a monad, then you should expect to access it without issues.

I kind of get why people don't want to call them monads, since it sounds like a heavy term and more things to learn that are not strictly "necessary", but the earlier you learn about their importance, the earlier you can use any of their benefits in your codebase.

[–] joyjoy@lemm.ee 8 points 2 years ago (1 children)

Null was added to JavaScript because Java had it. Null is unnatural. Undefined is the canon "no value" value.

[–] peopleproblems@lemmy.world 2 points 2 years ago

Null isn't unnatural, just isn't there.

[–] vettnerk@lemmy.ml 49 points 2 years ago (1 children)
[–] xigoi@lemmy.sdf.org 8 points 2 years ago* (last edited 2 years ago) (2 children)

But typeof(fire_extinguisher) === "toilet_paper"

[–] mkwarman@lemmy.mkwarman.com 11 points 2 years ago (1 children)
[–] Carighan@lemmy.world 4 points 2 years ago

Still to date one of more sensible JavaScript demos.

[–] stebo02@sopuli.xyz 2 points 2 years ago

maybe it's rather a box of tissues

[–] morgunkorn@discuss.tchncs.de 24 points 2 years ago (3 children)

I need a different picture for negative values

[–] master5o1@lemmy.nz 37 points 2 years ago (1 children)

Flip the toilet roll around.

[–] MatFi@lemmy.thias.xyz 3 points 2 years ago* (last edited 2 years ago)

Full scale analytics of this issue on Wikipedia

Or

Wikipedia

[–] ininewcrow@lemmy.ca 3 points 2 years ago

You're going to need a wet suit, an oxygen tank and scuba gear for that ..... and some air freshener or a deodorizer.

You know exactly what that picture is, and it's how I prefer to hang my toilet paper rolls because I'm in the small minority that finds it more aesthetically pleasing.

[–] ICastFist@programming.dev 13 points 2 years ago (3 children)

Javascript, regarding zero, null and undefined = They're all the same thing

[–] Skyhighatrist@lemmy.ca 14 points 2 years ago (1 children)

That's not true these days. You can try it yourself right in your browser's dev console.

These results are from Firefox's console.

0 == null == undefined
> false
0 == null
> false
0 == undefined
> false
null == undefined
> true
null === undefined
> false

And even in the one case where == says they are the same, you can fix that by making sure you are using === so that it doesn't do type coercion for the comparison.

[–] shrugal@lemm.ee 2 points 2 years ago* (last edited 2 years ago) (6 children)

Shhhhh, bashing Javascript is cool around here.

load more comments (6 replies)
[–] ryan@the.coolest.zone 8 points 2 years ago (1 children)

Oh man, you've got me itching to get into the intricacies of JavaScript...

One fun example of the difference: when doing arithmetic operations, null is indeed converted to 0, but undefined is converted to NaN. This has to do with null being an assigned value that represents empty, whereas undefined is not actually a value but a response indicating that there was no value assigned in the first place.

[–] dan@upvote.au 7 points 2 years ago* (last edited 2 years ago) (1 children)

response indicating that there was no value assigned in the first place.

You can explicitly assign undefined to a variable though.

Another fun fact about JavaScript is that undefined never used to be a keyword. If you did var foo = undefined, foo would indeed have a value of undefined, but it was only because there was no variable called undefined in scope!

You could do var undefined = 42 then var foo = undefined would actually set foo to 42! window.undefined = 42 would break all sorts of things.

Thankfully this was fixed with ES5 in 2009, although it took a few years for browsers to make the change.

[–] darcy@sh.itjust.works 4 points 2 years ago

javascript moment

load more comments (1 replies)
[–] TootSweet@lemmy.world 13 points 2 years ago (3 children)

Oh oh oh now do infinity, -infinity, -0, and NaN.

[–] dandroid@dandroid.app 21 points 2 years ago (3 children)

NaN would be something that isn't toilet paper. So I'm gonna say a picture of a whole rotisserie chicken on the toilet paper bar.

load more comments (3 replies)
[–] CanadaPlus@lemmy.sdf.org 3 points 2 years ago* (last edited 2 years ago)

Infinities: The toilet paper just keeps piling up until it's out of frame. It probably ends somewhere, but nowhere we can measure.

Alternately, the toilet paper it tightly wedged in the little hole and no more could be accommodated.

Subnormals: There's less than one square left, but there is still toilet paper.

-0: The glue marks on the empty tube are pointing the other way. This shouldn't matter, but is apparently enough to confuse some toilet users.

load more comments (1 replies)
[–] Dkarma@lemmy.world 12 points 2 years ago (1 children)

Who the fuck puts captions ABOVE a picture?????

Get the tar...

[–] Lewistrick@feddit.nl 8 points 2 years ago* (last edited 2 years ago) (1 children)

The worst thing is that the text and images are misaligned.

[–] Moc@lemmy.world 7 points 2 years ago (1 children)

Ease up mate CSS isn’t easy

load more comments (1 replies)
[–] lanaa@lemmy.ml 10 points 2 years ago (2 children)

Isn't the undefined one incorrect? From what I know undefined means it can contain whatever value. So it could be no toilet paper, full toilet paper or anything in-between.

[–] stebo02@sopuli.xyz 22 points 2 years ago* (last edited 2 years ago) (1 children)

not undefined in mathematics, an undefined variable is just a variable that hasn't been defined (yet) and is therefore nonexistent

[–] ActuallyRuben@actuallyruben.nl 3 points 2 years ago

Depends on what undefinedwe're talking about. JavaScript undefined is just a value for undefined variables. In C undefined behavior could be anything, ranging from reading in random garbage to time travel or summoning eldritch terrors.

[–] JackbyDev@programming.dev 4 points 2 years ago

This is JavaScript undefined specifically where it refers to a key that isn't present.

[–] BigBananaDealer@lemm.ee 6 points 2 years ago (2 children)

i thought zero and null were the same thing. very informative

[–] Gentoo1337@sh.itjust.works 3 points 2 years ago (2 children)

Depends on the language afaik.

[–] Ddhuud@lemmy.world 3 points 2 years ago

The only language I know where null equals 0 is German.

load more comments (1 replies)
load more comments (1 replies)
[–] SubArcticTundra@lemmy.ml 4 points 2 years ago (2 children)

You need to make one for void*

load more comments
view more: next ›