Re: substring return a new string?
Re: substring return a new string?
- Subject: Re: substring return a new string?
- From: Alastair Houghton <email@hidden>
- Date: Thu, 18 Mar 2004 23:27:47 +0000
On 18 Mar 2004, at 20:28, Ondra Cada wrote:
>
Marco,
>
>
On Thursday, Mar 18, 2004, at 21:11 Europe/Prague, Marco Scheurer
>
wrote:
>
>
> It may be safe, but it does not make it right. What you do is
>
> advertising a mutable property of your object,
>
>
No I am not--not in the sense at least I understand "property". Does,
>
for example, -[NSFileManager subpathsAtPath:] return a "file manager
>
property"? This case, I am just _generating an object on the user's
>
behalf_. The object happens to be mutable. I can see absolutely no
>
problem in that.
I think the issue is that in general it is the *value* that is
interesting, rather than the object itself, and therefore in the
majority of cases it doesn't make sense to return mutable objects. I
can see why in some circumstances it would be useful to return mutable
objects, particularly with container objects, but IMO the *right* way
to do this is to create *two* methods, e.g. -someArray and
-someMutableArray.
This approach has the benefits that:
1. It follows the naming precedent established by -copy and
-mutableCopy, in that the method that returns a mutable object
explicitly says "mutable" in the name. This makes it very clear,
without needing notes in the documentation, that a method returns a
mutable object.
2. It allows users of the class to specify what it is that they need,
leaving the implementation details of how that object is generated up
to the class. The implementation can use an efficient means of
obtaining an immutable array, then -mutableCopy it in the
-someMutableArray method, or it can use an efficient means of obtaining
a mutable array and just return it from -someArray. Equally, it could
have a separate implementation of the mutable and immutable methods in
order to achieve high efficiency whilst enforcing immutability. But
whichever approach the class takes, its users don't need to care.
In short, it has the benefits of both approaches and I see no obvious
disadvantages---other than perhaps creating an additional variant of
many methods.
Kind regards,
Alastair.
--
http://www.alastairs-place.net
[demime 0.98b removed an attachment of type application/pkcs7-signature which had a name of smime.p7s]
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.