Re: A question of style: Returning 'pairs'
I've run into this many times, and I think I've used all the techniques you mention and some others less hygienic. I've been most satisfied with your 2) and 3) solutions. There's not really that much overhead in making a struct or Obj-C class for two specific kinds of values, and once you've got it you know exactly what you're doing at all times. I like structs becaus they're so lightweight, and I like Obj-C classes (with properties, yay!) because structs are so ugly to declare. I note that Cocoa itself uses all these techniques, but maybe they lean toward structs with a few specialized functions for working with them, for constructs that will be used often like NSRange. On the other hand there's NSIndexPath; since it's a class, UITableView can create a category on it that provides properties. Very nice. On Jul 2, 2008, at 11:48 AM, James Montgomerie wrote: 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. Oh, and there's also 8) Rename the file .mm, and use a C++ std::pairid, id class. (Only joking :-) How would you do this? Are there other, better options? Jamie. ___ 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/casseres%40mac.com This email sent to [EMAIL PROTECTED] ___ 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]
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. Oh, and there's also 8) Rename the file .mm, and use a C++ std::pairid, id class. (Only joking :-) How would you do this? Are there other, better options? Jamie. ___ 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]
RE: A question of style: Returning 'pairs'
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: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] ] 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. Oh, and there's also 8) Rename the file .mm, and use a C++ std::pairid, id class. (Only joking :-) How would you do this? Are there other, better options? Jamie. .com ___ 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]
Re: A question of style: Returning 'pairs'
On Wed, Jul 2, 2008 at 1:48 PM, James Montgomerie [EMAIL PROTECTED] wrote: 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. This is what I would do. Sort of compliments NSRange and the functions that return it. ___ 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]
Re: A question of style: Returning 'pairs'
On Jul 2, 2008, at 3:22 PM, Andy Lee wrote: There is quite a bit of precedent for methods of this form -- in the Xcode documentation window, you can ...enter get in the searchf field, and *then*... do an API Starts With search, sort by language, and look at the Objective-C methods. This one is a good example: - (void)getCompression:(NSTIFFCompression *)compression factor:(float *)factor --Andy ___ 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]
Re: A question of style: Returning 'pairs'
I'd use an NSArray for this, wrapping the offset in an NSNumber. Joel P.S. Note to Mr. Butler: The correct term is complement; not compliment. ___ 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]
Re: A question of style: Returning 'pairs'
When I find myself in the situation you describe, my first instinct is to re-examine the basic design to see if the code can be re- structured. In your case, a better approach might be to have two separate methods, one which just returns the string, and another which takes a string as argument and returns the offset. That solves your immediate return-value problem, and is also a more flexible and re- usable design. But if the two operations are so interwined that separating them leads to excessive code duplication, a multi-valued return may be the best compromise; in that case my personal preference is a C struct, your option 2. Hope this helps! Bob S. On Jul 2, 2008, at 11:48 AM, James Montgomerie wrote: 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. Oh, and there's also 8) Rename the file .mm, and use a C++ std::pairid, id class. (Only joking :-) How would you do this? Are there other, better options? Jamie. ___ 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/bsmith%40h-e.com This email sent to [EMAIL PROTECTED] ___ 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]
Re: A question of style: Returning 'pairs'
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. I'd probably do it this way if the method was private to the module; with the struct passed in by reference thereby passing any memory management issues onto the caller, i.e. it could be stack allocated or malloc'd Otherwise if it was a public method, it follow the -get... convention returning both parameters by reference. Keith Duncan [EMAIL PROTECTED], 33software.com ___ 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]