Re: Convenience Methods
Re: Convenience Methods
- Subject: Re: Convenience Methods
- From: Wade Tregaskis <email@hidden>
- Date: Wed, 26 Sep 2007 17:05:23 -0700
@class Animal
+ (Animal*)animalWithName:(NSString*)n { ... }
- (Animal*)initWithName:(NSString*)n { ... }
@end
@class Dog(Animal)
@end
Now, in this case, you can call
Dog *pet = [Dog animalWithName:@"Rover"];
but that feel uncomfortable to me. I'd still be inclined to add
another
method so I could write:
Dog *pet = [Dog dogWithName:@"Rover"];
But you could still say [Dog animalWithName:]. A dog is still an
animal, right? I mean, it's subjective, sure, but the next step along
your example is germanShephardWithName:, and it just continues
downhill from there... generalism for teh win.
Additionally, with the above approach you've now got two identical
ways to create a dog, unless you do something mean like override
animalWithName: in your subclass and break it. And then you can't
write very generic code, where you might have some random class that
you know is an animal (e.g. you got it from an existing instance of an
animal), but you don't know anything else - then you do appreciate
having a generic method which works for all subclasses.
I appreciate what you're getting at, and I wouldn't particularly care
if I was using your Animals framework and there was this extra
dogWithName: method on the Dog subclass... but I probably wouldn't use
it. And I think it's just dodging the larger issue, which is that
[Dog dogWithName:], like [Animal animalWithName:], is redundant.
Something like [Animal createWithName:] would be better (which also
makes it explicit that you're creating a new animal...
"animalWithName:" sounds to me like NSImage's imageNamed:, which
returns the existing instance with that name, not a new one).
Wade
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden