打破java泛型命名约定?

发布于 2024-11-10 06:03:36 字数 583 浏览 4 评论 0原文

我有一个接口,其声明如下:

/**
* @param T - the type of entity.
* @param C - the type of entity container will be returned.
*/
public interface FindByNamedQuery<T extends Serializable, C extends Collection<T>> extends Command {
    C executeNamedQuery(String namedQuery);
}

我想知道我是否可以(应该)打破Java命名约定来做到这一点:

public interface FindByNamedQuery<ENTITY_TYPE extends Serializable, RETURNED_CONTAINER extends Collection<ENTITY_TYPE>> extends Command {
    RETURNED_CONTAINER executeNamedQuery(String namedQuery);
}

I have an interface whose declaration is as follows:

/**
* @param T - the type of entity.
* @param C - the type of entity container will be returned.
*/
public interface FindByNamedQuery<T extends Serializable, C extends Collection<T>> extends Command {
    C executeNamedQuery(String namedQuery);
}

I wonder if I can (should) break the Java naming convention to do this:

public interface FindByNamedQuery<ENTITY_TYPE extends Serializable, RETURNED_CONTAINER extends Collection<ENTITY_TYPE>> extends Command {
    RETURNED_CONTAINER executeNamedQuery(String namedQuery);
}

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

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

发布评论

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

