views:

169

answers:

2

In some interfaces i wrote I'd like to name generic type parameter with more than one character to make the code more readable.

Something like....

Map<Key,Value>

Instead of this...

Map<K,V>

But when it comes to methods, the type-parameters look like java-classes which is also confusing.

public void put(Key key, Value value)

This seems like Key and Value are classes. I found or thought of some notations, but nothing like a convention from sun or a general best-practice.

Alternatives i guesed of or found...

Map<KEY,VALUE>
Map<TKey,TValue>
A: 

I think there is no best practice for these situations that is generally agreed upon, but I suggest you use what is the most clear to you. I often find myself using descriptive names like 'Key'.

TjeerdB
+8  A: 

Sun recommends the following (check the bottom of the linked page):

Type Parameter Naming Conventions

By convention, type parameter names are single, uppercase letters. This stands in sharp contrast to the variable naming conventions that you already know about, and with good reason: Without this convention, it would be difficult to tell the difference between a type variable and an ordinary class or interface name.

The most commonly used type parameter names are:

  • E - Element (used extensively by the Java Collections Framework)
  • K - Key
  • N - Number
  • T - Type
  • V - Value
  • S,U,V etc. - 2nd, 3rd, 4th types

You'll see these names used throughout the Java SE API and the rest of this tutorial.

I'd stick to it to avoid the confusion among the developers and possible maintainers.

BalusC