[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Setter Gettor method style
From: |
Pascal Bourguignon |
Subject: |
Re: Setter Gettor method style |
Date: |
Sat, 3 Aug 2002 12:44:25 +0200 (CEST) |
> From: Martin =?iso-8859-1?Q?H=E4cker?= <mhaecker@mac.com>
> Date: Sat, 3 Aug 2002 11:27:08 +0200
>
> Hi there!
>
> Sorry if I spring in here, but I'd like to ask you about a topic wich
> was discussed in depth on the Apple Cocoa Mailing list lately:
>
> The question was how getter setter methods are written and they came
> to the conclusion that these are the way's:
>
> --- snip ---
> Method 1: The following pair is good if performance of "get" is
> important (say you expect it to be called it thousands of times a
> second):
>
> - (NSString *) title {
> return title;
> }
>
> - (void) setTitle: (NSString *)newTitle {
> [title autorelease];
Here, there is not advantage to wait before releasing title. It's
owned by the object, it can be released directly. If anybody has been
interested in title it will have retained it long ago. Since title is
overwritten immediately, it cannot be retrieved after this statement,
so you can as well use release.
> title = [newTitle copy];
> // or retain, depending on object & usage
> }
>
> (as an optimization, you can do "if (title != newTitle)" in the set method).
>
>
> Method 2: Otherwise use the following pair, where the return value
> is autoreleased in the scope of the caller, which is more correct.
> This is also more (but not fully) thread-safe as the returned value
> is autoreleased in the calling thread.
>
> - (NSString *) title {
> return [[title retain] autorelease];
This is strictly equivalent to { return title; }
> }
>
> - (void) setTitle: (NSString *)newTitle {
> if (title != newTitle) {
> [title release];
> title = [newTitle copy];
> // or retain, depending on object & usage
> }
> }
> Ali Ozer
> --- snap ---
>
> All this because of complicated manners which you can read about in
> this thread:
> http://cocoa.mamasam.com/COCOADEV/2002/07/2/41217.php
[object retain] and [object release] only increment and decrement the
retain count. The fact that release can delete the object in some
cases is irrelevant when the release count is greater than 1.
> Now, what I'd like to ask you is what you think about it and what
> style is used in the GnuStep frameworks?
> Thanks a lot!
>
> cu Martin
> --
> dont.wanna.tell
> [ot]coder - hehe
--
__Pascal_Bourguignon__ http://www.informatimago.com/
----------------------------------------------------------------------
- ANNOUNCE: netclasses 0.96, Andy Ruder, 2002/08/03
- Setter Gettor method style, Martin Häcker, 2002/08/03
- Re: Setter Gettor method style,
Pascal Bourguignon <=
- Re: Setter Gettor method style, Martin Häcker, 2002/08/03
- Re: Setter Gettor method style, Alexander Malmberg, 2002/08/03
- Re: Setter Gettor method style, Pascal Bourguignon, 2002/08/03
- Re: Setter Gettor method style, Martin Häcker, 2002/08/04
- Re: Setter Gettor method style, Alexander Malmberg, 2002/08/04
- Re: Setter Gettor method style, Martin Häcker, 2002/08/04
- Re: Setter Gettor method style, Pascal Bourguignon, 2002/08/04
- Re: Setter Gettor method style, Martin Häcker, 2002/08/05
- Message not available
- Re: Setter Gettor method style, Pascal Bourguignon, 2002/08/04
- Re: Setter Gettor method style, Martin Häcker, 2002/08/04