Question

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

Was it helpful?

Solution

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

OTHER TIPS

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

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top