Re: Is Apple's singleton sample code correct?
Re: Is Apple's singleton sample code correct?
- Subject: Re: Is Apple's singleton sample code correct?
- From: Uli Kusterer <email@hidden>
- Date: Mon, 28 Nov 2005 09:54:18 +0100
Listening to this thread going on and on, I can't help but think
there's over-engineering going on. The OP was simply wondering
whether there was a reason for Apple's singleton sample code being so
complicated, and it looks like we've found it's not really in any way
necessary to have this code for the typical singleton (i.e. like
+sharedWorkspace).
The case where this singleton code seems not un-sensible is for
constant stuff, such as [NSNumber numberWithInt:1], though even there
I'd personally consider it a matter of taste whether you'd want to go
to such lengths.
I can see how one would warn or prohibit certain memory management
practices on a singleton to catch usage bugs (like deallocation
through unbalanced -release calls, which is easily detected for an
object that's supposed to live as long as the app). This is
especially important when you have lots of projects, or co-workers
who may not be familiar with the code you wrote.
Apart from those considerations, I really think the reduction of
lines of code (as Wil Shipley would put it: "a deleted line of code
is a debugged line of code") is much more important than trying to
come up with a complex safety net of code that only introduces new
bugs. Just look at the code samples posted and all the bugs people
pointed out in them.
So, different from other Apple recommendations, I'd suggest to every
newbie to just ignore this one. Write the simplest singleton code: A
class-level accessor that lazily instantiates a regular object into a
static variable. Maybe override -dealloc to NSLog() a warning. Leave
the more complicated stuff until you're actually tracking down an
insiduous bug that seems to be related to the singleton or once
you've profiled using Shark and know for sure that a particular
singleton is a bottleneck and needs to be optimised.
Just my $0.02.
Cheers,
-- M. Uli Kusterer
http://www.zathras.de
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden