3 ms·
I’m not particularly impressed by of any of these extensions, because they don’t seem to actually fix the underlying problems. I used to write code that was ove
by saagarjha 8y ago
I’m not particularly impressed by of any of these extensions, because they don’t seem to actually fix the underlying problems. I used to write code that was overly defensive around Optionals, and I’d carefully do things like wrapping in flatMap or throw guards in, but I’ve come to realize that this just ends up hiding errors in the cases where you know something is supposed to have a value. For this article in particular, I think there are better ways to do what he’s trying to do:
For the converting an Optional to an error example, he could just put both statements in one guard. Sure, being able to show that you’re passing a function into another is pretty, but it really is overkill for something that doesn’t even need an extension to Optional.
I think isNilOrEmpty, personally, just encourages papering over bad APIs: it is never correct to represent an empty Collection with nil. UITextField.text should never be nil (to the point where I think that this method is improperly annotated; I can go check though); if it is your view hierarchy is in a state where asking it for its text returns nil this should be a hard error because you must have messed up somewhere. Just force unwrap in this case, and if you’re designing an API, think twice about returning Collection?.
Lastly, get(orSet:) is just a skirting around the issue. Forget to set the value of this view? Just set it again! Just ignore the fact that a clear logic error that must have occurred if I ended up at a point where I’m trying to access a view that doesn’t exist.
- tabs_masterrace 8y agoAgree with most of it, having .(unwrap)orThrow() seems nice syntax sugar though.
- aaaaaaaaaab 8y agoThese are my rules of thumb in priority order: 1. Don’t use optionals. 2. If you must use optionals due to technicalities (e.g. IBOutlet, weak reference, etc.), make it implicitly-unwrapped. 3. If you can’t make it implicitly unwrapped due to ‘nil’ being a legitimate value, then use regular optionals, but force-unwrap it everywhere except where `nil` is expected to occur. There’s nothing worse than a codebase unnecessarily littered with guard let x = x else { return } If my assumptions about some value were wrong, I want it to break as loud as possible. Silently returning (or making up some silly default value like an empty string or zero) is the worst thing you can do for yourself and your colleagues. (Except if you’re a manager whose bonus is tied to your app’s crash stats)
- dkarbayev 8y agoYour approach seems to be a completely wrong use of optionals (have you been writing in Java or any other language with implicit nullability?) since you’re giving up type-safety of Swift. Do you use implicitly unwrapped optionals for delegates too?
- saagarjha 8y agoDelegates should be nil, so it totally makes sense to keep them as an optional. I don’t see this conflicting with the parent’s set of “rules”.
- aaaaaaaaaab 8y ago>since you’re giving up type-safety of Swift Giving up type-safety? Sorry, but I don't understand this in the current context. As for delegates: if the delegate can be 'nil' during normal operations, then it should be a regular optional as per rule #3.
- b_t_s 8y agoisNilOrEmpty is great, though I prefer my variation nilEmpty that turns empty things into nils. Slightly more flexible/chainable. The reality is that there are a lot of cases where the empty state could be nil or an empty collection/string/whatever. I try not to add to them, preferring to use the empty collection over nil, however handling both is generally a lot easier and safer than trying to prove that the data from some other system can't be dumb.