当直观的名称是保留关键字时,如何简洁地命名方法/变量?

发布于 2024-10-04 17:03:51 字数 812 浏览 6 评论 0原文

我正在寻找方法(以及较小程度的变量)的良好命名约定。假设您在元编程或反射框架中有一些工厂类,并且这些方法与 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 技术交流群。

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

发布评论

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

评论(4

小猫一只 2024-10-11 17:03:51

方法名称应该是动词,而不是名词。

因此,如果您想要通用的东西,我只需使用 wrap()

命名方法

虽然方法名称可以是任何合法标识符,但代码约定限制方法名称。 按照惯例,方法名称应该是小写动词或多单词名称,以小写动词开头,后跟形容词、名词等。 在多单词名称中,第二个及后续单词均应大写。以下是一些示例:
运行
运行速度
获取背景
获取最终数据
比较
setX
isEmpty

来源:Java™ 教程 > 学习Java语言 > > 定义方法


以及关于重用方法名称。它有效,但需要一些小心。这是一个示例:

一个对象具有许多构造函数:

public class Wrapper{
    private final String type;
    public Wrapper(final byte inner){ type = "byte"; }
    public Wrapper(final short inner){ type = "short"; }
    public Wrapper(final int inner){ type = "int"; }
    public Wrapper(final char inner){ type = "char"; }
    public Wrapper(final Object inner){ type = "Object"; }
    public Wrapper(final float inner){ type = "float"; }
    public Wrapper(final double inner){ type = "double"; }
    public Wrapper(final boolean inner){ type = "boolean"; }
    public String getType(){ return type; }
}

一些工厂方法来获取此类对象:

public static Wrapper wrap(final byte inner){ return new Wrapper(inner); }
public static Wrapper wrap(final int inner){ return new Wrapper(inner); }
public static Wrapper wrap(final short inner){ return new Wrapper(inner); }
public static Wrapper wrap(final Object inner){ return new Wrapper(inner); }
public static Wrapper wrap(final boolean inner){ return new Wrapper(inner); }
public static Wrapper wrap(final char inner){ return new Wrapper(inner); }
public static Wrapper wrap(final float inner){ return new Wrapper(inner); }
public static Wrapper wrap(final double inner){ return new Wrapper(inner); }

测试代码:

public static void main(final String[] args){
    final byte byteValue = (byte) 0x12;
    final short shortValue = (short) 10;
    System.out.println(wrap(byteValue).getType());
    System.out.println(wrap(10).getType());
    System.out.println(wrap(shortValue).getType());
    System.out.println(wrap("").getType());
    System.out.println(wrap(true).getType());
    System.out.println(wrap('a').getType());
    System.out.println(wrap(13.1f).getType());
    System.out.println(wrap(13.1d).getType());
}

输出(如预期) :

字节
整数

对象
布尔值
字符
浮动

Method names should be verbs, not nouns.

So if you want something generic, I'd just go with wrap()

Naming a Method

Although a method name can be any legal identifier, code conventions restrict method names. By convention, method names should be a verb in lowercase or a multi-word name that begins with a verb in lowercase, followed by adjectives, nouns, etc. In multi-word names, the first letter of each of the second and following words should be capitalized. Here are some examples:
run
runFast
getBackground
getFinalData
compareTo
setX
isEmpty

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:

public class Wrapper{
    private final String type;
    public Wrapper(final byte inner){ type = "byte"; }
    public Wrapper(final short inner){ type = "short"; }
    public Wrapper(final int inner){ type = "int"; }
    public Wrapper(final char inner){ type = "char"; }
    public Wrapper(final Object inner){ type = "Object"; }
    public Wrapper(final float inner){ type = "float"; }
    public Wrapper(final double inner){ type = "double"; }
    public Wrapper(final boolean inner){ type = "boolean"; }
    public String getType(){ return type; }
}

Some factory methods to get such objects:

public static Wrapper wrap(final byte inner){ return new Wrapper(inner); }
public static Wrapper wrap(final int inner){ return new Wrapper(inner); }
public static Wrapper wrap(final short inner){ return new Wrapper(inner); }
public static Wrapper wrap(final Object inner){ return new Wrapper(inner); }
public static Wrapper wrap(final boolean inner){ return new Wrapper(inner); }
public static Wrapper wrap(final char inner){ return new Wrapper(inner); }
public static Wrapper wrap(final float inner){ return new Wrapper(inner); }
public static Wrapper wrap(final double inner){ return new Wrapper(inner); }

Test code:

public static void main(final String[] args){
    final byte byteValue = (byte) 0x12;
    final short shortValue = (short) 10;
    System.out.println(wrap(byteValue).getType());
    System.out.println(wrap(10).getType());
    System.out.println(wrap(shortValue).getType());
    System.out.println(wrap("").getType());
    System.out.println(wrap(true).getType());
    System.out.println(wrap('a').getType());
    System.out.println(wrap(13.1f).getType());
    System.out.println(wrap(13.1d).getType());
}

Output (as expected):

byte
int
short
Object
boolean
char
float
double

神经暖 2024-10-11 17:03:51

在这种情况下,我建议使用 wrapBytewrapInt,或者只是重载 create 方法。

In this case, I'd recommend wrapByte and wrapInt, or maybe just overloaded create methods.

一人独醉 2024-10-11 17:03:51

使用前缀“from”怎么样?

public class WrapperFactory {
    // create byte wrapper
    public MyWrapper fromByte(byte param) { ... }

    // create int wrapper
    public MyWrapper fromInt(int param) { ... }
}

这不遵循标准的 Java 命名约定,但它可能足够直观,可以被原谅。

How about using the prefix "from"?

public class WrapperFactory {
    // create byte wrapper
    public MyWrapper fromByte(byte param) { ... }

    // create int wrapper
    public MyWrapper fromInt(int param) { ... }
}

This does not follow the standard Java naming conventions, but it might be intuitive enough to be forgiven.

梦太阳 2024-10-11 17:03:51

在您的情况下,方法 newWrapper(byte...), newWrapper(int...) 会更好。或者create,或者其他用一个词描述该方法的作用的东西。

In your case methods newWrapper(byte...), newWrapper(int...) would be better. Or create, or somethings else which describes in one word what that method does.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文