Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No recommendation given on when to use delegate declarations #4

Open
JessyCatterwaul opened this issue Dec 27, 2016 · 0 comments
Open
Assignees

Comments

@JessyCatterwaul
Copy link

JessyCatterwaul commented Dec 27, 2016

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.

  1. Clarity for curried methods. Probably outside the scope of this guide.
  2. 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.

@BlackDragonBE BlackDragonBE self-assigned this Sep 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants