-
Notifications
You must be signed in to change notification settings - Fork 109
Open
Description
The guide makes recommendations about delegate style, but not when to use them. I feel like it suggests that they should be used, when, for the examples given, Action is a simpler choice. (A couple Unity forum users and I had a brief discussion five years ago which might give some insight.)
I can think of two cases where I would use a named delegate.
- Clarity for curried methods. Probably outside the scope of this guide.
- When the delegate takes at least one parameter. This should go in the style guide if the raywenderlich.com style is to use named parameters. I believe everyone should, but realize that's contentious.
delegate void DoMeaningfulThings(int count, string name);
static void FunctionWithSameSignature(int count, string name) {}
static void UnimportantFunction()
{
// Unimportant values.
var count = 11, name = "Jane Doremi";
DoMeaningfulThings doMeaningfulThings = (count, name) =>
{
// meaningful things are done here
};
// Why you'd use a custom delegate instead of an Action<int, string>:
// parameter names from the delegate are usable.
doMeaningfulThings(count: count, name: name);
// Compiles, but lacks clarity.
doMeaningfulThings(count, name);
// Compiles.
Action<int, string> action = FunctionWithSameSignature;
action(count, name);
// Neither compiles. Actions can't carry parameter names.
action(count: count, name: name);
action = doMeaningfulThings;
}If you don't want to take on named parameters at this point, I would suggest a "Prefer Action and Func" guideline, or an argument against using them in all cases.
Metadata
Metadata
Assignees
Labels
No labels