this post was submitted on 19 Nov 2025
40 points (100.0% liked)
Programming
23557 readers
125 users here now
Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!
Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.
Hope you enjoy the instance!
Rules
Rules
- Follow the programming.dev instance rules
- Keep content related to programming in some way
- If you're posting long videos try to add in some form of tldr for those who don't want to watch videos
Wormhole
Follow the wormhole through a path of communities !webdev@programming.dev
founded 2 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
They used .unwrap(...) in production, which can escape notice until there's an error, then it panics. It's better to always handle the potential error, or at least use ? to pass the error back to the caller.
I've read a lot of people saying "oh what a horror, they used unwrap in production, they should NEVER do that". But I don't think that's true at all.
That being said, this was clearly a case where
?should have been used, since the function has a clear error case. And it also is a critical application that can't afford panicking as an error handling method.EDIT: Just to clarify, the way you would do this is by having a type
Featuresthat can error on creation/parsing which has a limit of 200 entries. And afterwards you can just use that type knowing that it will always have <= 200 entries. And you gracefully handle that one error case on creation/parsing.EDIT2: Another point for why I disagree with "production should never have unwrap":
assert(my_variable.is_some). I don't know a single person that would say "you should never have asserts in production code", the only reason I disable some assertions for release builds is for performance. Asserts are statements that should never do anything, by that logic they pose no harm by being there, so no need to remove them, their only effect should be wasting a few CPU cycles. Since asserts are only put when the programmer assures that a certain precondition will always be met. And if it is not, it's because the code changes and automatic tests should catch that.Thank for elaborating my comment, but I never said never, only that it's usually better to avoid it.
And yven if you think it's provably impossible to get an Error back now, someone or something may change an underlying function behaviour on you in the future and invalidate your proof. There are ways to limit that with version control and pinning and so on, but it's easy for an assumption to be overlooked when merging in new versions of things.
So yes, I agree, better to use ? at least here, but like all guidelines, there may be times where you break it, accepting the risks.