[BioCocoa-dev] Peptides...

Koen van der Drift kvddrift at earthlink.net
Mon Mar 28 21:01:53 EST 2005

On Mar 28, 2005, at 12:22 PM, Charles PARNOT wrote:

> 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.

The original issue for the Peptides example is not the difference 
between immutable and mutable. Instead, the issue was that the accessor 
symbolArray returned a copy of the symbolArray, instead of just a 
pointer to that array, which is usual the case with accessors. Copying 
the array highly slowed down the Peptides calculation, while it was not 
necessary at all to create a copy. Therefore I suggested that we have 
the standard accessor:

- (NSArray *)symbolArray
	return symbolArray;

If the user really needs a copy of the array, there are at least two 
possibilities: 1. we supply an additional accessor that returns a copy 
(using [[symbolArray copy] autorelease]) 2. the user uses the standard 
accessor, and creates the copy herself.

The mutable/immutable question is unrelated to this.

- Koen.

More information about the Biococoa-dev mailing list