评论(7

罪#恶を代价 2024-11-17 06:03:36

自 20 世纪 90 年代中期以来,我开始不同意单字符约定。

我发现可读的名称更具可读性。这有助于理解泛型类型的实现和接口。

对于 Java 来说,歧义问题似乎被夸大了。很少有类名是全大写的。常量不与类名在同一上下文中使用。

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

,将 :

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

与:

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

进行比较,尽管 Sun/Oracle 已推荐使用单字符名称作为约定,但约定可以更改。挑战这一惯例的后果很小。如果您和您的团队更喜欢为类型参数指定有意义的名称,我个人会选择它。

编辑 (2015)

Google style for Java 允许单字母名称和多字符类- 类似以 T 结尾的名字。

5.2.8 类型变量名称

每个类型变量都以两种样式之一命名:

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

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

I am beginning to disagree with the single-character convention, after using it since the mid-1990s.

I find the readable names more readable. This is helpful in understanding both the implementation and interface of generic types.

The ambiguity problem seems overstated for Java. Few class names are all-uppercase. Constants are not used in the same context as class names.

It's true that the @param JavaDoc 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.)

For example, 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> {

Although the single-character names have been recommended as a convention by Sun/Oracle, conventions can be changed. The consequences of challenging this convention are minor. If you and your team prefer meaningful names for your type parameters, I personally would go for it.

Edit (2015)

Google style for Java allows both single-letter names and multi-character class-like names ending in 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).

卖梦商人 2024-11-17 06:03:36

我想知道我是否可以(应该)打破 java 命名约定来做到这一点:

不,应该避免这种情况,因为它更容易将类型参数与常量和其他参数混淆标识符。

以下是泛型官方跟踪的引用:

类型参数命名约定

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

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

  • E - 元素(由 Java 集合框架广泛使用)
  • K - 键
  • N - 数字
  • T - 类型
  • V - 值
  • SUV 等 - 第二、第三、第四类型

您将在整个 Java SE API 以及本教程的其余部分中看到这些名称。

I wonder if I can (should) break the java naming convention to do this:

No, this should be avoided as it becomes easier to confuse the type parameters with constants and other identifiers.

Here's a quote from the official trail on generics:

Type Parameter Naming Conventions

By convention, type parameter names are single, uppercase letters. This stands in sharp contrast to the variable naming conventions 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 tutorial.

帅的被狗咬 2024-11-17 06:03:36

在 C# 中使用 TDescription 非常常见。它保留了 T 名称,但同时也是描述性的,如下所示:

public interface FindByNamedQuery<
    TEntityType extends Serialiazble, 
    TReturnedContainer extends Collections<TEntityType>> extends Command 
{     
    TReturnedContainer executeNamedQuery(String namedQuery); 
} 

正如其他人所说的 ALL_CAPS 几乎总是表示一个常量。

IMO,“很难区分类型变量和普通类或接口名称之间的区别。”此处不适用,因为 T 前缀很容易将其标识为类型变量。

同样,这是 C#,但请参阅 MSDN:泛型命名约定

在所有其他情况下,官方
Microsoft 通用指南
命名约定是:

  • 使用描述性名称命名泛型类型参数,除非单个
    字母名称完全是自我
    解释性和描述性名称
    不会增加价值。

    公共接口ISessionChannel 
    {...}
    
    公共委托 TOutput Converter(TInput from);
    
  • 考虑在参数名称中指示对类型参数施加的约束。例如,限制为 ISession 的参数可以称为 TSession。

Using TDescription is pretty common in C#. It maintains the T name but is also descriptive at the same time, like so:

public interface FindByNamedQuery<
    TEntityType extends Serialiazble, 
    TReturnedContainer extends Collections<TEntityType>> extends Command 
{     
    TReturnedContainer executeNamedQuery(String namedQuery); 
} 

As others have said ALL_CAPS almost always indicates a constant.

IMO, "it would be difficult to tell the difference between a type variable and an ordinary class or interface name." does not apply here, because the T prefix easily identifies it as a type variable.

Again, this is C# but see MSDN: Naming Conventions For Generics

In all other cases, the official
Microsoft guidelines for generic
naming conventions are:

  • Name generic type parameters with descriptive names, unless a single
    letter name is completely self
    explanatory and a descriptive name
    would not add value.

    public interface ISessionChannel<TSession> 
    {...}
    
    public delegate TOutput Converter<TInput,TOutput>(TInput from);
    
  • Consider indicating constraints placed on a type parameter in the name of parameter. For example, a parameter constrained to ISession may be called TSession.
漫漫岁月 2024-11-17 06:03:36

编译器可能不会抱怨,但你的队友可能不喜欢你在他们需要类型参数的地方使用看起来像常量的东西。

The compiler might not complain, but your teammates might not appreciate you using what looks to be a constant in a place where they're expecting a type parameter.

以歌曲疗慰 2024-11-17 06:03:36

我认为这是许多使用仿制药的人的抱怨。我不太同意 Sun 的说法,即如果您使用完整的名称,那么它将与现有的类名称或其他名称混淆。在这种情况下,我们可以像这样以美元开头占位符名称:

public class HashMap<$Key,$Value> implements Map<$Key,$Value>{}

没有一个头脑清醒的人会以美元符号开头命名类。美元符号也用于表示许多模板语言的占位符,例如 Velocity、Struts、Spring 等。我认为这是正确的方法。

如果有人感兴趣的话,我在我的博客文章中获得了有关此内容的更多详细信息以及不必使用单个字母符号背后的原因。

http://readsethu. wordpress.com/2012/05/23/a-generic-class-and-why-is-it-confusing/

I think this is the gripe of many people using generics. I don't quite agree with Sun's statement that if you use a full fledged name then it will confuse with an existing class name or something else. In that case we can start the placeholder name with a dollar like this:

public class HashMap<$Key,$Value> implements Map<$Key,$Value>{}

No one in their sane mind names a class starting with a dollar sign. And a dollar sign also is used to denote a placeholder many templating languages velocity, struts, spring, etc. I think this is the way to go.

I have got more details about this and the reasoning behind not having to use a single letter notation in my blog post if anyone is interested.

http://readsethu.wordpress.com/2012/05/23/a-generic-class-and-why-is-it-confusing/

望她远 2024-11-17 06:03:36

就像 Allen 之前一样,我的建议更多来自 C#(我使用从 5 个月以来广泛使用)比 Java(我使用过它,但它从来没有走得太远......),但我发现 Java 和 C# 代码在精神上非常相似(也就是说,当比较时,比如说, C++)

无论如何,当在简单类型上使用 C#/Java 泛型(或 C++ 模板)时,我通常使用 T:

// C++
template<typename T>
class MyClass { /* ... */ } ;
// C#
public MyClass<T> { /* etc. */ }
// Java
public MyClass<T> { /* etc. */ }

通常,类型 T 与类一起使用,因此无需对其进行更多描述。

但当真正描述类型可以增加代码清晰度时,我就会这样做。

或者,当我在同一泛型/模板声明中有两种或多种类型时,它有助于区分两种类型。例如(C# 中的现实生活示例):

// C++
template<typename T_Data, typename T_Underlying>
class MyClass { /* ... */ } ;
// C#
public MyClass<T_Data, T_Underlying> { /* etc. */ }
// Java
public MyClass<T_Data, T_Underlying> { /* etc. */ }

这样,很容易区分代码中的两个类型名称,其中 TU 是很好的。 .. 有点匿名:对于那些使用 Visual C++ 的人来说,在 Dinkumware 的 STL 代码中进行调试(充满 T_U 和其他单字母类型名称)可能会非常令人沮丧。 ..我想也是一样适用于 C# 或 Java 代码。

您会注意到,在每种情况下(C++、Java 或 C#),我在类型命名中都没有遵循约定:原因是有时,您只需尝试其他方法而不是随波逐流,即使在到最后,你会发现你错了。

在目前的情况下,违反命名约定并不重要(Java 中存在比这种小犯罪更严重的问题),最后,您将亲自了解为什么它是错误的,而不是引用旧文档。

如果你最终发现你是对的,那么......

Like Allen before, my advice comes more from C# (which I use extensively since 5 months) than Java (which I played with, but it never went very far...), but I find Java and C# code quite similar in spirit (that is, when compared by, say, C++)

Anyway, when using a C#/Java generic (or a C++ template) on a simple type, I usually use T:

// C++
template<typename T>
class MyClass { /* ... */ } ;
// C#
public MyClass<T> { /* etc. */ }
// Java
public MyClass<T> { /* etc. */ }

Usually, the type T goes with the class, so there is no need to describe it more.

But when really describing the type adds to the code clarity, I do it.

Or when I have two or more types in the same generic/template declaration, it helps to make the difference between two types. For example (real life example in C#) :

// C++
template<typename T_Data, typename T_Underlying>
class MyClass { /* ... */ } ;
// C#
public MyClass<T_Data, T_Underlying> { /* etc. */ }
// Java
public MyClass<T_Data, T_Underlying> { /* etc. */ }

This way, it is easy to make the difference between the two typenames in the code, where T and U are, well... kinda anonymous: For those using Visual C++, going in debug inside Dinkumware's STL code, full of T, _U, and other mono-letter typenames can be quite frustrating... I guess the same goes for C# or Java code.

You will note that in each case (C++, Java or C#), I don't follow the convention somewhere in my type namings: The reason is that sometimes, you just have to try something else instead of following the herd, even if in the end, you'll find you're wrong.

In the current case, the violation of naming convention is not critical (there are worst problems in Java than this petty crime), and at the very last, you'll learn personally and exactly WHY it is wrong, instead of quoting old documents.

And if you find in the end you're right, well...

我ぃ本無心為│何有愛 2024-11-17 06:03:36

我会以类似于类型的方式命名类型变量,采用驼峰式大小写,但以“_”为前缀。

public interface FindByNamedQuery
    <_EntityType extends Serialiazble, 
     _ReturnedContainer extends Collections<_EntityType>> 
    extends Command 
{
    _ReturnedContainer executeNamedQuery(String namedQuery);
}

I would name type variables similar to types, in camel casing, but prefixed with "_".

public interface FindByNamedQuery
    <_EntityType extends Serialiazble, 
     _ReturnedContainer extends Collections<_EntityType>> 
    extends Command 
{
    _ReturnedContainer executeNamedQuery(String namedQuery);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文