4 ms·
Because they don't need to be strong. I like to establish basic Quality habits. This is one. I declare object references as weak (or, sometimes -quite rarely,
by ChrisMarshallNY 4y ago
Because they don't need to be strong. I like to establish basic Quality habits. This is one.
I declare object references as weak (or, sometimes -quite rarely, unowned) by default. If they need to be strong, either the compiler will let me know, when I try assigning to it, or the runtime will let me know with a bad access crash. Much quicker debugging than chasing reference loops. Crashes are actually good. I find the cause fairly quickly. I usually don't declare local scope variables weak; only ones that escape the scope.
I have found that I seldom need to switch to strong.
I also do other "useless" things, like setting the constant before the variable, in comparisons:
"Good Grief" == whatCharlieSays
I also tend to "in-name" incoming arguments, like so:
func compare(thisValue inThisValue: Int, toThisValue inThatValue: Int) -> Int
I also write @IBOutlets as explicit optionals, like so:
@IBOutlet weak var someLabel: UILabel?
as opposed to the default:
@IBOutlet weak var someLabel: UILabel!
This forces me to unwrap everywhere. I avoid implicit optionals like the plague. I use a lot of guards. I will unwrap implicit optionals in system-provided resources, even though it isn't necessary.
I don't really care whether or not they are "necessary," or what others think of me. Every little bit helps. I'm very conscientious about the code I write.
I write it very quickly, too. Once something becomes habit, it moves rapidly.
"We are what we repeatedly do. Excellence then, is not an act, but a habit"
- Incorrectly attributed to Aristotle
- pixel_tracing 4y agoNot sure if declaring weak objects all the time is a good practice, you can introduce silent bugs due to an object deallocating unintentionally in scope. Apple actually recommends using strong with proper architecture and relationships between objects. See https://developer.apple.com/wwdc21/10216 https://developer.apple.com/wwdc21/10216
- ChrisMarshallNY 4y agoReference loops are also “silent” bugs. They are the worst kind of silent, because they usually stay hidden until long after the bug was introduced. My way forces me to deal with the bug, very close to causing it. The bugs aren’t really silent. If the code doesn’t get executed (because of all my guards and whatnot), it’s still a bug. It becomes apparent, quite quickly, and is easier to find. I just put a breakpoint in the unexecuted code, and watch it not happen. Lot better than trying to read tea leaves with Instruments, a month after causing the loop. The issue still happens, but it does so, in a way that is predictable. It’s like deliberately filing weak points into a support strut, so that breaks happen in a known place. The problem isn’t the break, but the orthogonal shear that happens at that point, and the weak point simply forces the break to happen in a place that it can be contained. It almost never happens, but when it does, it saves me a great deal of time. That’s the old McConnell thing about finding bugs near the point of introduction.
- saagarjha 4y ago> Crashes are actually good. I find the cause fairly quickly. I agree, but I can't really square that with the rest of your comment. When you declare everything as weak, things don't crash, you just lose objects and they turn into nil out from under you. Like, the reason your IBOutlets can be weak is your view controller holds onto its view, and then that either directly or indirectly references the thing you actually care about. But if you actually care about it, you should also take responsibility for its lifetime, no? Why rely on some implicit chain to keep the object alive? If you're not careful, you might end up silently dropping the object if you temporarily remove it from the view for layout reasons. It seems very much the opposite of the part where you seem to like manually unwrapping things rather than letting this happen implicitly (which I FWIW don't really agree with, but I don't really want to get into unless you want to hear it :P). I should also note that using weak references comes with a performance penalty over strong ones, but that's a tangential issue.
- ChrisMarshallNY 4y agoThe performance thing is a valid point, but, if I were working on something with margins that tight, I’d probably use unowned, or strong, anyway. There are a significant number of folks that would sneer at me for even using optionals, in the first place, because, in their world, we should not be using classes, at all. It’s always fun to encounter that. And, like I said, it really only applies to escaped/closure arguments, or class/instance properties. Locals are almost always strong, and I always unwrap in a guard/if before accessing, anyway, even if it is a local optional (which could introduce performance issues in tight spaces). Reference loops are a huge pain to debug. It takes a lot less time to find the cause of a crash or unexecuted code, than a loop. An important companion to always using weak, is also using lots of guards and tested unwraps. Without those, always using weak is just a bad habit, and sweeping bugs under the rug. People are always happy to judge my coding habits, such as insisting on heavily documenting my code, and they are not always wrong, but I’ve been at this for a while. I get a lot done, quite quickly, and at a very high Quality level. It’s hard to argue with results.
- saagarjha 4y agoI guess I just feel that the silent failures that come from things being weak generally outweigh the leaks that come from reference cycles