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:
  • April 25, 2012 -- SkyDrive, Windows 8 and Domain Account (0)
    I am running Windows 8 on my primary work machine now, which is domain joined. When I try and use the SkyDrive metro app (which ships with Windows 8), it does not like that fact I am domain joined and wants me to switch accounts, which is something I don’t want to do. This of course works great for those who are not domain joined and essentially are personal machines. For many of us who will be using this on ‘work’ machines, this seems like we will be ignored. Of course I can install the deskt...
  • March 6, 2012 -- Enabling Win 8 Metro on a Netbook (0)
    When you install Win 8 on a Netbook the screen resolution would be too low for Metro apps to run which is a bummer. One way to get around this and “fix” this is to update the registry (and you thought that was so XP!) :). Run Regedit and search for “display1_downscalingsupported” (without quotes). Find all occurrences of this entry and change its value from 0 to 1. Reboot when finished and you should have more options on your Screen Resolution choosing which will allow you to run Metro. :) ...
  • January 10, 2012 -- 15 Year of Microsoft at CES (0)
    ...

Tags:

Leave a Reply

*

Get Adobe Flash player