ジェネリクスの命名規約

ジェネリクスクラス(やメソッド)を作る人は,そんな多くはないと思うが,命名規約ないと書けないっていう人いるよなぁと思って調べてみた。そしたら,Generics Tutorial(PDF)に書いてあったよ。

2 Definign Simple Generics
A note on naming conventions. We recommend that you use pithy (single character if possible) yet evocative names for formal type parameters. It's best to avoid lower case characters in those names, making it easy to distinguish formal type parameters from ordinary classes and interfaces. Many container types use E, for element, as in the examples above. We'll see some additional conventions in later examples.

  • 型変数は,簡潔で分かりやすい名前がイイ(できれば1文字がいいぞ)。
  • クラスやインターフェイスと区別しやすくするため,小文字は使わない方が良い。
  • コンテナの型変数は「E」がオススメ(Elementだから)。

5 Generic Methods
Finally, again let's take note of the naming convention used for the type parameters. We use "T" for type, whenever there isn't anything more specific about the type to distinguish it. This is often the case in generic methods. If there are multiple type parameters, we might use letters that neighbor "T" in the alphabet, such as "S". If a generic method appears inside a generic class, it's a good idea to avoid using the same names for the type parameters of the method and class, to avoid confusion. The same applies to nested generic classes.

  • なんも考えなしの場合,型変数は「T」を使う(Type parameterだから)。
  • 型変数が複数あるなら「T」のとなり「S」とか使うかな(前方向なんだ?)。
  • ジェネリクス・クラス中にジェネリクス・メソッドがあるなら,型変数は別の名前にしたほうがいい。

あと,Map<K, V>みたいに,Key, Valueって付けるのもある。