On 28 Aug 2008 11:52:46 -0400, "Michael Ash" <[EMAIL PROTECTED]> wrote:

On Wed, Aug 27, 2008 at 6:59 PM, R.L. Grigg
<[EMAIL PROTECTED]> wrote:
Hmm, I guess the wrinkle in this particular case is if the "contract" doesnt
specify something that the programmer assumes to be safe to do (like
enumerating backwards), how can you know how to implement your end? I guess
there are times when the underlying implementation details can/must
influence your highlevel design?

When at all possible, your code should only be influenced by
underlying implementation details in the realm of performance, not
correctness.

This business of copying the array is a good example. It's fine to
write code that depends on the array no longer being copied, so long
as it still performs acceptably (although potentially worse) if the
array is copied.

There are a lot of cases where you have to make assumptions about
which technique is fastest, and this in turn depends on how those
techniques are implemented at a low level. But so long as your code
merely becomes slower when the assumptions change, but still
functions, this is generally not a problem.

In the case of enumerating backwards, the documentation says a lot
about how you should never mutate collections that you're enumerating
and nothing about it being safe to mutate while enumerating backwards.
The conclusion there should be obvious.

While if fully agree with you about valid assumptions and so, I am still wondering what is the disadvantage of forgetting about NSEnumerator, Fast Enumeration and the like and simply doing:

unsigned count = [ array count ];
if ( count == 0 ) return;
for( unsigned i = count - 1;; i--)
{
        id a = [ array objectAtIndex: i ];
        if ( a is not nice ) [ array removeObjectAtIndex: i ];
        if ( i == 0 ) break;
}

Maybe not "Fast" as in "Fast Enumeration" but maybe simpler and faster than copying all the "nice" objects into a secondary array.


Kind regards,

Gerriet.

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to