Re: NSWindowController that closes itself
Re: NSWindowController that closes itself
- Subject: Re: NSWindowController that closes itself
- From: "John C. Randolph" <email@hidden>
- Date: Tue, 13 Nov 2001 20:13:16 -0800
On Tuesday, November 13, 2001, at 08:00 PM, Erik M. Buck wrote:
NSWindow *window = [self window];
id delegate = [window delegate];
The above two lines merely save two unnecessary messages. This is
probably
a premature optimization since this code in unlikely to be in a critical
performance path.
I didn't do it for optimization, I did it for legibility. ;-)
if(delgate && [delegate
respondsToSelector:@selector(windowWillClose:)])
Testing delegate could/should have been (nil != delegate), but it is
immaterial because sending -respondsToSelector: to nil is harmless and
returns nil which also happens to be NO.
Testing (nil != delegate) is much more important that avoiding
sending -respondsToSelector: to nil since Apple could introduce a "nil
object" that has an address other than 0 yet any message to nil will
still
return nil. Furthermore, the implicit conversion of the return value
from
nil to NO is exactly the same thing you are doing when you test delegate
without a Boolean binary comparison operator.
Nevertheless, we are all friends and this is just a style difference or
aesthetic difference. The optimization is unimportant here. I like
your
code too.
{
if([delegate windowWillClose:window])
[window performClose:self];
}
else
[window performClose:self];
Ah, that's better. ;-)
-jcr
For every complex problem there is an answer that is clear, simple, and
wrong. -- H L Mencken