[BioCocoa-dev] Peptides...

Charles PARNOT charles.parnot at stanford.edu
Mon Mar 28 12:22:12 EST 2005

>I think you switched them around. IMO it should be as follows:
>- (NSMutableArray *)mutableSymbolArray
> {
>   return [[symbolArray mutableCopy] autorelease];		// do we need an autorelease here?
> }
> - (NSArray *)symbolArray
> {
>   return symbolArray;
> }
>- Koen.

About the autorelease, yes: 'copy' adds one to the retain count, just like alloc/init. So we need to autorelease.

Now, regarding the switching, there was a misunderstanding at some point...

My issue is: all the problems come if you return a NSMutableArray instead of a NSArray in the symbolArray accessor. When you tell the user it gets an NSArray, the user expects that the array will never change. But if we return the ivar, the array may change and this will yield unexpected and difficult to debug results.
for instance:

BCSequence *seq=[BCSequence sequenceWithString:@"ATGT"];
NSArray *symbols=[seq symbolArray];
int n=[symbols count];
[seq removeSymbolAtIndex:1];
BCSymbol *last=[symbols objectAtIndex:n]; //UNEXPECTED EXCEPTION

To make things clear, my 2-cents idea was that we could return the NSMutableArray ivar directly when performance is an issue. The headerdoc would clearly tell that the method returns a pointer to the ivar, and that it should only been used when performance is an issue (and it is actually already in the headerdoc, yes, Alex;-). Even if the user does not read the doc, at least it can't expect the returned array to be immutable and may be more careful anyway and if it behaves in a weird way, the fact that it is an NSMutableArray should be a clue.

My opininon is the standard accessor 'symbolArray' should behave in a very standard way, because the average user will use it and it should be rock-solid. So when we say we return an immutable NSArray, it should really return an immutable NSArray.

What do the others think? Alex? John? Phil?...

Ultimately, we will probably want immutable versions of the sequence classes. Or is is just me? ;-)


Help science go fast forward:

Charles Parnot
charles.parnot at stanford.edu

Room  B157 in Beckman Center
279, Campus Drive
Stanford University
Stanford, CA 94305 (USA)

Tel +1 650 725 7754
Fax +1 650 725 8021

More information about the Biococoa-dev mailing list