[BioCocoa-dev] Peptides...

Charles PARNOT charles.parnot at stanford.edu
Sat Mar 26 03:11:03 EST 2005

At 10:48 AM -0500 3/25/05, Koen van der Drift wrote:
>On Mar 25, 2005, at 8:40 AM, John Timmer wrote:
>>>//    return [[symbolArray copy] autorelease];
>There are indeed some more places where this construction is used. I understand the carefulness why this was introduced, but in most cases speed is probably a more important factor. I don't think we should throw these out, but maybe we should think of adding some accessors that have the word 'copy' in it. These can then be called when a copy is really needed, instead of only a pointer. For instance:
>- (NSArray *)copyOfSymbolArray  or - (NSArray *)symbolArrayCopy
>Or something else. If we make the difference clear in the docs, I don't think there will be an issue. What do you think?
>- Koen.

I don't think we need to put any of that, because the returned object is typed as an NSArray, so the interface would be confusing for the user: what is the difference between an NSArray and its copy!?. It can be left as is: anybody trying to use NSMutableArray methods on the object returned will have compiler warnings. So even the not-too-careful user won't mess up with the original array.

However, returning the original object (disguised as an NSArray) can bite us back in the near future. Suppose you are the user, you get the array back as a snapshot of the sequence at the time it is returned. Then you modify the BCSequence. You would expect the NSArray to still be the same, e.g.:

BCSequence *seq;
NSArray *myArray=[seq symbolArray];
int i=[myArray count];
[seq appendSequence:@"ATGT"];
int j=[myArray count];
NSAssert (i=j;@"An NSArray can't change!?? What happened");

This might seem trivial, but it can be much more subtle than that and very difficult to debug when you expect an NSArray to be immutable. With multithreading, things can get even uglier.

I did not follow the peptides story, but would an immutable sequence object be useful in that context? If yes, then here is a hint that something needs to be done. (it is in my agenda)

Another option is to have a 'mutableSymbolArray' and 'symbolArray' methods.


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