Naming generic type parameters: Dictionary<K,V> or Dictionary<KeyType, ValueType>http://blogs.msdn.com/b/brada/archive/2004/07/04/172982.aspxThere is a good debate going on over in the Whidbey product feedback site . I encourage you to go and chime in one way or another&#8230; The team is watching the thread there and looking for feedback. On the surface this looks like a simple issue (moreen-USTelligent Evolution Platform Developer Build (Build: 5.6.50428.7875)re: Naming generic type parameters: Dictionary<K,V> or Dictionary<KeyType, ValueType>http://blogs.msdn.com/b/brada/archive/2004/07/04/172982.aspx#191234Thu, 22 Jul 2004 15:34:00 GMT91d46819-8472-40ad-a661-2c78acb4018c:191234陈奕迅why?<div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=191234" width="1" height="1">re: Naming generic type parameters: Dictionary<K,V> or Dictionary<KeyType, ValueType>http://blogs.msdn.com/b/brada/archive/2004/07/04/172982.aspx#180121Sun, 11 Jul 2004 18:29:00 GMT91d46819-8472-40ad-a661-2c78acb4018c:180121Ming Chen(VSData)I think I am becomming a lazy person since I get hooked on the intellisense. I typically rely intellisense pop up the class method to look for the method and parameters instead of look it up from the menu/help (I really hate MSDN unless I have to). Hey I am not the only one---
<br><a target="_new" href="http://www.joelonsoftware.com/uibook/chapters/fog0000000062.htmlIf">http://www.joelonsoftware.com/uibook/chapters/fog0000000062.htmlIf</a>
<br>
<br>From that perspective, well, I learn my lesson of put the *long &amp; descriptive* name in the type defination, xml comment on the beginning every public function that will benefit other teammate whoever to use it and vise versa.
<br>
<br>That is, if I have to choose to purchase a external templateize colletion from two companies one implement Dictionary&lt;Key,Value&gt;, the other implements Dictionary&lt;K, V&gt;, I will pick Dictionary&lt;Key,Value&gt; anyday.
<br>
<br>Yes, i am VS intelliense addict &amp; I don't *want to* read manual unless absolute necessary.
<br>
<br>-Ming<div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=180121" width="1" height="1">re: Naming generic type parameters: Dictionary<K,V> or Dictionary<KeyType, ValueType>http://blogs.msdn.com/b/brada/archive/2004/07/04/172982.aspx#179826Sun, 11 Jul 2004 06:22:00 GMT91d46819-8472-40ad-a661-2c78acb4018c:179826Keith PatrickKey/Value (or whatever the class is supposed to be). It's like getting rid of Hungarian notation: let the IDE tell you what something is to the compiler/runtime, not the name, so why use abbreviations to denote a template/generic? Also, the concept doesn't really extend out beyond traditional templates. Sure, there's K/V, and it's well understood, but what woudl the standard be (and for that matter, the litmus) for when something is well-known enough to abbreviate?<div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=179826" width="1" height="1">re: Naming generic type parameters: Dictionary<K,V> or Dictionary<KeyType, ValueType>http://blogs.msdn.com/b/brada/archive/2004/07/04/172982.aspx#179797Sun, 11 Jul 2004 05:24:00 GMT91d46819-8472-40ad-a661-2c78acb4018c:179797Cyrus NajmabadiI've flipped my position on this after a conversation with SteveJS. I'll post something on it later :-)<div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=179797" width="1" height="1">re: Naming generic type parameters: Dictionary<K,V> or Dictionary<KeyType, ValueType>http://blogs.msdn.com/b/brada/archive/2004/07/04/172982.aspx#179561Sat, 10 Jul 2004 19:20:00 GMT91d46819-8472-40ad-a661-2c78acb4018c:179561KlausSpell the type parameters out, please. So my vote is for the Dictionary&lt;KeyType, ValueType&gt;. I would read the use of &lt;&gt; and from that know that it should not be used as Dictionary&lt;2,26&gt;.<div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=179561" width="1" height="1">re: Naming generic type parameters: Dictionary<K,V> or Dictionary<KeyType, ValueType>http://blogs.msdn.com/b/brada/archive/2004/07/04/172982.aspx#175684Wed, 07 Jul 2004 20:23:00 GMT91d46819-8472-40ad-a661-2c78acb4018c:175684damien mortonPhilip - your example has me recoiling - youre right - its ugly and distracting.
<br>
<br>How about this then:
<br>
<br>@Value Process(@Other key)
<br>{
<br> @Other newKey = key.Next();
<br> foreach (@Value value in values[key])
<br> {
<br> value = key.Get&lt;@Value&gt;
<br> }
<br>...
<br>
<br>Perhaps this distinguishes type paramaters, without beeing to visually jarring. (shades of perl, but... type paramaters are special and you dont want to go an confuse them with actual types).<div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=175684" width="1" height="1">re: Naming generic type parameters: Dictionary<K,V> or Dictionary<KeyType, ValueType>http://blogs.msdn.com/b/brada/archive/2004/07/04/172982.aspx#175623Wed, 07 Jul 2004 19:26:00 GMT91d46819-8472-40ad-a661-2c78acb4018c:175623Philip RieckDamien - the only problem with VALUE is that many other languages (C# included, to some extent), use the all caps pattern to signify a constant, #define, or macro -- In fact, it's normally reserved for a value fixed at compile time.
<br>
<br>Also, I personally would be distracted by code such as this (completely nonsensical:)
<br>VALUE Process(OTHER key)
<br>{
<br> OTHER newKey = key.Next();
<br> foreach(VALUE value in values[key])
<br> {
<br> value = key.Get&lt;VALUE&gt;
<br>...
<br>
<br>
<br>-- you can see that the all-caps type specifiers can be littered in there quite a bit, and would distract a reader. The reader would be much more likely to care what the code does than to have the placement of type specifiers scream out at them<div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=175623" width="1" height="1">re: Naming generic type parameters: Dictionary<K,V> or Dictionary<KeyType, ValueType>http://blogs.msdn.com/b/brada/archive/2004/07/04/172982.aspx#175548Wed, 07 Jul 2004 18:12:00 GMT91d46819-8472-40ad-a661-2c78acb4018c:175548damien mortonCyrus sais:
<br>public interface IDictionary&lt;KeyType,ValueType&gt;
<br>{
<br>ValueType GetAt(KeyType key);
<br>}
<br>
<br>Why is KeyType necessary? We know it's a &quot;key&quot; because the name of the variable is &quot;key&quot;.
<br>
<br>Damien sais:
<br>
<br>Ahh - but what about ValueType in that expression - we dont know what that is, until we look at the name of the generic type param.
<br>
<br>how about this pattern:
<br>
<br>public interface IDictionary&lt;KEY,VALUE&gt;
<br>{
<br>VALUE GetAt(KEY key);
<br>}
<br>
<br>I know evyerone will recoil at the use of all-caps, but its shorter and clearer than pre-pending a letter or appending the word &quot;Type&quot;. Its a currently unused pattern, and its compatable with single-upper-case-letters for simple type params.<div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=175548" width="1" height="1">re: Naming generic type parameters: Dictionary<K,V> or Dictionary<KeyType, ValueType>http://blogs.msdn.com/b/brada/archive/2004/07/04/172982.aspx#175074Wed, 07 Jul 2004 08:51:00 GMT91d46819-8472-40ad-a661-2c78acb4018c:175074Cyrus NajmabadiI'm looking forward to prepending classes with C, delegates with D, and enums with E. There's a certain beauty in the succession of those letts, C/D/E. Now we just need to something to start with F...<div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=175074" width="1" height="1">re: Naming generic type parameters: Dictionary<K,V> or Dictionary<KeyType, ValueType>http://blogs.msdn.com/b/brada/archive/2004/07/04/172982.aspx#175072Wed, 07 Jul 2004 08:50:00 GMT91d46819-8472-40ad-a661-2c78acb4018c:175072Cyrus NajmabadiJulien: Dont' get me started on how much I hate that about interfaces :-)<div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=175072" width="1" height="1">re: Naming generic type parameters: Dictionary<K,V> or Dictionary<KeyType, ValueType>http://blogs.msdn.com/b/brada/archive/2004/07/04/172982.aspx#175069Wed, 07 Jul 2004 08:46:00 GMT91d46819-8472-40ad-a661-2c78acb4018c:175069Cyrus NajmabadiDamien: &quot;Dictionary&lt;K,V&gt; is a case in point. Its clear what K and V mean, and consise to read and write. &quot;
<br>
<br>Not to me it isn't. Why K instead of A? If it were Key or KeyType it still wouldn't help out at all.
<br>
<br>These are types we are talking about and in the end they're substituted by the user for somthing else.
<br>
<br>take a fictional method example:
<br>
<br>public interface IDictionary&lt;KeyType,ValueType&gt;
<br>{
<br> ValueType GetAt(KeyType key);
<br>}
<br>
<br>Why is KeyType necessary? We know it's a &quot;key&quot; because the name of the variable is &quot;key&quot;. So KeyType adds no extra information. When you end up using this you'll have:
<br>
<br>IDictionary&lt;int,string&gt; and you'll end up seeing:
<br>
<br>string GetAt(int key);
<br>
<br>In the end, it was &quot;key&quot; that told you what this was, not the type (Which is up to the user to provide).
<br>
<br>So i don't see why K is any better than A and V is any better than B.<div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=175069" width="1" height="1">T* pattternhttp://blogs.msdn.com/b/brada/archive/2004/07/04/172982.aspx#174970Wed, 07 Jul 2004 07:23:00 GMT91d46819-8472-40ad-a661-2c78acb4018c:174970Julien CouvreurThere is an I* standard for interfaces. Why not T*, like TKey and TValue, for type parameters?<div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=174970" width="1" height="1">re: Naming generic type parameters: Dictionary<K,V> or Dictionary<KeyType, ValueType>http://blogs.msdn.com/b/brada/archive/2004/07/04/172982.aspx#174514Wed, 07 Jul 2004 00:11:00 GMT91d46819-8472-40ad-a661-2c78acb4018c:174514damien mortonNot wanting to hedge my bets, but...
<br>
<br>For one or two type paramaters, and where their meaning is crear, single letter type params are acceptable to me.
<br>
<br>Dictionary&lt;K,V&gt; is a case in point. Its clear what K and V mean, and consise to read and write.
<br>
<br>Collection&lt;T&gt; is another case in point; its also clear and consise.
<br>
<br>Foo&lt;U,V,W&gt; is consise, but not clear. It would be better to use more descriptive names; In this case, I like the *Type pattern.<div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=174514" width="1" height="1">re: Naming generic type parameters: Dictionary<K,V> or Dictionary<KeyType, ValueType>http://blogs.msdn.com/b/brada/archive/2004/07/04/172982.aspx#174431Tue, 06 Jul 2004 22:29:00 GMT91d46819-8472-40ad-a661-2c78acb4018c:174431Staffan GustafssonCount me in on Chance's side :)
<br>
<br>He's argument that a solution needs to accomodate both aspects is definitly valid. (i.e. both indicate a generic type, and it's intended use).
<br>
<br>Don't you have a similar situation with interfaces?
<br>
<br>And I like Ron's suggestion on ItemT in favor of TItem. But both are better than single letter.
<br>
<br>Please don't go down that path...
<br><div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=174431" width="1" height="1">re: Naming generic type parameters: Dictionary<K,V> or Dictionary<KeyType, ValueType>http://blogs.msdn.com/b/brada/archive/2004/07/04/172982.aspx#174170Tue, 06 Jul 2004 17:54:00 GMT91d46819-8472-40ad-a661-2c78acb4018c:174170RonI have to agree with Chance (as well as others). Descriptive names are far better, in terms that it provides a clearer meaning of the type, than single characters (no matter how intuitive these single characters may seem). I'll use generics either way, but I can't sit back and recommend the current single letter approach as a convention for custom generic classes. I'd certainly rather see the Framework and design standards more toward the full description names.
<br>
<br>As for the actual format, I see no problem with ItemT, KeyT, ValueT, etc.; but I think it could lead to the same issue where developers begin to question what the T really means.<div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=174170" width="1" height="1">re: Naming generic type parameters: Dictionary<K,V> or Dictionary<KeyType, ValueType>http://blogs.msdn.com/b/brada/archive/2004/07/04/172982.aspx#174136Tue, 06 Jul 2004 17:38:00 GMT91d46819-8472-40ad-a661-2c78acb4018c:174136Chance GillespieI just happened upon all this while checking the blog for some other thread. In an interesting coincidence I was fiddling with this myself today trying to find something that worked for me since &lt;T, U&gt; was just not going to cut it for my own code. I do my best to stick with the conventions laid out by MS (for .Net of course, I used to vomit any time I saw any Hungarian Notation :) ) but this is one where I just couldn’t do it. I can’t grasp how anyone can argue in favor of the cryptic naming convention currently in use. A key point that needs to be achieved is to differentiate generic parameter types from normal types (as they will be used in more places than between the &lt;&gt; ), but I don’t see the logic in an approach that says “OK, here, this is our convention, and you know it’s a generic parameter when you look at it, but we aren’t going to tell you what it’s actually used for or means.”. The solution requires both… It must indicate it’s a generic parameter and it must also be ‘intended use’ descriptive. There have been quite a few suggestions (TKey, KeyT, GKey, KEY) .. we can argue till we are blue in the face on which alternate method is best, but I believe that ANY of them are better than the single capital letter we have at the moment.
<br>
<br>Please, please PLEASE step up the plate on this one MS. You’re still in BETA and there is time to change. Whatever you end up shipping will end up the standard and if what we have now becomes the standard we have a hell of a lot of hand obfuscated generic code to look forward to. Joy of Joys
<br><div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=174136" width="1" height="1">re: Naming generic type parameters: Dictionary<K,V> or Dictionary<KeyType, ValueType>http://blogs.msdn.com/b/brada/archive/2004/07/04/172982.aspx#173974Tue, 06 Jul 2004 14:48:00 GMT91d46819-8472-40ad-a661-2c78acb4018c:173974Brad AbramsJust to respond to Ernst's point... the CLR does allow any number of letters in type paramaters... This is just a question of what we do in the Framework and suggest in the API Design Guidelines. <div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=173974" width="1" height="1">re: Naming generic type parameters: Dictionary<K,V> or Dictionary<KeyType, ValueType>http://blogs.msdn.com/b/brada/archive/2004/07/04/172982.aspx#173924Tue, 06 Jul 2004 13:45:00 GMT91d46819-8472-40ad-a661-2c78acb4018c:173924Philip RieckNo matter what the subtleties are in this, you don't have
<br>public object this[object k] { ...}
<br>
<br>in the current hashtable class. Instead, we have
<br>public object this[object key] {...}
<br>
<br>
<br>Why would we have
<br>public T this[K key]{...}
<br>
<br>instead of
<br>public ItemType this[KeyType key] { ...}
<br>
<br>
<br>The only justification I can see that doesn't also apply to shortened parameter names (do you really want that?) is that of differentiating between using a type that was used to construct this generic, or using a type simply defined elsewhere (as in, is KeyType a separate class, or one used to construct this generic).
<br>
<br>I'm willing to live with that minor ambiguity (especially since the sematics are the same as far as the generic implementation is concerned) In order to not have to guess what T,U,V,W mean every time I construct a type.
<br>
<br>
<br><div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=173924" width="1" height="1">re: Naming generic type parameters: Dictionary<K,V> or Dictionary<KeyType, ValueType>http://blogs.msdn.com/b/brada/archive/2004/07/04/172982.aspx#173811Tue, 06 Jul 2004 11:11:00 GMT91d46819-8472-40ad-a661-2c78acb4018c:173811Mitch DennyI personally prefer the single letter versions. Just caus'<div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=173811" width="1" height="1">re: Naming generic type parameters: Dictionary<K,V> or Dictionary<KeyType, ValueType>http://blogs.msdn.com/b/brada/archive/2004/07/04/172982.aspx#173810Tue, 06 Jul 2004 11:05:00 GMT91d46819-8472-40ad-a661-2c78acb4018c:173810Ernst KuschkeMaking provision for more letters will satisfy both camps... my 5 cents ;)
<br>How (non)trivial this change might be in the CLR, I don't know.<div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=173810" width="1" height="1">re: Naming generic type parameters: Dictionary<K,V> or Dictionary<KeyType, ValueType>http://blogs.msdn.com/b/brada/archive/2004/07/04/172982.aspx#173577Tue, 06 Jul 2004 03:42:00 GMT91d46819-8472-40ad-a661-2c78acb4018c:173577Darren OakeyI like this &quot;On the surface this looks like a simple issue (more letters==more clarity)&quot;..
<br>
<br>well... I don't see any depth here. What it comes down to is Dictionary&lt;KeyType, ValueType&gt; is more quickly understood than Dictionary&lt;A,B&gt;, and really, that should be the be all and end all of it.
<br>
<br>It's not like you get browny points for shorter code or anything! There are 0 (count em) possible advantages of using a non-descriptive variable name at any point - (forgetting about the original basic where you would incur a performance penalty) and hundreds of reasons why having a meaningful parameter may one day help someone understand something.
<br>
<br>
<br>
<br>
<br>
<br><div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=173577" width="1" height="1">re: Naming generic type parameters: Dictionary<K,V> or Dictionary<KeyType, ValueType>http://blogs.msdn.com/b/brada/archive/2004/07/04/172982.aspx#173164Mon, 05 Jul 2004 11:36:00 GMT91d46819-8472-40ad-a661-2c78acb4018c:173164Mario GoebbelsWell, I prefer the single letter versions. It would be nice though if Intellisense would work on these parameters, in case someone starts using random letters for the parameters.<div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=173164" width="1" height="1">re: Naming generic type parameters: Dictionary<K,V> or Dictionary<KeyType, ValueType>http://blogs.msdn.com/b/brada/archive/2004/07/04/172982.aspx#173115Mon, 05 Jul 2004 09:48:00 GMT91d46819-8472-40ad-a661-2c78acb4018c:173115Cyrus NajmabadiDictionary&lt;A,B&gt; obviously.
<br>
<br>parametric polymorphism allows a user to substitute their own types for the parameters. The parameters are pretty meaningless and can be subtably represented with something like:
<br>
<br>Dictionary&lt;1,2&gt;
<br>
<br>A suitable scheme is to start with A and the continue through the alphabet. If you hit more than 26... well, that's interesting :-)<div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=173115" width="1" height="1">re: Naming generic type parameters: Dictionary<K,V> or Dictionary<KeyType, ValueType>http://blogs.msdn.com/b/brada/archive/2004/07/04/172982.aspx#173013Mon, 05 Jul 2004 05:10:00 GMT91d46819-8472-40ad-a661-2c78acb4018c:173013Mike DunnMORE letters please. The tradition of writing a C++ template example like this:
<br>
<br>template class Foo&lt;T, U, bar&gt; { ... }
<br>
<br>is a bad one because it just makes the template harder to read and understand.<div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=173013" width="1" height="1">re: Naming generic type parameters: Dictionary<K,V> or Dictionary<KeyType, ValueType>http://blogs.msdn.com/b/brada/archive/2004/07/04/172982.aspx#172994Mon, 05 Jul 2004 04:06:00 GMT91d46819-8472-40ad-a661-2c78acb4018c:172994Jeff AtwoodI belive the issue are more subtle too. It can not be more subtle.
<br>
<br>;)
<br>
<br><div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=172994" width="1" height="1">