1117
you are viewing a single comment's thread
view the rest of the comments
[-] kibiz0r@midwest.social 112 points 1 year ago

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 53 points 1 year ago
[-] Omega_Haxors@lemmy.ml 9 points 1 year ago

Bobby Tables would be proud.

[-] gornius@lemmy.world 5 points 1 year ago* (last edited 1 year ago)

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

[-] FiniteLooper@lemm.ee 5 points 1 year 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

[-] chaorace@lemmy.sdf.org 1 points 1 year ago

Except you can define a value with undefined and accessing that value will have different behavior than attempting to access an undefined value.

[-] MonkderZweite@feddit.ch 2 points 1 year ago

It's a non-zero string tho?

[-] Badland9085@lemm.ee 21 points 1 year ago

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 1 year ago

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.

[-] Spzi@lemm.ee 3 points 1 year ago

"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 11 months 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 1 year ago* (last edited 1 year 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 9 points 1 year ago

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

[-] peopleproblems@lemmy.world 2 points 1 year ago* (last edited 1 year ago)

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

this post was submitted on 05 Oct 2023
1117 points (97.0% liked)

Programmer Humor

32155 readers
212 users here now

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

Rules:

founded 5 years ago
MODERATORS