Func的首选命名约定是什么?方法参数?
我承认这个问题是主观的,但我对社区的观点感兴趣。我有一个缓存类,它采用 Func
类型的缓存加载器函数,它使用该函数从数据库检索值并将其存储在缓存中。
public static class Cache
{
public TResult Get<TResult>(string cacheKey, Func<TResult> cacheLoader)
{
// Implementation
}
}
我的问题是:我应该如何命名函数参数?
- 我应该将它命名为一个对象,例如
cacheLoader
吗? - 我应该将其命名为方法,例如
loadResult
吗? - 我是否应该明确将其称为函数,例如
cacheLoadFunction
? (我不喜欢这个。)
我对这个特定函数参数的命名不太感兴趣,而对一般如何命名函数参数更感兴趣。 Stack Overflow 社区对此有何看法?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在框架中使用名词是有先例的,例如,
名词通常是带有施事后缀。
在您的示例中,我将使用诸如
loader
或可能的valueFactory
之类的东西。我个人不喜欢cacheLoader
因为可能是调用者而不是委托执行在缓存中插入的工作。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.我喜欢将其命名为方法,这样当您调用它时,就像这样:
它看起来像普通的方法调用,但大小写表明它是一个变量,因此两条信息都被传达。
您可以附加诸如
Method
或Delegate
或Lambda
之类的后缀,但这些后缀通常只会使其变得冗长而不会增加清晰度。这可能取决于具体情况和您的编码标准,当然还有您的偏好。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.我通常在命名中使用“委托”一词,以表明此参数正在接收委托。例如,我可能会命名上述内容:
我这样做是为了避免与问题中建议的命名产生混淆。
cacheLoader
听起来太像一个对象,而loadResult
听起来太像一个对象/类型(结果本身)。我个人也不喜欢使用function
或method
,因为委托实际上不是函数,而是委托 - 引用函数的类型。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:
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.