FROM : Jean-Daniel Dupas
DATE : Wed Jul 02 21:14:54 2008
Le 2 juil. 08 à 20:56, Abernathy, Joshua a écrit :
> If it were an oft-used pair, I'd probably make a struct to hold them
> together, but if it's a just-this-one-method, just-this-one-time thing
> I'd settle with a Dictionary.
>
> Of course, you do have one more option: pass-by-reference or pointer.
> I've never been a big fan, personally, because I prefer to break
> methods
> into more discrete blocks and limit side effects, but it is an option
> and it's arguably more self-documenting.
>
> J
>
> -----Original Message-----
> From: cocoa-dev-bounces+jabernathy=burrislogistics.<email_removed>
> [mailto:cocoa-dev-bounces+jabernathy=burrislogistics.<email_removed>
> ] On Behalf Of James Montgomerie
> Sent: Wednesday, July 02, 2008 2:48 PM
> To: Cocoa-Dev List
> Subject: A question of style: Returning 'pairs'
>
> Say I have a method that needs to return two equally important values
> (in my case, a string and an offset into it). I am overthinking how
> to do it, and I though it would be interesting to see what others have
> done.
>
> I see these opportunities (my use of 'object' and 'value' is blurred
> below, since I'm thinking of the abstract case - assume that both
> values could be objects):
>
> 1) Just return the first value, and have the caller supply an argument
> that the second value gets written into (akin to how NSError is
> customarily used). This seems a bit unclean, since one value is not
> more important than the other, and both are necessarily returned.
> 2) Define a custom C struct (like NSRect, but with e.g. 'string' and
> 'offset' members) and return objects in it. Just like any other
> returned objects, the caller would be expected to retain them
> individually if it needed to keep them around.
> 3) Define a custom Obj-C class with two properties [e.g. 'string' and
> 'offset'] and return an object of that class (with properties
> appropriately set).
> 4) Create a 'Pair' C struct with two ids in it. Use it like the
> custom struct in (2). This struct is more reusable than the one in
> (2), so this solution seems less 'heavyweight', but it is less
> descriptive.
> 5) Define a 'Pair' Obj-C class with 'first' and 'second' properties,
> use as (3). Again, more reusable, less 'heavy' seeming than (3), but
> less descriptive.
> 6) Return an NSArray with two items in it (this seems the least
> descriptive option, from the point of view of someone reading the
> header).
> 7) Return an NSDictionary with two items in it, keyed by their
> property names. This seems a bit wasteful, since the dynamicisim of a
> dictionary is not required, and is also not so descriptive from a
> header-reading perspective.
>
Choice 9)
A function that return void (or a result code, or a boolean for
successful, failed) and take two parameters by ref.
void GetMyStringAnOffset(NSString **str, int *offset)
Like that, both parameters have the same importance ;-)
DATE : Wed Jul 02 21:14:54 2008
Le 2 juil. 08 à 20:56, Abernathy, Joshua a écrit :
> If it were an oft-used pair, I'd probably make a struct to hold them
> together, but if it's a just-this-one-method, just-this-one-time thing
> I'd settle with a Dictionary.
>
> Of course, you do have one more option: pass-by-reference or pointer.
> I've never been a big fan, personally, because I prefer to break
> methods
> into more discrete blocks and limit side effects, but it is an option
> and it's arguably more self-documenting.
>
> J
>
> -----Original Message-----
> From: cocoa-dev-bounces+jabernathy=burrislogistics.<email_removed>
> [mailto:cocoa-dev-bounces+jabernathy=burrislogistics.<email_removed>
> ] On Behalf Of James Montgomerie
> Sent: Wednesday, July 02, 2008 2:48 PM
> To: Cocoa-Dev List
> Subject: A question of style: Returning 'pairs'
>
> Say I have a method that needs to return two equally important values
> (in my case, a string and an offset into it). I am overthinking how
> to do it, and I though it would be interesting to see what others have
> done.
>
> I see these opportunities (my use of 'object' and 'value' is blurred
> below, since I'm thinking of the abstract case - assume that both
> values could be objects):
>
> 1) Just return the first value, and have the caller supply an argument
> that the second value gets written into (akin to how NSError is
> customarily used). This seems a bit unclean, since one value is not
> more important than the other, and both are necessarily returned.
> 2) Define a custom C struct (like NSRect, but with e.g. 'string' and
> 'offset' members) and return objects in it. Just like any other
> returned objects, the caller would be expected to retain them
> individually if it needed to keep them around.
> 3) Define a custom Obj-C class with two properties [e.g. 'string' and
> 'offset'] and return an object of that class (with properties
> appropriately set).
> 4) Create a 'Pair' C struct with two ids in it. Use it like the
> custom struct in (2). This struct is more reusable than the one in
> (2), so this solution seems less 'heavyweight', but it is less
> descriptive.
> 5) Define a 'Pair' Obj-C class with 'first' and 'second' properties,
> use as (3). Again, more reusable, less 'heavy' seeming than (3), but
> less descriptive.
> 6) Return an NSArray with two items in it (this seems the least
> descriptive option, from the point of view of someone reading the
> header).
> 7) Return an NSDictionary with two items in it, keyed by their
> property names. This seems a bit wasteful, since the dynamicisim of a
> dictionary is not required, and is also not so descriptive from a
> header-reading perspective.
>
Choice 9)
A function that return void (or a result code, or a boolean for
successful, failed) and take two parameters by ref.
void GetMyStringAnOffset(NSString **str, int *offset)
Like that, both parameters have the same importance ;-)
| Related mails | Author | Date |
|---|---|---|
| James Montgomerie | Jul 2, 20:48 | |
| Abernathy, Joshua | Jul 2, 20:56 | |
| Stephen J. Butler | Jul 2, 20:58 | |
| Jean-Daniel Dupas | Jul 2, 21:14 | |
| Andy Lee | Jul 2, 21:22 | |
| Andy Lee | Jul 2, 21:30 | |
| Joel Norvell | Jul 2, 21:33 | |
| Bob Smith | Jul 2, 22:03 | |
| Keith Duncan | Jul 2, 22:19 | |
| David Casseres | Jul 4, 04:02 |






Cocoa mail archive

