29

I admit that this question is subjective but I am interested in the view of the community. I have a cache class that takes a cache loader function of type Func<TResult>, which it uses to retrieve a value from the database and store it in cache.

public static class Cache
{
    public TResult Get<TResult>(string cacheKey, Func<TResult> cacheLoader) 
    {
        // Implementation
    }
}

My question is: How should I name the function parameter?

  • Should I name it like an object, e.g. cacheLoader?
  • Should I name it like a method, e.g. loadResult?
  • Should I explicitly refer to it as a function, e.g. cacheLoadFunction? (I don't like this.)

I'm less interested in what I should name this particular function parameter and more interested in how you name function parameters in general. What say ye, Stack Overflow community?

John Bledsoe
  • 17,142
  • 5
  • 42
  • 59

3 Answers3

34

There are precedents for using a noun in the Framework, e.g.

Enumerable.Average<TSource>(this IEnumerable<TSource> source, Func<TSource, decimal?> selector)

Enumerable.Count<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)

Enumerable.GroupBy<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector)

ConcurrentDictionary<TKey,TValue>.GetOrAdd(TKey key, 
            Func<TKey, TValue> valueFactory);

The noun is often an appropriate verb with an agentive suffix.

In your example I would use something like loader or possibly valueFactory. I personally don't like cacheLoader because presumably it's the caller rather than the delegate that does the work of inserting in the cache.

Joe
  • 122,218
  • 32
  • 205
  • 338
  • 1
    While I like @Reed's idea of naming it like a method, so that calling it looks more natural, framework precedent is the trump card in my opinion. – John Bledsoe May 19 '11 at 17:39
  • I'll speak up against the framework It was not written to be an example to follow. It was written by a large number of developers to meet corporate deadlines. It's often not readable/clean. – Vimes Jun 25 '23 at 00:23
8

I like to name it like a method so that when you invoke it, like this:

loadResult(result);

it looks like an ordinary method call but the casing indicates that it is a variable, so both pieces of information are conveyed.

You can append a suffix like Method or Delegate or Lambda but those often just make it verbose without adding clarity. It can depend on the situation and your coding standards, and of course your preferences.

Rick Sladkey
  • 33,988
  • 6
  • 71
  • 95
  • I sort of like this logic, except for one thing. The argument name should be chosen to make the purpose clear to the caller, not necessarily to the implementer of the method... – Reed Copsey May 19 '11 at 16:10
  • @Reed: You're right, that's a conflict of interest that argues in the favor of the caller. If a library exposes a public API the naming of parameters is sacred. That's an example of coding standards. – Rick Sladkey May 19 '11 at 16:15
  • Yeah, it seems like the parameter name should communicate to the caller "pass in a method that will do this". Whether the caller implements the method or finds one, he needs to know what it ought to do. – John Bledsoe May 19 '11 at 17:04
5

I typically actually use the word delegate in my naming, to make it obvious that this parameter is receiving a delegate. For example, I'd potentially name the above:

public static class Cache
{
    public TResult Get<TResult>(string cacheKey, Func<TResult> cacheLoadingDelegate) 
    {
        // Implementation
    }
}

I do this specifically to avoid confusion from the suggested naming in the question. cacheLoader sounds too much like an object, and loadResult like an object/type (the result itself). I also don't personally like using function or method, as a delegate is not actually a function, but rather a delegate - a type that references a function.

openshac
  • 4,966
  • 5
  • 46
  • 77
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • 2
    The answer [here](http://stackoverflow.com/a/25993838/661933) says MS is against that convention. – nawfal Jun 29 '15 at 20:57