Java的通用类型参数命名约定(具有多个字符)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2900881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 14:18:13  来源:igfitidea点击:

Generic type parameter naming convention for Java (with multiple chars)?

javagenericsnaming-conventions

提问by chaper29

In some interfaces I wrote I'd like to name generic type parameters with more than one character to make the code more readable.

在我编写的某些接口中,我想用多个字符命名泛型类型参数,以使代码更具可读性。

Something like....

就像是....

Map<Key,Value>

Instead of this...

而不是这个...

Map<K,V>

But when it comes to methods, the type-parameters look like java-classes which is also confusing.

但是当涉及到方法时,类型参数看起来像 java 类,这也令人困惑。

public void put(Key key, Value value)

This seems like Key and Value are classes. I found or thought of some notations, but nothing like a convention from Sun or a general best-practice.

这似乎 Key 和 Value 是类。我发现或想到了一些符号,但没有像 Sun 的约定或一般最佳实践那样。

Alternatives I guessed or found...

我猜测或发现的替代方案......

Map<KEY,VALUE>
Map<TKey,TValue>

回答by BalusC

Oracle recommends the following in Java Tutorials > Generics > Generic Types:

Oracle 在Java Tutorials > Generics > Generic Types 中推荐以下内容:

Type Parameter Naming Conventions

By convention, type parameter names are single, uppercase letters. This stands in sharp contrast to the variable namingconventions that you already know about, and with good reason: Without this convention, it would be difficult to tell the difference between a type variable and an ordinary class or interface name.

The most commonly used type parameter names are:

  • E - Element (used extensively by the Java Collections Framework)
  • K - Key
  • N - Number
  • T - Type
  • V - Value
  • S,U,V etc. - 2nd, 3rd, 4th types

You'll see these names used throughout the Java SE API and the rest of this lesson.

类型参数命名约定

按照惯例,类型参数名称是单个大写字母。这与您已经知道的变量命名约定形成鲜明对比,并且有充分的理由:如果没有这个约定,就很难区分类型变量和普通类或接口名称之间的区别。

最常用的类型参数名称是:

  • E - 元素(被 Java 集合框架广泛使用)
  • K - 键
  • N - 数字
  • T - 类型
  • V - 值
  • S、U、V 等 - 第 2、第 3、第 4 种类型

您将在 Java SE API 和本课程的其余部分中看到这些名称。

I'd stick to it to avoid the confusion among the developers and possible maintainers.

我会坚持这样做,以避免开发人员和可能的维护人员之间产生混淆。

回答by Tom Carchrae

You can use javadoc to at least give users of your generic class a clue. I still don't like it (I agree with @chaper29) but the docs help.

您可以使用 javadoc 至少为您的泛型类的用户提供线索。我仍然不喜欢它(我同意 @chaper29),但文档有所帮助。

eg,

例如,

/**
 * 
 * @param <R> - row
 * @param <C> - column
 * @param <E> - cell element
 */
public class GenericTable<R, C, E> {

}

The other thing I have been known to do is use my IDE to refactor a class breaking the convention. Then work on the code and refactor back to single letters. Makes it easier for me anyway if many type parameters are used.

我知道的另一件事是使用我的 IDE 来重构一个打破惯例的类。然后处理代码并重构回单个字母。如果使用了许多类型参数,无论如何都会让我更容易。

回答by Basil Bourque

Append Type

附加 Type

A good discussion can be found in the comments on the DZone page, Naming Conventions for Parameterized Types.

在 DZone 页面的评论中可以找到一个很好的讨论,参数化类型的命名约定

See the comment by Erwin Mueller. His suggestion makes perfect obvious sense to me: Append the word Type.

请参阅 Erwin Mueller 的评论。他的建议对我来说非常明显:附加这个词Type

Call an apple an apple, a car a car. The name in question is the name of a data type, right? (In OOP, a class essentially defines a new data type.) So call it a “Type”.

称苹果为苹果,称汽车为汽车。有问题的名称是数据类型的名称,对吗?(在OOP 中,一个类本质上定义了一种新的数据类型。)所以称之为“类型”。

Mueller's example, drawn from the original post's article:

穆勒的例子,摘自原帖的文章:

public interface ResourceAccessor < ResourceType , ArgumentType , ResultType > {
    public ResultType run ( ResourceType resource , ArgumentType argument );
}

Append T

附加 T

A duplicate Question provides this Answerby Andy Thomas. Note the excerpt from Google's style guide that suggests a multi-character type name should end in a single uppercase T.

一个重复的问题提供了安迪·托马斯的这个答案。请注意谷歌风格指南的摘录,建议多字符类型名称应以单个大写结尾T

回答by Andy Thomas

Yes, you can use multi-character names for type variables, as long as they are clearly distinguished from class names.

是的,您可以为类型变量使用多字符名称,只要它们与类名称明确区分即可。

This differs from the convention suggested by Sun with the introduction of generics in 2004. However:

