12 ms·
I’ve run into the exact same situation and continue to repeatedly. In fact this is a stupidly common pattern in a library in working on at work right now. My s
by 52-6F-62 7y ago
I’ve run into the exact same situation and continue to repeatedly. In fact this is a stupidly common pattern in a library in working on at work right now.
My solutions have involved casts (and comments explaining the assumptions involved) instead of the ‘if’ statement more times than I’d like to have done, but it ends up with the same result with the added (however small) compute with the conditional since it can be safe to assume the value is not undefined. It’s not perfect, but it at least omits unnecessary runtime code.
- cloverich 7y agoIts unnecessary until the clause above it changes and the assumption no longer holds. That's how I fight my urge to go with the cast.
- 52-6F-62 7y agoThat’s totally fair. Admittedly in my situation I have high degree of control over the input so my constraints are probably a little looser. At any rate, I’d appreciate the same thing you would in these cases.