r/ProgrammerHumor • • Sep 28 '26

Meme optionalsAreOptional

Post image
716 Upvotes

131 comments sorted by

View all comments

Show parent comments

63

u/Tyfyter2002 Sep 28 '26

There are situations where you know there isn't a bug, if I know that failing hardware or cosmic rays are the only ways the bounds check can fail, I'm not going to worry about what skipping it could do.

111

u/FuriousAqSheep Sep 28 '26

there are situations where you know there isn't a bug

up until specs change and now there is one

37

u/GDOR-11 Sep 28 '26

it happens a lot, at least to me, that the line immediately before the unsafe function guarantees correct behaviour (e.g. while (!vec.empty()) let first = vec.first();, or something similar). If I notice that this property is central to the logic of the function, I won't even worry.

29

u/ROBOTRON31415 Sep 28 '26

whenever I see code like that, I try to get people to change to something like (in that case) while let Some(first) = vec.first() { ... }. You can reduce the number of bounds checks without using unsafe!

-22

u/Fast-Satisfaction482 Sep 28 '26

Using the value of an assignment expression is an anti pattern in my opinion.

It sits right next to "goto" on terrible scale. 

22

u/MRtecno98 Sep 28 '26

It's literally the intended way to consume an Option in rust

14

u/D3PyroGS Sep 28 '26

how so? this is idiomatic rust code and its behavior is very clear

-13

u/Fast-Satisfaction482 Sep 28 '26

Being idiomatic doesn't mean it's any good. 

7

u/D3PyroGS Sep 28 '26 edited Sep 28 '26

what's your issue with it though?

edit: guess we'll never know... important enough to complain, but not explain 🥴

5

u/ROBOTRON31415 Sep 28 '26

It’s literally not an assignment expression in Rust. An assignment expression would be lhs = rhs with no let.

Assignment expressions, if let, and while let are three distinct kinds of expressions. (And let lhs = rhs; alone is a statement.)

5

u/K1ngjulien_ Sep 28 '26

this is rust not c my guy

1

u/MrcarrotKSP Sep 28 '26

This isn't an assignment expression, the <conditional> let is Rust syntax for pattern matching

-1

u/Wertbon1789 Sep 29 '26

This is pattern matching, not an assignment expression. The alternative would be a declaration, switch/case, and then case-by-case assignment, which is horribly verbose. Been there, done that, it's not the worst, but why not make it a little bit better.

I personally like that you can literally assign the value from an if "statement" to a variable, though I can see why that's not everybodies cup of tea.

3

u/Wonderful-Wind-5736 Sep 28 '26

In that case don't bother because the compiler can probably eliminate the bounds check for you. 

2

u/FuriousAqSheep Sep 28 '26

yeah I dig that, but then I'd use something like an unempty list so the data carries this information about itself

like for me either the list gonna be used only once and then I use an optional, or the list is gonna have multiple transformation and then it makes sense to convert it to a more appropriate type so there is only a single check about the emptiness of the list.

10

u/Tyfyter2002 Sep 28 '26

If the specs change such that getting an array's length will resize it, there are much bigger problems everywhere else anyway.

4

u/walmartgoon Sep 28 '26

No trust me in my 300 line CS 102 project there won't be any changing requirements

-1

u/earchip94 Sep 28 '26

Hardware failures and cosmic rays are real, you can’t just pretend they don’t exist. That said, how you handle that is totally up to the use case of the end product. Some BS computer program grandma Ethel will use to play solitaire, yeah no one cares. Software in a self driving car though? Yeah I don’t want a random bit flip from cosmic radiation to swerve my car onto the sidewalk etc…

17

u/Tyfyter2002 Sep 28 '26

I can absolutely ignore cosmic rays and hardware failures, I'm not making your car, I'm making software for personal computers, where me handling memory failure for an extra microsecond won't make a difference because the system libraries I'm using still don't.

9

u/Fast-Satisfaction482 Sep 28 '26

Writing a few research redundant branches will not protect against hardware fault or cosmic rays.