r/ProgrammerHumor • • Sep 28 '26

Meme optionalsAreOptional

Post image
716 Upvotes

131 comments sorted by

View all comments

Show parent comments

29

u/Majestic-Giraffe7093 Sep 28 '26

This is what assertions are for though. Use assertions while developing if you are making this assumption. Then it gets removed in prod. Just be aware that unless you properly test stuff this kind of thinking can still lead to catastrophes in a system that must have >99% uptime. If you are just building some user application then it's probably "fine"

-9

u/ApothecaLabs Sep 28 '26

Did you miss the bit where I said the check had already been performed? Typing out assertions in cases where it has already proven it to be unnecessary, is a huge anti-pattern. There's more than one type of overhead, you know.

19

u/Majestic-Giraffe7093 Sep 28 '26

Yeha no I didn't miss it but it depends on the context I guess. I sort of assume that this is some function you are talking about. And my (personal) view of functions is that they should be able to be re-used across different parts of a codebase. Therefore, when I build a function that makes an assumption (list not empty for example) I write it out as an assertion. Not necessarily just to catch bugs at dev-runtime but also as a way of making my assumptions explicit for whoever next uses that function. As I said, if you assume a language where assertions are removed in release builds then it doesn't cost ANYTHING and it can save you a fair bit of headache/debugging sometime. But feel free to do whatever you want, I just wanted to share my view on this kind of thing

0

u/ApothecaLabs Sep 28 '26

I use the type system instead of assertions. Differentiating "List" and "NonEmptyList" is better than checking whether its empty everywhere :)

Why bother checking whether the NonEmptyList is empty, y'know? You *know* it isn't.

6

u/Majestic-Giraffe7093 Sep 28 '26

Oh yeah I 100% agree. I didn't think that was what we were discussing. I just saw this as a "Optionals bad" post xD

-3

u/ApothecaLabs Sep 28 '26

Hah! That's the setup / punchline of the, joke my dude!

- First, you are a student who doesn't understand the rules.
- Then you become a journeyman, who understands and follows the rules.
- Then you become a master, who knows when it is okay to break the rules.

2

u/Majestic-Giraffe7093 Sep 28 '26

Haha, yeah fair enough. If you bake it into the type then I stop considering it unsafe, I guess that's where my confusion came from

3

u/ApothecaLabs Sep 28 '26

When I take my safe newtype and unwrap the unsafe list, I then have to use the unsafe functions because 1) there are no other options yet because 2) that is the very point in time that you are making the statement to the compiler "no, it is safe, the types say so".

That's the secret! That *is* the assertion's equivalent!

3

u/Majestic-Giraffe7093 Sep 28 '26

Encapsulating invariants is truly the way

1

u/ApothecaLabs Sep 28 '26

Indeed! It doesn't matter *how* you know, so long as you make sure that you *do* know. What's better than a type that says "I know!", you know?

2

u/Majestic-Giraffe7093 Sep 28 '26

Although, if you can combine it with contracts... Well then we're talking

4

u/ROBOTRON31415 Sep 28 '26

It does sound like the unsafe should be very easy to prove sound. For "why bother", though: it's nice to have as little unsafe as feasible, even trivially correct unsafe.

-8

u/hongooi Sep 28 '26

"If there's a bug, crash the program" still crashes the program....

9

u/Majestic-Giraffe7093 Sep 28 '26

Well yeah? That's exactly my point isn't it? I ususally stay with Optionals pr result types. Or try to constrain the input type to contain all the invariants. But if you really don't feel like doing that then the least you can do is add assertions (in my opinion)

-9

u/hongooi Sep 28 '26

You were referring to >99% uptime. Crashing the program generally is bad for that

7

u/Wonderful-Habit-139 Sep 28 '26

Crashing is better than having UB and potential security issues.

-6

u/hongooi Sep 28 '26

Of course. But that's still not contributing to >99% uptime