• 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
Bounds checking in indexed accessor methods
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Bounds checking in indexed accessor methods


  • Subject: Bounds checking in indexed accessor methods
  • From: Mailing list subscriptions <email@hidden>
  • Date: Sat, 7 Oct 2006 12:25:03 +0200

What are people's thoughts on performing bounds checking in indexed accessor methods?

For example, see the sample methods pasted below: given an NSMutableArray instance variable called "sockets", you can generate "simple" indexed accessors that don't perform any bounds checking, or "robust" accessor that do perform bounds checking (these method implementations generated using Accessorizer: http:// www.kevincallahan.org/software/accessorizer.html).

If I were just accessing the NSMutableArray directly I would expect it to throw an exception if I used an out-of-bounds index. This would hopefully draw my attention to a programming error somewhere in my code which I could then fix. So I am tempted to think that "simple" accessors are the way to go. Are there any arguments for why I should use "robust" indexed accessors then?

I did find this old post by Scott Stevenson (http:// www.cocoabuilder.com/archive/message/cocoa/2004/3/3/100753) in which he says:

I've encountered situations where the array controller will request index 2147483647, which I would guess is the unsigned (rollover) version of -1. I'm guessing this is some sort of bug in the frameworks.

Anyone else seen this possible bug in recent versions of Mac OS X? In my own code (custom class with NSMutableArray instance variable, bound to NSTableView via an NSArrayController) I've put in logging statements to see when out of range values might be generated and I don't ever see any such values (neither adding nor removing rows, nor dragging and dropping to rearrange their order).


Any other thoughts?

Anyway, here are the accessors:

"simple"

- (unsigned int)countOfSockets
{
    return [[self sockets] count];
}

- (id)objectInSocketsAtIndex:(unsigned int)index
{
    return [[self sockets] objectAtIndex:index];
}

- (void)insertObject:(id)anObject inSocketsAtIndex:(unsigned int)index
{
    [[self sockets] insertObject:anObject atIndex:index];
}

- (void)removeObjectFromSocketsAtIndex:(unsigned int)index
{
    [[self sockets] removeObjectAtIndex:index];
}

- (void)replaceObjectInSocketsAtIndex:(unsigned int)index withObject: (id)anObject
{
[[self sockets] replaceObjectAtIndex:index withObject:anObject];
}



"robust"

- (unsigned int)countOfSockets
{
    return [[self sockets] count];
}

- (id)objectInSocketsAtIndex:(unsigned int)index
{
    id mySockets = [self sockets];
    unsigned int socketsCount = [mySockets count];
    if ( socketsCount == 0 || index > (socketsCount - 1) ) return nil;

    return [[[mySockets objectAtIndex:index] retain] autorelease];
}

- (void)insertObject:(id)anObject inSocketsAtIndex:(unsigned int)index
{
    id mySockets = [self sockets];
    unsigned int socketsCount = [mySockets count];
    if (index > socketsCount) return;

    if (anObject) [mySockets insertObject:anObject atIndex:index];
}

- (void)removeObjectFromSocketsAtIndex:(unsigned int)index
{
    id mySockets = [self sockets];
    unsigned int socketsCount = [mySockets count];
    if ( socketsCount == 0 || index > (socketsCount - 1) ) return;

    [mySockets removeObjectAtIndex:index];
}

- (void)replaceObjectInSocketsAtIndex:(unsigned int)index withObject: (id)anObject
{
id mySockets = [self sockets];
unsigned int socketsCount = [mySockets count];
if ( socketsCount == 0 || index > (socketsCount - 1) ) return;


    [mySockets replaceObjectAtIndex:index withObject:anObject];
}



_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Bounds checking in indexed accessor methods
      • From: Ricky Sharp <email@hidden>
  • Prev by Date: Re: (no subject)
  • Next by Date: Writing PDF with Outlines
  • Previous by thread: (no subject)
  • Next by thread: Re: Bounds checking in indexed accessor methods
  • Index(es):
    • Date
    • Thread