类型参数命名准则是什么?

发布于 2024-07-17 08:18:33 字数 356 浏览 8 评论 0原文

我注意到,并且在 Essential C# 3.0 书中看到,参数是通常定义为 TTEntity

例如:

public class Stack<T>
{


}

public class EntityCollection<TEntity>
{


}

如何决定使用哪个名称?

谢谢

I noticed, as well as saw in the Essential C# 3.0 book, that paramters are usually defined as T or TEntity

For example:

public class Stack<T>
{


}

or

public class EntityCollection<TEntity>
{


}

How do you decide which name to use?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(7

霓裳挽歌倾城醉 2024-07-24 08:18:33

我从 http://referencesource.microsoft.com/dotnet46.zip。 提取它并处理数据以从所有泛型类声明中提取泛型参数名称。

注意:我只从只有一个泛型参数的泛型类中提取了泛型参数名称。 因此,这没有考虑具有多个泛型参数的泛型类。

grep -nohrP "class \w+<T\w*>" | sed -e 's/.*\<//' -e 's/>//' | sort | uniq -cd | sort -bgr

结果:

361 T
 74 TChannel
 51 TKey
 33 TResult
 30 TSource
 28 T_Identifier
 18 TElement
 12 TEntity
 11 TInputOutput
  7 TItem
  6 TLeftKey
  6 TFilterData
  5 T_Query
  4 T_Tile
  4 TInput
  3 TValue
  3 TRow
  3 TOutput
  3 TEventArgs
  3 TDataReader
  3 T1
  2 TWrapper
  2 TVertex
  2 TValidationResult
  2 TSyndicationItem
  2 TSyndicationFeed
  2 TServiceType
  2 TServiceModelExtensionElement
  2 TResultType
  2 TMessage
  2 TLocationValue
  2 TInnerChannel
  2 TextElementType
  2 TException
  2 TEnum
  2 TDuplexChannel
  2 TDelegate
  2 TData
  2 TContract
  2 TConfigurationElement
  2 TBinder
  2 TAttribute

I fetched the .NET Framework 4.6 source code from http://referencesource.microsoft.com/dotnet46.zip. Extracted it and processed the data to extract the generic parameter name from all generic class declarations.

Note: I only extracted the generic parameter name from generic classes with only one generic parameter. So this does not take into consideration the generic classes with multiple generic parameters.

grep -nohrP "class \w+<T\w*>" | sed -e 's/.*\<//' -e 's/>//' | sort | uniq -cd | sort -bgr

Result:

361 T
 74 TChannel
 51 TKey
 33 TResult
 30 TSource
 28 T_Identifier
 18 TElement
 12 TEntity
 11 TInputOutput
  7 TItem
  6 TLeftKey
  6 TFilterData
  5 T_Query
  4 T_Tile
  4 TInput
  3 TValue
  3 TRow
  3 TOutput
  3 TEventArgs
  3 TDataReader
  3 T1
  2 TWrapper
  2 TVertex
  2 TValidationResult
  2 TSyndicationItem
  2 TSyndicationFeed
  2 TServiceType
  2 TServiceModelExtensionElement
  2 TResultType
  2 TMessage
  2 TLocationValue
  2 TInnerChannel
  2 TextElementType
  2 TException
  2 TEnum
  2 TDuplexChannel
  2 TDelegate
  2 TData
  2 TContract
  2 TConfigurationElement
  2 TBinder
  2 TAttribute
你丑哭了我 2024-07-24 08:18:33

这是我的一套规则

  • 如果有一个参数,我将其命名为 T
  • 如果有多个参数,我选择一个有意义的名称并以 T 为前缀。例如 TKey,TValue

半官方意见,值得一看关于该主题的框架设计指南:

Here is my set of rules

  • If there is one parameter, I name it T
  • If there is more than one parameter, I pick a meaningful name and prefix with T. For example TKey, TValue

For a semi-official opinion, it's worth looking at the framework design guidelines on the subject:

终遇你 2024-07-24 08:18:33

最后,这并不重要。 使用有意义的命名约定。

public class MyDictionary<T1, T2>
{ }

可能不如

public class MyDictionary<KeyType, ValueType>

(或 TKey、TValue,如果您愿意的话)有用。

如果我正在查看您的实现并且必须思考“好吧,这个‘T3’又是什么?” 那你就没有做好。

In the end, it doesn't REALLY matter. Use a naming convention that makes sense.

public class MyDictionary<T1, T2>
{ }

is probably not as useful as

public class MyDictionary<KeyType, ValueType>

(or TKey, TValue, if you prefer).

If I'm looking at your implementation and have to think "ok, what is this 'T3' thing again?" then you didn't do a good job.

国产ˉ祖宗 2024-07-24 08:18:33

到目前为止,我已经查看了所有答案,我认为它们都部分正确,但没有充分考虑所有情况。

我的观点是命名应该始终增加上下文价值。 因此,命名类型参数 TEntity 因为其类型约束是 IEntity 不一定会增加值,特别是因为 IntelliSense 无论如何都会指示其类型。 名称应反映类型参数的功能角色。 但这并不是说如果没有其他合适的描述性名称(并且单个字母不能不言自明),就不应该这样做。

对于 one 类型参数,上下文通常在类方面应该是明显的,因此 T 就可以了。 对于多个类型参数,请为每个参数添加描述性上下文,例如 TKeyTValue - 这是在多个类型参数为同一类型的情况下不应使用这些类型的另一个原因 (导致 TEntity1TEntity2 几乎没有增加任何价值)?

所以,我的答案与 JaredPar 和 Tomas Aschan 的答案类似,但有附加条件。

更新 2019 年 4 月 12 日:Microsoft 关于类型参数命名的指南 对于命名约定非常明确。 因此,我修改了我的答案以反映这一点,并删除了我说可以使用 TU 等或 T1、T2 等。根据指南,仅在存在单个类型参数(如果不言自明)的情况下才应使用单个字母。 始终对多个类型参数使用描述性名称。

I've looked at all the answers so far, and I think they're all partially right, but don't fully consider all situations.

My view is that naming should always add contextual value. So, naming a type parameter TEntity because its type constraint is IEntity would not necessarily add value, especially as IntelliSense would indicate its type anyway. The name should reflect the type parameter's functional role. But this is not to say it shouldn't be done if no other descriptive name is appropriate (and a single letter wouldn't be self-explanatory).

In the case of one type parameter, the context should normally be obvious in terms of the class, so T is fine. For multiple type parameters, add a descriptive context to each such as TKey and TValue - another reason the types shouldn't be used in case multiple type parameters are the same type (resulting in TEntity1 and TEntity2 which adds little value)?

So, my answer is similar to JaredPar and Tomas Aschan's answers, but with added qualification.

UPDATE 04/12/19: The Microsoft guidelines on type parameter naming are quite clear on the naming conventions. I've therefore modified my answer to reflect this, and removed the paragraph where I said it is acceptable to use T, U, etc. or T1, T2, etc. From the guidelines, single letters should only be used where there is a single type parameter (if self-explanatory). Always use descriptive names for multiple type parameters.

这个俗人 2024-07-24 08:18:33

我认为您应该考虑以下几点:

  1. 有多少个类型参数?
  2. 他们有什么限制吗?
  3. 它们用于用于某些特定用途吗?

一般来说,我总是在类型参数前加上 T 前缀,并使它们“具有足够的描述性”,这意味着它们需要具有描述性,以便我理解它们的作用和/或需要什么当我在六个月内查看代码时,他们。

几个例子,在我看来,类型参数的良好命名(此列表中的编号与上面的编号无关......):

  1. 一个参数,从类名中可以明显看出(或者从代码中的上下文中得知)为什么需要类型名称:

    列表 
      

    由于我们可以看到这是一个T类型的对象列表,并且对T没有具体的约束,因此不需要给出更具体的类型参数的名称。

  2. 几个参数,代表通用类/接口中的不同事物:

    IDictionary; 
      

    我们需要能够区分两个参数,因此我们不提供值的键类型,反之亦然。 因此,将参数命名为 KeyValue,并以 T 为前缀,似乎是合适的。
    我必须强调,这是比例如 IDictionaryIDictionary 更好的做法,因为在后两种情况下没有直观地知道哪个参数将用于什么的方法。

  3. 一个类型参数,但该类型必须满足某些要求或其他要求:

    存储库   其中 TEntity :类,IEntity 
      

    由于我们要求该类型实现另一个接口,因此向程序员提示这种情况是有意义的。 选择一些信息丰富的名称,帮助您了解类型的要求,并在其前面加上 T 前缀。

There are a couple of things I think you should take into account:

  1. How many type arguments are there?
  2. Are there any constraints on them?
  3. Are they used for something particular?

In general, I always prefix my type arguments with T, and make them "descriptive enough", meaning as descriptive as they need to be for me to understand what they do, and/or what is required of them, when I look at the code in six months.

A couple of examples of, in my opinion, good naming of type arguments (numbering in this list independent of numbering above...):

  1. One argument, and it's obvious from the class name (or otherwise from the context in the code) why the type name is needed:

    List<T>
    

    Since we can see that this is a list of objects of type T, and there are no specific constraints on T, there is no need to give a more specific name to the type argument.

  2. Several arguments, that represent different things in the generic class/interface:

    IDictionary<TKey, TValue>
    

    We need to be able to distinguish between the two arguments, so we don't supply the key type for value and vice versa. Thus, naming the arguments Key and Value, and prefixing with T, seems appropriate.
    I must emphasize that this is a much better practice than for example IDictionary<T1, T2> or IDictionary<T, U>, since in the latter two cases there is no way to intuitively know which argument will be used for what.

  3. One type argument, but the type must fulfill some requirements or other:

    Repository<TEntity> where TEntity : class, IEntity
    

    Since we require that the type implements another interface, it makes sense to give some heads-up to the programmer that this is the case. Choose some informative name that helps you see what is required of the type, and prefix it with T.

夜深人未静 2024-07-24 08:18:33

我真的不知道有任何可靠的泛型约定。

我见过的示例使用了其中一种 ff 变体:

  • T 表示单一类型参数,
  • K 表示第二个类型参数,U 表示第二个类型参数。第三个,例如 SomeGeneric
  • T 以及第二个和第三个类型参数的数字,例如 SomeGeneric;

我猜仿制药还很新,通用的行业惯例还没有建立。

I'm not aware of any solid conventions for generics really.

The samples that I have seen though use one of the ff variations:

  • T for single type parameters
  • K for a second type parameter, U for a third, e.g., SomeGeneric<T, K, U>
  • T and a number for a second and third type parameter, e.g., SomeGeneric<T1, T2, T3>

I guess generics are new enough that common industry conventions haven't been established yet.

青衫负雪 2024-07-24 08:18:33

来自微软的例子:

public interface IDictionary<TKey, TValue>

类型参数代表某物,所以如果你想要有可读的代码,这个“某物”应该从代码中显而易见(没有额外的注释)。 使用 T、V、U 等类型名称不一定很明显(但有时可能很明显)。

Example from Microsoft:

public interface IDictionary<TKey, TValue>

The type parameter represents something, so if you want to have readable code, this "something" should be obvious from the code (without extra comments). Using type names like T, V, U isn't necessarily obvious (but sometimes it can be).

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