打破Java泛型命名约定? [英] Breaking java generics naming convention?

查看:153
本文介绍了打破Java泛型命名约定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  / ** 
* @param T - 实体类型。
* @param C - 将返回实体容器的类型。
* /
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);


解决方案

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

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

Java的歧义问题似乎被夸大了。很少有类名都是大写字母。常量不会与类名称在同一个上下文中使用。

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



例如,比较:

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

到:

  public final class EventProducer< LISTENER extends IEventListener< EVENT>,EVENT> 
实现了IEventProducer< LISTENER,EVENT> {

尽管Sun / Oracle推荐单字符名称作为约定,但约定可以是改变。挑战这项公约的后果是微不足道的。如果你和你的团队喜欢为你的类型参数选择有意义的名字,那么我个人会喜欢的。



编辑(2015)



适用于Java的Google样式允许使用单字母名称和多字符以类结尾的类名称。


5.2.8类型变量名称



每个类型变量都被命名为一个的两种风格:


  • 一个大写字母,可选地后跟一个数字(如E,T,X,T2 )


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


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

解决方案

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

这篇关于打破Java泛型命名约定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