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

Other similar posts you might be interested to check out:
  • April 8, 2010 -- Free (technical) Microsoft Courses (0)
    Channel 9 has a number of free technical courses on a number of emerging MS technologies covering a wide range such as Azure, Win7, Identity, SQL Server 2008 R2, Visual Studio 2010, .NET 4.0, Silverlight 4, MOSS 2010, Office 2010, etc. These cover a number of the features and essentially have everything to get a developer quite comfortable with the stack. In some areas they go a little deep as well. I think its an excellent way to come up to speed. Here is a quick example of the topics covere...
  • March 19, 2009 -- Win 7 and loading ISO’s (0)
    If you are on Win 7 (any build) you will find quickly that Daemon tools don’t work on that. Even if it was working, I would in any case recommend ditching Daemon tools and recommend using Virtual Clone Drive which not only is free but also works like a champ. I use it on Vista as well. ...
  • October 24, 2008 -- PDC’ing (0)
    I am going to be at PDC – anyone else I know going to be there? If you are reading this, and are going to be there then it is quite likely you know how to ping me and let me know you going. It also might be a good idea to check out the Devil’s field guide to the PDC. I will be blogging from there – but not during the sessions – need to ensure I am not distracted – especially as the Avalanche is coming! Expect to get some posts later in the evenings. ...

Tags:

Leave a Reply

Get Adobe Flash playerPlugin by wpburn.com wordpress themes