Sep 29 2004

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

Category: .ms.futureAmit Bahree @ 3:22 am

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

Share
Similar posts to check out:
  • January 10, 2012 -- 15 Year of Microsoft at CES (0)
    ...
  • November 1, 2011 -- Productivity Future Vision (0)
    Microsoft's productivity future - how cool is this? ...
  • June 17, 2011 -- Kinect SDK (0)
    Microsoft recently release the Kinect SDK which allows you to implement a Natural User Interface and program against it! There is a lot of interest  around including claims on how Robotics will change to how you can integrate a light sensor. You can use Visual Studio (C++, C# and VB.NET supported) and get quite interesting results. Here are a series of links below which will help you get started. Download and install the Kinect SDK Download and install Quickstart Samples and Slide...

Tags:

Leave a Reply

*

Get Adobe Flash playerPlugin by wpburn.com wordpress themes