Re: passing a method name?
Re: passing a method name?
- Subject: Re: passing a method name?
- From: Graham Cox <email@hidden>
- Date: Wed, 23 Dec 2009 10:22:08 +1100
On 23/12/2009, at 6:20 AM, glenn andreas wrote:
>> But when the kind of situation that calls for it arises, it might seem very
>> natural and elegant. I'm just suggesting that we not condemn an architecture
>> out of hand merely because we've no experience of it. m.
>>
>
> And isn't even that uncommon - consider all the various kvc methods based on common name (as an example of manipulating a common root name for multiple purposes).
>
> And I've written menu validation code that manipulates the selector to figure out the name of the method to actually handle the validation (which is more of the "completion method" style thing - dynamically figure out what to call based on the SEL passed in)...
Yes, but this is still talking about something different from what the OP was (or appeared to be) suggesting. It's fine to CALL one of n methods based on a e.g. rootname + suffix(n), say. That's just a variation on table-based dispatch.
What isn't fine is to pass (my own method name) to another method and have that single method take different branches according to it. That's just setting up an unnecessary dependency between two separate pieces of code that a) doesn't need to be there b) will make it difficult to debug and c) difficult to document. It's completely avoidable by factoring the code in a more logical way. If you find yourself needing to do this, it's simply telling you that the place where you decided to factor your code was badly chosen, or that you need to parameterise it more sensibly.
This is exactly the sort of code smell we were always on the look out for at code reviews, and it was always avoidable. In every case, the revised code was more elegant, maintainable and straightforward. I don't believe there is a genuine case where it would be more "natural and elegant" to do this than to take a more conventional approach. I'm also unaware of any part of Cocoa doing this.
Going back to the OP's post, he says: "currently, i'm passing a string object so the called method will know which method had called it". That's not table-based dispatch, that's just bad programming. If he's not also passing a target, this doesn't sound like a completion method or callback, unless the target is implied to be the object's delegate or some other implied target. Until the OP clarifies what he meant, I'm taking it at face value, and this is a no-no.
--Graham
_______________________________________________
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