Re: Wisdom of overriding isEqual:
Re: Wisdom of overriding isEqual:
- Subject: Re: Wisdom of overriding isEqual:
- From: Chris Kane <email@hidden>
- Date: Wed, 10 Mar 2004 08:14:54 -0800
On Mar 10, 2004, at 5:46 AM, Wade Tregaskis wrote:
3) isEqual: exists as a 'hook' to customize the way (mostly)
collection classes such as NSArray and NSDictionary handle
objects.
<snip>
My question is: won't this trip up any of the internal workings of
Cocoa?
Suppose 'somewhere insyde' Cocoa relies on the default implementation
of
isEqual:?
If you use a category to a Cocoa class you might override a built in
implementation of isEqual. It's unlikely, but possible, that this
would
interfere with the normal operations of that class.
So long as your overrided isEqual does perform that task, comparing
equality, it should not interfere with any normal operation. Any
problems that arise would be bugs in Apple's code; i.e. dependencies
on undocumented and undefined behaviour, which of course is the first
thing you learn not to do in any OO programming course.
[...]
For isEqual, the required behaviour is about as simple as you can get,
so there's no room for error (aside from the possibility that your
overrided version doesn't compare equality properly, but that is of
course a given). Override to your hearts content. :)
Implementing equality-testing properly, particularly when adding
additional tests of additional properties, isn't trivial to do
properly, particularly when comparing objects of different [sub]types
[of the same superclass]. For example, if there are multiple
subclasses (perhaps privately) of NSURLRequest floating around in the
app [now or in a future OS release], changing the behavior of the
supertype's equality check, even if in some sense "correct" in
isolation, could produce strange effects or incorrect behavior.
See, for example, numerous articles in Dr. Dobb's Journal, C/C++ User's
Journal (and its Java Solutions adjunct), and various Java programming
magazines in 2001, 2002, and 2003. Google can probably help find
articles too. I recall some articles referring to advice given in some
books and past articles as "bad", so you can't necessarily believe
everything you read in one place either.
Another effect could be produced on performance by an override which
checked additional things. As the OP pointed out, isEqual is a
[low-level] hook used in the context of dictionaries and sets, and
other uses. Slowing down isEqual might slow down other things using
NSURLRequest (including the internals of Cocoa). Which, I suppose,
would be a case of Getting What You Asked For.
Chris Kane
Cocoa Frameworks, Apple
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.