• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Is checking -count worth it?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Is checking -count worth it?


  • Subject: Re: Is checking -count worth it?
  • From: Phil <email@hidden>
  • Date: Thu, 24 Jul 2008 17:38:53 +1200

On Thu, Jul 24, 2008 at 4:15 PM, Steve Cronin <email@hidden> wrote:
> I have a mutable array, M.  I want to remove an object, O, from M; O might
> not be in M.
> M which is being constantly diminished could become empty.
>
> Is this code worth it?
> if ([M count]>0) [M removeObject:O];
>
> OR should I just do
> [M remove O];
>

I'm not quite sure what you gain in the first case? If the array is
already empty, then what extra processing do you imagine will be in
-removeObject: that you will save by checking -count first.

You may be making assumptions about how the mutable array works
internally (I'm assuming you mean an NSMutableArray), which is a bad
thing to do for any class and in the case of Apple's collection
classes, the internals can change on you:
<http://ridiculousfish.com/blog/archives/2005/12/23/array/>

I think that your first piece of code is far less readable than the
second, because I don't find it obvious why you're checking -count
first?

And as mentioned, measuring performance is the only way to get a true
answer as to which method is better.

Phil
_______________________________________________

Cocoa-dev mailing list (email@hidden)

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:

This email sent to email@hidden

References: 
 >Is checking -count worth it? (From: Steve Cronin <email@hidden>)

  • Prev by Date: Help on Menu Item
  • Next by Date: Sent Actions, Delegates, Outlets
  • Previous by thread: Re: Is checking -count worth it?
  • Next by thread: Re: Is checking -count worth it?
  • Index(es):
    • Date
    • Thread