这与 Sun 在 2004 年引入泛型时建议的约定不同。但是:

  • More than one convention exists.
  • Multi-character names are consistent with other Java styles, such as Google's style for Java.
  • The readable names are (surprise!) more readable.
  • 存在不止一种公约。
  • 多字符名称与其他 Java 样式一致,例如Google 的Java 样式。
  • 可读的名称(惊喜!)更具可读性。

Readability

可读性

In some interfaces I wrote I'd like to name generic type parameter with more than one character to make the code more readable.

在我编写的某些接口中,我想用多个字符命名泛型类型参数,以使代码更具可读性。

Readability is good.

可读性很好。

Compare:

相比:

    public final class EventProducer<L extends IEventListener<E>,E> 
            implements IEventProducer<L,E> {

to:

到:

    public final class EventProducer<LISTENER extends IEventListener<EVENT>,EVENT> 
           implements IEventProducer<LISTENER, EVENT> {

or, with Google's multi-character convention:

或者,使用 Google 的多字符约定:

    public final class EventProducer<ListenerT extends IEventListener<EventT>,EventT> 
           implements IEventProducer<ListenerT, EventT> {

    public final class EventProducer<ListenerT extends IEventListener<EventT>,EventT> 
           implements IEventProducer<ListenerT, EventT> {

Google style

谷歌风格

The Google Java Style Guideallows both single-letter names and multi-character class-like names ending in T.

谷歌的Java风格指南允许单字母名称和多字符类,像T.结尾的名称

5.2.8 Type variable names

Each type variable is named in one of two styles:

  • A single capital letter, optionally followed by a single numeral (such as E, T, X, T2)

  • A name in the form used for classes (see Section 5.2.2, Class names), followed by the capital letter T (examples: RequestT, FooBarT).

5.2.8 类型变量名

每个类型变量都以以下两种风格之一命名:

  • 单个大写字母,可选后跟单个数字(例如E, T, X, T2

  • 用于类的形式的名称(请参阅第 5.2.2 节,类名称),后跟大写字母 T(例如:RequestT, FooBarT)。

Issues

问题

“Without this convention, it would be difficult to tell the difference between a type variable and an ordinary class or interface name.” – from the Oracle tutorials, “Generic types”

“如果没有这个约定,就很难区分类型变量和普通的类或接口名称。” – 来自Oracle 教程,“通用类型”

Single-character names are not the only way to distinguish type parameters from class names, as we've seen above.

正如我们在上面看到的那样,单字符名称并不是区分类型参数和类名称的唯一方法。

Why not just document the type parameter meaning in the JavaDoc?

为什么不在 JavaDoc 中记录类型参数的含义?

It's true that the @paramJavaDoc elements can provide a longer description. But it's also true that the JavaDocs are not necessarily visible. (For example, there's a content assist in Eclipse that shows the type parameter names.)

@paramJavaDoc 元素确实可以提供更长的描述。但是,JavaDocs 不一定可见也是事实。(例如,Eclipse 中有一个显示类型参数名称的内容辅助。)

Multi-character type parameter names don't follow the Oracle convention!

多字符类型参数名称不遵循 Oracle 约定!

Many of Sun's original conventions are followed nearly universally in Java programming.

在 Java 编程中几乎普遍遵循 Sun 的许多原始约定。

However, this particular convention is not.

然而,这个特殊的约定不是。

The best choice among competing conventions is a matter of opinion. The consequences of choosing a convention other than Oracle's in this case are minor. You and your team can choose a convention that best meets your needs.

相互竞争的公约中的最佳选择是一个意见问题。在这种情况下,选择 Oracle 以外的约定的后果是轻微的。您和您的团队可以选择最能满足您需求的约定。

回答by Vojtech Ruzicka

The reason why the official naming conventionreccommends using single letter is the following:

为什么原因正式命名约定reccommends使用单字母如下:

Without this convention, it would be difficult to tell the difference between a type variable and an ordinary class or interface name.

如果没有这个约定,就很难区分类型变量和普通的类或接口名称。

I think with modern IDEs that reason is no longer valid as eg. IntelliJ Idea shows generic type parameters with different colors than regular classes.

我认为对于现代 IDE,这个原因不再有效,例如。IntelliJ Idea 使用与常规类不同的颜色显示泛型类型参数。

Code with generic type as displayed in IntelliJ Idea 2016.1Code with generic type as displayed in IntelliJ Idea 2016.1

IntelliJ Idea 2016.1 中显示的具有泛型类型的代码IntelliJ Idea 2016.1 中显示的具有泛型类型的代码

Because of that distinction I use longer descriptive namesfor my generic types, with same convention as regular types. I avoid adding prefixes and suffixes such as T or Type as I consider them unnecessary noise and no longer needed to visually distinguish generic types.

由于这种区别,为我的泛型类型使用了更长的描述性名称,与常规类型具有相同的约定。我避免添加前缀和后缀,如 T 或 Type,因为我认为它们是不必要的噪音,不再需要在视觉上区分泛型类型。

Note: As I am not a user of Eclipse or Netbeans, I do not know whether they offers a similliar feature.

注意:由于我不是 Eclipse 或 Netbeans 的用户,我不知道它们是否提供类似的功能。