Why are T, K, V such a hot topic in Generics?

Is something as simple as do the T, K and V parameterised types make sense versus mnemonics in Generics a hot item to talk about? You bet, people cannot seem to get enough of it.The crux of the issue here is do T, K, V, etc in examples like List<T>, IDictionary<K,V> make sense versus something like List<Item>, IDictionary<Key, Value>. Apparently both sides make seemingly valid arguments and from what I gather, its also a hot discussion topic within Microsoft. There have been examples that point out that c# is supposed to be easy to understand and not messy as shown in this example. But in the long discussion here are the main points in favour of descriptive names:

  1. You can tell at a glance what each type parameter is used for.
  2. As with method parameters, so with type parameters: descriptive names are useful and helpful and mean you have to memorise less.
  3. To dispel the mistaken notion that a type parameter is simply a placeholder that doesn’t “mean anything”.
  4. Saying of List<T>: “it’s a list of some type T” may work, but other similar situations don’t work (e.g. Point<T>)
  5. Even List<T> isn’t as simple as people think it is; “T” is never really OK.
  6. T, V, K etc. may (just) be OK for native English speakers, but is not OK for those whose first language is not English.
  7. If you choose the first letter of each word as your type parameter name (e.g. K, V), what if two or more words start with the same letter ?
  8. Using descriptive names reduces the incidence of name clashing where two or more “T”s mix in the same lexical space.
  9. You don’t have to look up what “T” means virtually every single time you come back to use a generic class, and you don’t have to bear it mind while you’re using the class (and why should you ?).
  10. The case for using of single letters gets worse as more type parameters are added to generic classes in the future.
  11. Using “T” leads you to then use “U” (as in the Converter delegate). “T” may stand for Type, but “U” doesn’t stand for anything. So the single-letter convention is inconsistent as well as unhelpful.
  12. The brain isn’t overloaded having to remember what “T” and “U” stand for in more complex situations where generic types are mixed (e.g. in List<T>’s ConvertAll method).

I personally don’t particularly care, though given a choice, I prefer the <T,K,V> but that is probably because what I am use to seeing. We of course will know the outcome of this when Whidbey ships. 🙂

Published by

Amit Bahree

This blog is my personal blog and while it does reflect my experiences in my professional life, this is just my thoughts. Most of the entries are technical though sometimes they can vary from the wacky to even political – however that is quite rare. Quite often, I have been asked what’s up with the “gibberish” and the funny title of the blog? Some people even going the extra step to say that, this is a virus that infected their system (ahem) well. [:D] It actually is quite simple, and if you have still not figured out then check out this link – whats in a name?

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.