打破java泛型命名约定?
我有一个接口,其声明如下:
/**
* @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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
自 20 世纪 90 年代中期以来,我开始不同意单字符约定。
我发现可读的名称更具可读性。这有助于理解泛型类型的实现和接口。
对于 Java 来说,歧义问题似乎被夸大了。很少有类名是全大写的。常量不与类名在同一上下文中使用。
@param JavaDoc 元素确实可以提供更长的描述。但 JavaDoc 不一定是可见的,这也是事实。 (例如,Eclipse 中有一个显示类型参数名称的内容辅助。)例如
,将 :
与:
进行比较,尽管 Sun/Oracle 已推荐使用单字符名称作为约定,但约定可以更改。挑战这一惯例的后果很小。如果您和您的团队更喜欢为类型参数指定有意义的名称,我个人会选择它。
编辑 (2015)
Google style for Java 允许单字母名称和多字符类- 类似以 T 结尾的名字。
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 :
to:
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.
不,应该避免这种情况,因为它更容易将类型参数与常量和其他参数混淆标识符。
以下是泛型官方跟踪的引用:
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:
在 C# 中使用 TDescription 非常常见。它保留了 T 名称,但同时也是描述性的,如下所示:
正如其他人所说的
ALL_CAPS
几乎总是表示一个常量。IMO,“很难区分类型变量和普通类或接口名称之间的区别。”此处不适用,因为 T 前缀很容易将其标识为类型变量。
同样,这是 C#,但请参阅 MSDN:泛型命名约定
Using TDescription is pretty common in C#. It maintains the T name but is also descriptive at the same time, like so:
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
编译器可能不会抱怨,但你的队友可能不喜欢你在他们需要类型参数的地方使用看起来像常量的东西。
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.
我认为这是许多使用仿制药的人的抱怨。我不太同意 Sun 的说法,即如果您使用完整的名称,那么它将与现有的类名称或其他名称混淆。在这种情况下,我们可以像这样以美元开头占位符名称:
没有一个头脑清醒的人会以美元符号开头命名类。美元符号也用于表示许多模板语言的占位符,例如 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:
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/
就像 Allen 之前一样,我的建议更多来自 C#(我使用从 5 个月以来广泛使用)比 Java(我使用过它,但它从来没有走得太远......),但我发现 Java 和 C# 代码在精神上非常相似(也就是说,当比较时,比如说, C++)
无论如何,当在简单类型上使用 C#/Java 泛型(或 C++ 模板)时,我通常使用 T:
通常,类型 T 与类一起使用,因此无需对其进行更多描述。
但当真正描述类型可以增加代码清晰度时,我就会这样做。
或者,当我在同一泛型/模板声明中有两种或多种类型时,它有助于区分两种类型。例如(C# 中的现实生活示例):
这样,很容易区分代码中的两个类型名称,其中
T
和U
是很好的。 .. 有点匿名:对于那些使用 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:
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#) :
This way, it is easy to make the difference between the two typenames in the code, where
T
andU
are, well... kinda anonymous: For those using Visual C++, going in debug inside Dinkumware's STL code, full ofT
,_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...
我会以类似于类型的方式命名类型变量,采用驼峰式大小写,但以“_”为前缀。
I would name type variables similar to types, in camel casing, but prefixed with "_".