ХабрПостыНовостиМегапосты?

Apply «capital letters jam» for generic type parameters

Typical type parameter?
Typical type parameter?

There are such types in Java - called "Generics".
And the point here is that there is a convention for naming generic type parameters WITH ONE CAPITAL LETTER.


Generics exist only at the compilation stage and only for the compiler.

So, the problem is that in practice, when you need several type parameters and especially with the same first letters of classes, it breaks down code readability:

  1. It is not clear by one capital letter what type is behind the letter.

  2. You can start "dancing" and add a number to the letter, BUT again - it will not make it readable for developers who will had to support your code.

Let's consider an example. Below is the interface with 4 type parameters. And 2 last types presents different classes, and both of classes are named like "...Param".

What if there were no 2 letters here in type param name??

/**
 * Flow of data mart processor.
 *
 * @param <FT> flow tables type
 * @param <TS> flow task steps type
 * @param <FP> flow param type
 * @param <TP> task param type
 */
public interface Flow<FT extends FlowTables, TS extends TaskStep<FT, FP, TP>, FP, TP> {

    /**
     * Plans a new task in the flow.
     *
     * @return new task
     */
    Task<FT, TS, FP, TP> nextTask();

    /**
     * Plans a new task in the flow.
     *
     * @param name task name
     * @return new task
     */
    Task<FT, TS, FP, TP> nextTask(String name);

    /**
     * Plans a dataset calculation for later using in flow.
     *
     * @param key   the key under which the dataset is stored
     * @param value dataset calculation function
     * @return this flow
     */
    Flow<FT, TS, FP, TP> putToStorage(String key, Function<Param<FT, FP>, Dataset<Row>> value);

    /**
     * Starts flow.
     *
     */
    void start();
}

We will definitely had to give some random one letter to identify needed type. Maybe for someone it is fine, but when:

  1. There are no explaining java docs;

  2. You are new to project;

  3. You have to work with several such "unclear" classes.

Then it becomes serious task to understand what type parameter expected by contract.

Capital letters jam - it is technique of naming type parameter by capital letters of class name.

And yes, in such approach we will definitely violate the known convention, but at least we will be able to understand from the code what kind of type it is, instead of observing the convention and suffering.


When developing classes with one type parameter - is is okay to give one letter to that parameter. But when there are several types and with the same class names - better apply "capital letters jam" technique to make it clear.

If you have what to say about the topic and want to give negative rating - please feel free to write your comments to understand your opinion. Otherwise it is not serious and it is not clear why you vote negatively.

And of course If you like this article, please vote UP - this will support me to write more such posts with real code examples!

@urusoff
30.05.2023 19:11 UTC
Первоисточник

Комментарии

@h0tkey
30.05.2023 14:59 UTC
0

But why not give them proper descriptive names when there's more than a couple of type parameters and their meaning is not completely obvious? The type parameter names never appear on the type's use sites, so giving them longer names does not put any burden on the users.

31.05.2023 08:39 UTC
0

It is up to you, if you like can give long names.

but from my perspective by capital letters better:

  1. Closer to convention. It states that should be one letter + digits. Here I suggest by capitals. For example if class name contain 3 words - will be 3 letters. It exactly defines the class is such case.

  2. Class name could be really long - how will you name type parameter in this case? It will match to class name? Long parameter names - not usual practice.

  3. Class name could differ only by 1 word from 3. For example - FlowParamBuilder and TaskParamBuilder. How you suggest to name type parameter in this case? I think that FPB and TPB will fit good here. If needed - even in IDEA you can find the class by type parameter capitals.