• Rao (unregistered)

    Seems like a modern take on https://thedailywtf.com/articles/What_Is_Truth_0x3f_

  • (nodebb)

    Pre-1999 versions of C? C23 is the first version of C where true and false were their own thing. Before then they are just macros defined in stdbool.h!

  • (nodebb) in reply to NimbusTheLion

    OK, but versions starting at C99 but still before C23 do at least have a way to create a bool type that coerces values to exactly true or exactly false without the programmer having to define it.

  • Phelps (unregistered)

    These really need a comment above them in the code that says “I know this is stupid but it is the only thing that fixes [bug scenario].” Then we get to cry with the original coder rather than laugh at him.

  • (nodebb) in reply to NimbusTheLion

    Considering that C/C++ is not 100% type safe with it's implicit cast operations of standard types that can result in data loss, TRUE/FALSE were perfectly fine and well defined long before C99 (FALSE is zero and TRUE is everything but FALSE).

  • (nodebb)

    And the billion dollar mistake is still unsolved because Optional can also (a) be null (it's a class like everything in Java) and (b) contain null (because Boolean is also a class)!

  • Jonathan (unregistered)

    I know in .NET's CLR, instantiating the same object repeatedly adds churn to the GC and static instances of commonly used types (e.g. serialization configuration options object) can improve performance (although maybe not meaningfully below a certain scale though).

    I'm guessing this is Java, would it benefit the same way?

  • RLB (unregistered)

    Optional solves null the same way a sign saying "Are you wearing your hard hat?" solves builders not wearing their hard hats. The same people who don't check for null don't check that the optional has a value.

  • (nodebb) in reply to NimbusTheLion

    #define TRUE 0 #define FALSE !TRUE

    stdbool.h was not in K & R 'C'

  • (nodebb) in reply to colejohnson66

    Optionals cannot "contain null", conceptually. They are either empty or contain a non-null value. And if you use an Optional, you expect that under certain conditions it can be empty, otherwise you wouldn't have used an Optional.

  • AzureDiamond (unregistered)

    this actually kinda makes sense its just implemented the wrong way. if youre dealing with incomplete data Optional.empty means unknown and Optional.of(Boolean) is a true/false value. the problem is making these into a constant and naming them TRUE and FALSE

  • Argle (unregistered) in reply to Rick

    I've already revealed my age, so I guess it's ok to mention that the first C class I taught used the K&R white-book.

  • (nodebb) in reply to Rick

    #define TRUE 0 #define FALSE !TRUE

    This is a joke, right?

  • (nodebb) in reply to Maia-Everett

    Optionals cannot "contain null", conceptually. They are either empty or contain a non-null value. And if you use an Optional, you expect that under certain conditions it can be empty, otherwise you wouldn't have used an Optional.

    That sounds ... wrong, or at least slightly off the mark.

    I don't do Java, nor C#, but an Optional<T> (where T is a nullable type) that doesn't allow me to store a null value in it is broken. In essence, such an optional has three possible states: empty (no value stored at all), NULL stored, and something-that's-not-NULL stored. If Java (or C#) doesn't work like that, I'd be happy for someone to fill the gaps in my knowledge.

  • Anonymous (unregistered)

    This may make sense when parsing JSON with Jackson, if you need to differentiate between a field not being present, or being explicitly set to null:

    Parsing { "foo": null } will give foo = Optional.empty(), while an empty object {} will result in foo = null.

  • nope (unregistered)

    Optional does not, at all, fix the "problem" of null. because, regardless of whether you use NULL or Optional, it boils down to: if you use the value you're actually interested in without checking if said value actually exists, you're gonna have a bad time.

  • TS (unregistered)

    "Optional types are an attempt to patch the "billion dollar mistake"." Nope. The billion dollar mistake was not inventing nulls, it was making it so very easy for objects to be null when they shouldn't be. Most objects, once properly constructed and ready to use, should never be null in the same way that integers shouldn't be. But language design misfeatures mean that that's really hard to enforce and it's really easy to introduce bugs.

    Optional/nullable types, like database nulls, were invented to solve an entirely different problem, namely that sometimes it's useful to have an easy way to handle the case that sometimes the value of a variable is genuinely "unknown".

  • (nodebb)

    The billion dollar mistake was assuming that programmers would check return codes from functions.

  • richarson (unregistered)

    It seems to me that there's no single "billion dollar mistake" but a billion "dollar mistakes" spread all over...

  • (nodebb) in reply to Steve_The_Cynic

    The statement in the original post was about making constants for true and false. C99 explicitly makes constants for true and false …

  • Java guy (unregistered) in reply to Steve_The_Cynic

    I think you're just misunderstanding what "empty" means here. An empty Optional is just holding a null value. Being inside an Optional means you can't get a null back out of it by accident; Optional.<Object>ofNullable(null).get() throws an exception rather than giving you a null.

  • Nick (unregistered)

    In Java and Scala, at least, Optional values can indeed contain null. It’s rather the bane of my existence at times.

    Conceptually, we can have None, Some(value) or Some(null). This comes up most often when modelling input from an external system (config, user input, API input etc.) where a value may or may not be supplied, and if supplied, may be expressly “unset”.

    We then have None meaning “user did not supply a value”, Some(x) meaning “user supplied value x” or Some(null) meaning “user expressly supplied an empty value”.

    There are definitely other ways to handle this - one being Optional<Optional<T>>, and another to use specific sentinel values for “empty” - but I’m not convinced that there are actuallly better ways to handle it!

  • (nodebb) in reply to nope

    Yes, but since you have to explicitly extract the value from inside the Optional (which already puts you on notice that the value may not be there or it wouldn't be wrapped in an Optional), that's the point where not checking there's a value there to start with will bite. Not at some arbitrary point in the program where that object of type T you've been passing around turns out to have actually been a null of type T all along.

    And that's the "billion-dollar mistake". Not the existence of nulls, but null references: that it's possible to have an object reference that doesn't reference an object; the fact that having an object of type T does not guarantee that you have an object of type T.

  • airdrik (unregistered) in reply to Nick

    In Java and Scala, at least, Optional values can indeed contain null.

    By that, you mean "In Scala, and java when using Scala's Optional type", because Java 8 introduced it's own Optional type, which permits Optional.empty() or Optional.of(value), but doesn't permit of(null). (and has Optional.ofNullable(valueMaybeNull) which returns Optional.empty() if provided null)

    Both have their usefulness, depending on if you need an explicit indication of there being a (non-null) value, or if you need to distinguish between no value was supplied vs. the value supplied was null/empty.

  • cros (unregistered) in reply to AzureDiamond

    They could have declared a third constant named EMPTY.

  • kythyria (unregistered) in reply to TS

    I suspect it's partly due to requiring a somewhat more complicated compiler to avoid all those Optionals taking up extra space, on top of language features to make sure you do the check.

  • (nodebb) in reply to RLB

    Optional solves null the same way a sign saying "Are you wearing your hard hat?" solves builders not wearing their hard hats. The same people who don't check for null don't check that the optional has a value.

    No they don't. At least, they don't in languages that were designed with optionals from the start. Optional<T> is a different type to T which means that the compiler will refuse to compile programs where you haven't explicitly unwrapped the value of type T. This contrasts with the null reference approach because a null reference - in Java - is the same type as a non null reference. The compiler cannot enforce checking.

  • (nodebb) in reply to Steve_The_Cynic

    Even back in the 80s, I couldn't remember whether 0 was true or false. (Probably because UNIX return codes of 0 were "good", but in C 0 was false.)

    For my own sanity, I always used:

    #define TRUE (0==0)
    #define FALSE (0==1)
    

    Addendum 2025-09-02 11:32: I'm sure I would have added a space after TRUE to align the operators. It took years of using source-code-control to break that habit.

  • nxtangl (unregistered) in reply to Rao

    const FILE_NOT_FOUND = None

Leave a comment on “Springs are Optional”

Log In or post as a guest

Replying to comment #683727:

« Return to Article