<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msmvps.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Evil code of the day</title><link>http://msmvps.com/blogs/jon_skeet/archive/2009/07/03/evil-code-of-the-day.aspx</link><description>At a glance, this code doesn&amp;#39;t look particularly evil. What does it do though? Compile it with the C# 4.0b1 compiler and run it... using System; class Base { &amp;#160;&amp;#160;&amp;#160; public &amp;#160; virtual &amp;#160; void Foo( int x, int y) &amp;#160;&amp;#160;&amp;#160;</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>re: Evil code of the day</title><link>http://msmvps.com/blogs/jon_skeet/archive/2009/07/03/evil-code-of-the-day.aspx#1714965</link><pubDate>Tue, 11 Aug 2009 20:49:37 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1714965</guid><dc:creator>Yelinna</dc:creator><description>&lt;p&gt;Happily, this is an unlikely situation. Why would you want to overload a method with another method with the same name, the same number of patameters and their same types?&lt;/p&gt;
&lt;p&gt;Overloading is not for this coding evilness!!!&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1714965" width="1" height="1"&gt;</description></item><item><title>re: Evil code of the day</title><link>http://msmvps.com/blogs/jon_skeet/archive/2009/07/03/evil-code-of-the-day.aspx#1702378</link><pubDate>Thu, 16 Jul 2009 18:44:57 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1702378</guid><dc:creator>Leo Bushkin</dc:creator><description>&lt;p&gt;Minimally, the compiler should provide a warning when arguments to a method are renamed in an overriding method, or reordered at a call site.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1702378" width="1" height="1"&gt;</description></item><item><title>re: Evil code of the day</title><link>http://msmvps.com/blogs/jon_skeet/archive/2009/07/03/evil-code-of-the-day.aspx#1702376</link><pubDate>Thu, 16 Jul 2009 18:39:29 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1702376</guid><dc:creator>Leo Bushkin</dc:creator><description>&lt;p&gt;This is something that really bothers me about the optional parameter spec in C# 4.0 - that and the fact that optional values are actually compiled into the callsite, rather than dynamically fetched at runtime from the assembly&amp;#39;s metadata and then cached.&lt;/p&gt;
&lt;p&gt;C# 4.0 has essentially made the parameter names a weak part of the signature of the method - as far as overload resolution is concerned for optional/named parameters.&lt;/p&gt;
&lt;p&gt;I have to say, that I believe that this should be a case where the compiler detects and prohibits the override in the derived class. &lt;/p&gt;
&lt;p&gt;In fact, the compiler should enforce that when the override keyword is applied to a method, the argument names must match the base method in both order and name.&lt;/p&gt;
&lt;p&gt;It may not be perfect, especially when parameter names can change in the base class over time, or conflict with other overloads - but it&amp;#39;s better than changing the overload resolution on the fly based on which version of the framework you compile to - or whether you call the method through a base or derived reference, or through a dynamic reference.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1702376" width="1" height="1"&gt;</description></item><item><title>re: Evil code of the day</title><link>http://msmvps.com/blogs/jon_skeet/archive/2009/07/03/evil-code-of-the-day.aspx#1699766</link><pubDate>Sun, 12 Jul 2009 11:55:50 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1699766</guid><dc:creator>Trumpi</dc:creator><description>&lt;p&gt;I hope that there is an FxCop rule for this!!&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1699766" width="1" height="1"&gt;</description></item><item><title>re: Evil code of the day</title><link>http://msmvps.com/blogs/jon_skeet/archive/2009/07/03/evil-code-of-the-day.aspx#1698627</link><pubDate>Tue, 07 Jul 2009 19:13:39 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1698627</guid><dc:creator>ShuggyCoUk</dc:creator><description>&lt;p&gt;I added my +1 to the connect feature&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1698627" width="1" height="1"&gt;</description></item><item><title>re: Evil code of the day</title><link>http://msmvps.com/blogs/jon_skeet/archive/2009/07/03/evil-code-of-the-day.aspx#1698422</link><pubDate>Mon, 06 Jul 2009 15:54:59 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1698422</guid><dc:creator>Koush</dc:creator><description>&lt;p&gt;It&amp;#39;s more evil that the parameters on an overridden method were renamed. ;)&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1698422" width="1" height="1"&gt;</description></item><item><title>re: Evil code of the day</title><link>http://msmvps.com/blogs/jon_skeet/archive/2009/07/03/evil-code-of-the-day.aspx#1698362</link><pubDate>Mon, 06 Jul 2009 08:07:30 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1698362</guid><dc:creator>runefs</dc:creator><description>&lt;p&gt;Rather unfortunate that the specifications basicallty breaks Liskov.&lt;/p&gt;
&lt;p&gt;If you for some reason renamed the parameters of one of your classes you can no longer make the substitution with out breaking your code.&lt;/p&gt;
&lt;p&gt;You might argue that the the developer is breaking the principle by using named parameters (and changing names) but I none the less finds it questionable when the language makes that possible&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1698362" width="1" height="1"&gt;</description></item><item><title>re: Evil code of the day</title><link>http://msmvps.com/blogs/jon_skeet/archive/2009/07/03/evil-code-of-the-day.aspx#1698275</link><pubDate>Sun, 05 Jul 2009 20:26:52 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1698275</guid><dc:creator>skeet</dc:creator><description>&lt;p&gt;@David: Admittedly I hadn&amp;#39;t checked whether VB had named arguments. (Even C# has had named *parameters* forever :) Yes, I hope it&amp;#39;s mentioned in the FDG. I suspect it&amp;#39;s a guideline which hasn&amp;#39;t been treated particularly seriously in the past though - there&amp;#39;s nothing like the risk of it happening *within your own language* to highlight a potential problem to developers :)&lt;/p&gt;
&lt;p&gt;(Of course, it means that it&amp;#39;s yet more names to try to get right - and we know what a pain *they* are.)&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1698275" width="1" height="1"&gt;</description></item><item><title>re: Evil code of the day</title><link>http://msmvps.com/blogs/jon_skeet/archive/2009/07/03/evil-code-of-the-day.aspx#1698271</link><pubDate>Sun, 05 Jul 2009 20:18:24 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1698271</guid><dc:creator>David Nelson</dc:creator><description>&lt;p&gt;Anyone who has been concerned with interoperability has always considered parameter names to be a part of the public API, given that VB has had named parameters from the beginning. I believe there is a statement about that in the Framework Design Guidelines, although I can&amp;#39;t remember where at the moment.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1698271" width="1" height="1"&gt;</description></item><item><title>re: Evil code of the day</title><link>http://msmvps.com/blogs/jon_skeet/archive/2009/07/03/evil-code-of-the-day.aspx#1698176</link><pubDate>Sun, 05 Jul 2009 11:50:30 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1698176</guid><dc:creator>skeet</dc:creator><description>&lt;p&gt;@Joren: Yes, it&amp;#39;s certainly my code which is evil here. I mainly wanted to draw attention to the fact that something which was previously harmless (renaming parameters) can now be very harmful indeed - and if you just reorder parameter names (instead of giving them completely different names) you can end up causing a bug quite easily.&lt;/p&gt;
&lt;p&gt;Likewise changing the declared type of a variable now not only affects which overloads are considered, but also which argument means what.&lt;/p&gt;
&lt;p&gt;I&amp;#39;m in no way trying to say that named arguments are a misfeature: just that they also introduce more opportunities for error. Use with care, and be aware that parameter names are now part of the API.&lt;/p&gt;
&lt;p&gt;Jon&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1698176" width="1" height="1"&gt;</description></item><item><title>re: Evil code of the day</title><link>http://msmvps.com/blogs/jon_skeet/archive/2009/07/03/evil-code-of-the-day.aspx#1698167</link><pubDate>Sun, 05 Jul 2009 11:11:06 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1698167</guid><dc:creator>Joren</dc:creator><description>&lt;p&gt;There is nothing nasty about the language here. Only the derived code is nasty.&lt;/p&gt;
&lt;p&gt;You can&amp;#39;t use the parameter names for Base.Foo when directly calling Derived.Foo. If you have a method with parameters &amp;#39;foo&amp;#39; and &amp;#39;bar&amp;#39; it&amp;#39;s ridiculous to have to call it with &amp;#39;blah&amp;#39; and &amp;#39;zing&amp;#39; just because those happen to be the parameter names in some ancestor of the method. Just think of the confusion this would cause.&lt;/p&gt;
&lt;p&gt;You also can&amp;#39;t use the parameter names for Derived.Foo when calling Base.Foo, because there is no reason to suppose that Derived.Foo is any better for taking the names of than SomeOtherDerived.Foo. In fact, there might not even be any derived classes.&lt;/p&gt;
&lt;p&gt;So you always have to use the parameter names for the method you&amp;#39;re calling, regardless of it being virtual or an override or not. Which makes perfect sense.&lt;/p&gt;
&lt;p&gt;There is nothing else the language could logically do. It&amp;#39;s your code that is evil. And there&amp;#39;s a simple solution for that: don&amp;#39;t write this code (and who would!) :-)&lt;/p&gt;
&lt;p&gt;PS. I don&amp;#39;t actually see you saying that the problem is in C# and not your code per se ... in fact I basically see you saying the opposite. But then, why call attention to the obvious fact that even if there&amp;#39;s nothing wrong with a language you can still do horrible things in it? I don&amp;#39;t understand.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1698167" width="1" height="1"&gt;</description></item><item><title>re: Evil code of the day</title><link>http://msmvps.com/blogs/jon_skeet/archive/2009/07/03/evil-code-of-the-day.aspx#1698041</link><pubDate>Sat, 04 Jul 2009 21:59:38 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1698041</guid><dc:creator>Jeroen Landheer</dc:creator><description>&lt;p&gt;Code analisys (FxCop) has a rule for this one: &amp;quot;Parameter names should match base declaration&amp;quot; (CA1725)&lt;/p&gt;
&lt;p&gt;This rule only checks types with a public access modifier though.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1698041" width="1" height="1"&gt;</description></item><item><title>re: Evil code of the day</title><link>http://msmvps.com/blogs/jon_skeet/archive/2009/07/03/evil-code-of-the-day.aspx#1698034</link><pubDate>Sat, 04 Jul 2009 21:23:56 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1698034</guid><dc:creator>Eamon Nerbonne</dc:creator><description>&lt;p&gt;C# devs have historically seemed reluctant to add new warnings, the motivation apparently being that this may break users with &amp;quot;Warnings as Errors&amp;quot; on. &amp;nbsp;For instance, even obviously unsafe usage of IDisposable classes still doesn&amp;#39;t cause a compiler warning (when it really should).&lt;/p&gt;
&lt;p&gt;So, if that&amp;#39;s really a prime motivation, then adding a warning at the call site may be lower risk to them - after all, you can&amp;#39;t break old code that way, since no old code has named parameters _anyhow_.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1698034" width="1" height="1"&gt;</description></item><item><title>re: Evil code of the day</title><link>http://msmvps.com/blogs/jon_skeet/archive/2009/07/03/evil-code-of-the-day.aspx#1698027</link><pubDate>Sat, 04 Jul 2009 20:39:27 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1698027</guid><dc:creator>skeet</dc:creator><description>&lt;p&gt;@James: At that point the dynamic version and the statically typed version would presumably diverge in behaviour.&lt;/p&gt;
&lt;p&gt;Let&amp;#39;s not do that :)&lt;/p&gt;
&lt;p&gt;For what it&amp;#39;s worth, I&amp;#39;m going to make it very clear in C# in Depth that named arguments make parameter names effectively part of the API for public members: change them at your peril!&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1698027" width="1" height="1"&gt;</description></item><item><title>re: Evil code of the day</title><link>http://msmvps.com/blogs/jon_skeet/archive/2009/07/03/evil-code-of-the-day.aspx#1698025</link><pubDate>Sat, 04 Jul 2009 20:35:52 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1698025</guid><dc:creator>James Hart</dc:creator><description>&lt;p&gt;For extra giggles, I find myself wondering what happens if Base, Derived, and PureEvil all live in separate assemblies, and you fiddle with the parameter names in Base and/or Derived without recompiling the other assemblies...&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1698025" width="1" height="1"&gt;</description></item><item><title>re: Evil code of the day</title><link>http://msmvps.com/blogs/jon_skeet/archive/2009/07/03/evil-code-of-the-day.aspx#1698009</link><pubDate>Sat, 04 Jul 2009 18:57:37 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1698009</guid><dc:creator>skeet</dc:creator><description>&lt;p&gt;Right, Connect suggestion added at &lt;a rel="nofollow" target="_new" href="https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=472581"&gt;connect.microsoft.com/.../ViewFeedback.aspx&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;It&amp;#39;ll be interesting to see what the feedback is like :)&lt;/p&gt;
&lt;p&gt;Jon&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1698009" width="1" height="1"&gt;</description></item><item><title>re: Evil code of the day</title><link>http://msmvps.com/blogs/jon_skeet/archive/2009/07/03/evil-code-of-the-day.aspx#1698006</link><pubDate>Sat, 04 Jul 2009 18:40:23 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1698006</guid><dc:creator>skeet</dc:creator><description>&lt;p&gt;@ShuggyCoUk: That&amp;#39;s a very interesting idea - I&amp;#39;d actually suggest putting the warning on both the call site and the overriding code.&lt;/p&gt;
&lt;p&gt;@John: Parameter names are the names of variables, but in C# 4 you can specify named arguments, which can be in a different order to the declaration order.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1698006" width="1" height="1"&gt;</description></item><item><title>re: Evil code of the day</title><link>http://msmvps.com/blogs/jon_skeet/archive/2009/07/03/evil-code-of-the-day.aspx#1697968</link><pubDate>Sat, 04 Jul 2009 14:59:24 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1697968</guid><dc:creator>John Kraft</dc:creator><description>&lt;p&gt;I thought that the named parameters were only supposed to be &amp;#39;labels&amp;#39; for the parameters, and not effect behavior at all???&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1697968" width="1" height="1"&gt;</description></item><item><title>re: Evil code of the day</title><link>http://msmvps.com/blogs/jon_skeet/archive/2009/07/03/evil-code-of-the-day.aspx#1697967</link><pubDate>Sat, 04 Jul 2009 14:56:50 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1697967</guid><dc:creator>ShuggyCoUk</dc:creator><description>&lt;p&gt;I would suggest that they should add a compiler warning for any call site using named parameters where the parameter names involved have changed in the virtual hierarchy. &lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1697967" width="1" height="1"&gt;</description></item><item><title>re: Evil code of the day</title><link>http://msmvps.com/blogs/jon_skeet/archive/2009/07/03/evil-code-of-the-day.aspx#1697917</link><pubDate>Sat, 04 Jul 2009 10:14:50 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1697917</guid><dc:creator>skeet</dc:creator><description>&lt;p&gt;@Tom: I think C# 4 will help a little bit in many situations, and a lot in a few situations. If you&amp;#39;re working with COM it&amp;#39;ll be a lot nicer. If you&amp;#39;re working in a C# 4-only shop, optional parameters can reduce the need for lots of overloads, and named arguments *can* add readability when used sensible. Covariance and contravariance just removes a little roadblock (in some situations). Dynamic typing is great for interop with COM or dynamic languages, and also makes reflection easier.&lt;/p&gt;
&lt;p&gt;It&amp;#39;s not *nearly* the same size of change as C# 2 or 3 though. And yes, I&amp;#39;d like to see some more const/immutable help too.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1697917" width="1" height="1"&gt;</description></item></channel></rss>