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?
I like to name it like a method so that when you invoke it, like this:
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
orDelegate
orLambda
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.There are precedents for using a noun in the Framework, e.g.
The noun is often an appropriate verb with an agentive suffix.
In your example I would use something like
loader
or possiblyvalueFactory
. I personally don't likecacheLoader
because presumably it's the caller rather than the delegate that does the work of inserting in the cache.I typically actually use the work delegate in my naming, to make it obvious that this parameter is receiving a delegate. For example, I'd potentially name the above:
I do this specifically to avoid confusion from the suggested naming in the question.
cacheLoader
sounds too much like an object, andloadResult
like an object/type (the result itself). I also don't personally like usingfunction
ormethod
, as a delegate is not actually a function, but rather a delegate - a type that references a function.