4 ms·
I just avoid using the copy(_:) method. I feel that it's one of those "code smell" flags. If I find myself contemplating using it, I probably need to revisit my
by ChrisMarshallNY 2y ago
I just avoid using the copy(_:) method. I feel that it's one of those "code smell" flags. If I find myself contemplating using it, I probably need to revisit my design. This is the kind of stuff that does not age well.
Those of us "of a certain age," may remember the pre-MacOSX CopyBits[0] function.
That was fun. :P
[0] http://preserve.mactech.com/articles/develop/issue_06/Othmer_text_.html http://preserve.mactech.com/articles/develop/issue_06/Othmer...
- klodolph 2y agoLots of fuss to make sure you stay on the CopyBits fast path… no masking, rectangular regions, no format conversions, color table has identical ctSeed, and you can even get a pointer to a lower-level, specialized version of CopyBits if you need.
- meindnoch 2y agoIf you have objects storing NS(String|Array|Dictionary)s, you ought to copy them before storing, otherwise you might inadvertently end up storing an NSMutable(String|Array|Dictionary), which can get updated behind your back, producing hard to debug issues.
- KerrAvon 2y ago`copy` alone is not a red flag. There are definitely cases where you need to use it for safety -- mutable pair types like property list types, for example. But you can't trust an object like -- for example -- NSWindow to do something sane with it unless the behavior is explicitly documented.
- ChrisMarshallNY 2y agoWell, most of my work, these days, is in UIKit, so I'm fairly used to "let" not meaning much. I also tend to rewrite the code completion implicit unwraps (!) with explicit ones (?). Forces me to acknowledge that I'm skating on thin ice. If there's something that I need to keep around, I make a local let copy of just that property, as opposed to the whole shooting match that contains it, or I look up the value in a JiT manner. I tend to have a lot of computed properties. I'll often extend base classes and structs, to add computed accessors.
- meindnoch 2y ago>I also tend to rewrite the code completion implicit unwraps (!) with explicit ones (?). Forces me to acknowledge that I'm skating on thin ice. Hold on. Do you prefer your code failing silently instead of crashing?
- ChrisMarshallNY 2y agoNope. I can't use the properties without explicitly unwrapping them. Implicit means that I can pretend they aren't optional. But I often have lines that use ?? to bring in alternatives, and will, if the occasion calls for it, allow the app to fail silently. I tend to like using things like assertions and preconditions, if I want to make the app crash.