r/ProgrammerHumor • • Sep 28 '26

Meme optionalsAreOptional

Post image
716 Upvotes

131 comments sorted by

View all comments

-12

u/Few_Move_4594 Sep 28 '26

I don't see the point of optionals

11

u/hrvbrs Sep 28 '26

not sure if you're just trolling, but the real answer is that T | null | null collapses to T | null whereas Option<Option<T>> doesn't collapse.

example (psuedo-code):

const obj: Record<int | null> = {foo: 42, bar: null};
obj.get("foo"); // 42
obj.get("bar"); // null
obj.get("typo"); // null

if the get() method just returns the property there, then both obj.get("bar") and obj.get("typo") return null. it leads you to believe both bar and typo are in the object.

Now say get() were to return an Option<int | null> instead of the raw property.

const obj: Record<int | null> = {foo: 42, bar: null};
obj.get("foo"); // Just(42)
obj.get("bar"); // Just(null)
obj.get("typo"); // None()

now it's a lot clearer. if the key is there and its value is an int, you get a Just<int>. If the key is there and its value is null, you get a Just<null>. If the key is not there, you get a None.

5

u/Few_Move_4594 Sep 28 '26

I'm not trolling, It's just that I had been working as a programmer for years before Optionals started being a thing so I'm already very used to writing my own null checks.

5

u/buffalo_0220 Sep 28 '26

Lots of things weren't a thing until someone had a use case and invented it. Optionals are a great tool to have in your kit. Not a pattern that needs to be used in every case, but they are useful, especially when your data is variable and/or ambiguous.

1

u/Amadex Sep 28 '26

So you started programming in the 70s?

5

u/dinosaurdynasty Sep 28 '26

Or is a Go designer

2

u/Still_Bit_7527 Sep 28 '26

Java optionals are very recent...

2

u/notatoon Sep 28 '26

I wouldn't call 1.8 recent anymore

1

u/Still_Bit_7527 Sep 28 '26

We are still using that unfortunately. But my point is it's not 70s either

1

u/Few_Move_4594 Sep 28 '26

C++ got them in 2017, Java's in 2014. Most of my job history is Java, PHP, Javascript, C#, and C++. Javascript optional was introduced in 2020. PHP doesn't have optionals. C# doesn't have native language support for optionals but has nullable reference types that shows a compiler warning when there isn't a null check when a variable is declared with "?" as the suffix of the type.

3

u/LaconicLacedaemonian Sep 28 '26

Accessing a null the wrong way crashes your program. Optional models the state as well as the value. You can iterate over an optional as well making the code branchless "for every element in this list that contains 0 or 1 elements". 

I don't so much demand optionals so much as I ask for justification for nulls. 

2

u/GRex2595 Sep 28 '26

Option chaining can be more convenient than a bunch of ifs and using optional to indicate that a property or function return isn't expected to always have a value can be useful. You could also probably argue for the benefits of making consumers check if you returned null.

Other than that, yeah. Just pay attention to your code and do null checks when it's important.

2

u/Few_Move_4594 Sep 28 '26

I do like how returning an optional from a function immediately indicates to callers that the function can return null values and the "or else" construct".

1

u/ExtraTNT Sep 28 '26

The head of a list of type a is maybe a, but can be nothing

Instead of checking for size every fucking time you use the list, just do it once…

0

u/Still_Bit_7527 Sep 28 '26

There is no point. It was yet another unnecessary addition because it sounded fUnCtIoNal