Func的首选命名约定是什么?方法参数?

发布于 2024-11-08 15:24:58 字数 592 浏览 2 评论 0原文

我承认这个问题是主观的,但我对社区的观点感兴趣。我有一个缓存类,它采用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

兔姬 2024-11-15 15:24:58

在框架中使用名词是有先例的,例如,

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);

名词通常是带有施事后缀。

在您的示例中,我将使用诸如 loader 或可能的 valueFactory 之类的东西。我个人不喜欢 cacheLoader 因为可能是调用者而不是委托执行在缓存中插入的工作。

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.

少女净妖师 2024-11-15 15:24:58

我喜欢将其命名为方法,这样当您调用它时,就像这样:

loadResult(result);

它看起来像普通的方法调用,但大小写表明它是一个变量,因此两条信息都被传达。

您可以附加诸如 MethodDelegateLambda 之类的后缀,但这些后缀通常只会使其变得冗长而不会增加清晰度。这可能取决于具体情况和您的编码标准,当然还有您的偏好。

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.

云仙小弟 2024-11-15 15:24:58

我通常在命名中使用“委托”一词,以表明此参数正在接收委托。例如,我可能会命名上述内容:

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

我这样做是为了避免与问题中建议的命名产生混淆。 cacheLoader 听起来太像一个对象,而 loadResult 听起来太像一个对象/类型(结果本身)。我个人也不喜欢使用 functionmethod,因为委托实际上不是函数,而是委托 - 引用函数的类型。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文