当直观的名称是保留关键字时,如何简洁地命名方法/变量?
我正在寻找方法(以及较小程度的变量)的良好命名约定。假设您在元编程或反射框架中有一些工厂类,并且这些方法与 java 的原始类型相关。
// dumb example
public class WrapperFactory {
// create byte wrapper
public MyWrapper byte(byte param) { ... }
// create int wrapper
public MyWrapper int(int param) { ... }
}
从可读性的角度来看,我希望有相当短的方法名称。与所示的示例不同,不同类型的方法签名可能相同,因此仅使用带有一堆重载的 create(...) 方法是不可能的。
从方法的上下文来看(它们毕竟在 WrapperFactory 中),很明显它们将生成一个 Wrapper。所以像 byteWrapper() (或者更冗长的 createByteWrapper())之类的东西似乎是完全多余的。
对于简短的方法名称有什么建议吗?
编辑:总体趋势似乎是具有重载的方法是最常见的,它适用于我的大多数工厂方法,但目前有四个具有相同签名的 createXXXWrapper(...) ,它们创建不同行为的包装器(但相同的通用类型)。 因此,为了使所有类型保持一致,我目前赞成 prefixXXX() 命名建议。什么前缀最好(wrap不是我最喜欢的之一,还有其他工厂创建不同的对象,这些对象在功能上不是包装器)。所以我想要通用前缀,例如 newX(...)、createX(...) 或 getX( ...)。
I am looking for a good naming convention for methods (and variables to a lesser degree). Let's say you have some factory class in a meta programming or reflection framework and the methods are related to java's primitive types.
// dumb example
public class WrapperFactory {
// create byte wrapper
public MyWrapper byte(byte param) { ... }
// create int wrapper
public MyWrapper int(int param) { ... }
}
From a readability point, I'd like to have reasonably short method names. Unlike the example shown, method signatures for different types may be the same, so having just a create(...)-method with a bunch of overloads is impossible.
From the context of the methods (they are in a WrapperFactory after all) its clear that they will produce a Wrapper. So anything like byteWrapper() (or even more verbose createByteWrapper()) seems to be completely redundant.
Any suggestions for short and concise method names?
Edit: The general tendency seems to be that a method with overloads is most common, it would work for most of my factory methods, but there are currently four createXXXWrapper(...) with identical signatures that create wrappers of different behavior (but the same general type).
So to be consistent for all types, I currently favor the prefixXXX() naming suggestion. What prefix would be best (wrap is not one of my favorites, there are other factories which create different objects that are not functionally not wrappers). So I'd like to have generic prefix like newX(...), createX(...) or getX(...).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
方法名称应该是动词,而不是名词。
因此,如果您想要通用的东西,我只需使用
wrap()
来源:Java™ 教程 > 学习Java语言 > 类 > 定义方法
以及关于重用方法名称。它有效,但需要一些小心。这是一个示例:
一个对象具有许多构造函数:
一些工厂方法来获取此类对象:
测试代码:
输出(如预期) :
Method names should be verbs, not nouns.
So if you want something generic, I'd just go with
wrap()
Source: The Java™ Tutorials > Learning the Java language > Classes > Defining Methods
And about reusing method names. It works, but it requires some care. Here's an example:
An object with many constructors:
Some factory methods to get such objects:
Test code:
Output (as expected):
在这种情况下,我建议使用
wrapByte
和wrapInt
,或者只是重载create
方法。In this case, I'd recommend
wrapByte
andwrapInt
, or maybe just overloadedcreate
methods.使用前缀“from”怎么样?
这不遵循标准的 Java 命名约定,但它可能足够直观,可以被原谅。
How about using the prefix "from"?
This does not follow the standard Java naming conventions, but it might be intuitive enough to be forgiven.
在您的情况下,方法
newWrapper(byte...), newWrapper(int...)
会更好。或者create
,或者其他用一个词描述该方法的作用的东西。In your case methods
newWrapper(byte...), newWrapper(int...)
would be better. Orcreate
, or somethings else which describes in one word what that method does.