<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.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>Breaking changes and named arguments</title><link>http://blogs.msdn.com/b/ericlippert/archive/2011/11/07/breaking-changes-and-named-arguments.aspx</link><description>Before I get into the subject of today's post, thanks so much to all of you who have given us great feedback on the Roslyn CTP. Please keep it coming . I'm definitely going to do some articles on Roslyn in the future; the past few weeks I have been too</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: Breaking changes and named arguments</title><link>http://blogs.msdn.com/b/ericlippert/archive/2011/11/07/breaking-changes-and-named-arguments.aspx#10252052</link><pubDate>Fri, 30 Dec 2011 15:23:27 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10252052</guid><dc:creator>Alan Pretre</dc:creator><description>&lt;p&gt;It appears there is a bug in C# 4.0 with named arguments and struct members passed by reference:&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://connect.microsoft.com/VisualStudio/feedback/details/716017/evaluation-of-named-by-ref-arguments-in-c"&gt;connect.microsoft.com/.../evaluation-of-named-by-ref-arguments-in-c&lt;/a&gt;&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10252052" width="1" height="1"&gt;</description></item><item><title>re: Breaking changes and named arguments</title><link>http://blogs.msdn.com/b/ericlippert/archive/2011/11/07/breaking-changes-and-named-arguments.aspx#10245736</link><pubDate>Thu, 08 Dec 2011 19:27:53 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10245736</guid><dc:creator>Alex</dc:creator><description>&lt;p&gt;Slightly off topic, but I recently noticed that the use of named parameters has a noticable performance impact. What is the reason for that? Aren&amp;#39;t optional and named parameters just caller side &amp;quot;compiler sugar&amp;quot;?&lt;/p&gt;
&lt;div class="yellowbox"&gt;
&lt;p&gt;Named and optional arguments can cause the compiler to introduce temporary varibles (to ensure that the side effects of argument computation happen in source code order, not in the order in which the arguments are put on the stack for the call.) Introducing extra temporaries can sometimes cause the jitter to generate less optimal code. I don&amp;#39;t know of any other specific situation though; can you give me an example? -- Eric&lt;/p&gt;
&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10245736" width="1" height="1"&gt;</description></item><item><title>re: Breaking changes and named arguments</title><link>http://blogs.msdn.com/b/ericlippert/archive/2011/11/07/breaking-changes-and-named-arguments.aspx#10237137</link><pubDate>Tue, 15 Nov 2011 06:37:06 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10237137</guid><dc:creator>NB</dc:creator><description>&lt;p&gt;@Gabe - No, I think I prefer named parameters to defining wrapper classes/structs for all different parameter kinds. Seems to me they achieve pretty much the same in a generic way and without overhead as well. They might perhaps not be as powerful but I think I can live with that.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10237137" width="1" height="1"&gt;</description></item><item><title>re: Breaking changes and named arguments</title><link>http://blogs.msdn.com/b/ericlippert/archive/2011/11/07/breaking-changes-and-named-arguments.aspx#10236918</link><pubDate>Mon, 14 Nov 2011 18:37:04 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10236918</guid><dc:creator>Gabe</dc:creator><description>&lt;p&gt;DRBlaise: Yes, it is much better to always create a wrapper class for every parameter type that is duplicated in any method call. That way rather than having something ridiculously confusing like&lt;/p&gt;
&lt;p&gt;CopyFile(SourcePath: srcPath, SourceFilename: srcFile, DestPath: dstPath, DestFilename: dstFile);&lt;/p&gt;
&lt;p&gt;you can have the much clearer&lt;/p&gt;
&lt;p&gt;CopyFile(new SourcePathName(new Path(srcPath), new Filename(srcFile)), new DestPathName(new Path(dstPath), new Filename(dstFile)));&lt;/p&gt;
&lt;p&gt;right?&lt;/p&gt;
&lt;p&gt;Sure, it requires extra typing, the creation of 4 wrapper classes, and additional overhead of memory allocation, wrapping calls, unwrapping calls, and garbage collection. But it&amp;#39;s all worth it because, even though the wrapper classes&amp;#39; names can&amp;#39;t ever change and the user of a method can&amp;#39;t put the arguments in the order that makes sense in context, the method parameter names can be changed on a whim, right?&lt;/p&gt;
&lt;p&gt;Besides, you can just make the wrapper types structs to avoid the memory allocation and GC overhead, and hopefully your JIT will inline the wrapping/unwrapping calls you make. So really only the person writing the method and the person calling the method would have to pay for the overhead; the person running the code should be largely unaware of the wrappers so long as they don&amp;#39;t notice the extra disk space, memory usage, and JIT time that the wrappers add.&lt;/p&gt;
&lt;p&gt;Of course if you&amp;#39;re using a language like Java, you don&amp;#39;t have structs, so each type has to be a class that requires memory allocation (though maybe the JIT can optimize that out?), and each wrapper class requires not only its own source file but also its own object file.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10236918" width="1" height="1"&gt;</description></item><item><title>re: Breaking changes and named arguments</title><link>http://blogs.msdn.com/b/ericlippert/archive/2011/11/07/breaking-changes-and-named-arguments.aspx#10236815</link><pubDate>Mon, 14 Nov 2011 15:02:34 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10236815</guid><dc:creator>DRBlaise</dc:creator><description>&lt;p&gt;@Gabe - As PleaseDontLowerCSharpQualityBar points out, the clarity of of named parameters is only valid when you make a poor design choice in the creation of your method parameters. &amp;nbsp;If you go with the best design practice of never having 2 method parameters of the same type, then you avoid the need of named parameters.&lt;/p&gt;
&lt;p&gt;If the CalculateMotion method was defined as void CalculateMotion(Postion position, Velocity velocity, Acceleration acceleration) then there would be no problem.&lt;/p&gt;
&lt;p&gt;Just like you should never use bool as a parameter of a method, you should really never use double, int, string, etc. as they don&amp;#39;t specify the MEANING of the parameter. &amp;nbsp;Always create a wrapper class that specifies the meaning of the parameter, and you can avoid the need for named parameters.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10236815" width="1" height="1"&gt;</description></item><item><title>re: Breaking changes and named arguments</title><link>http://blogs.msdn.com/b/ericlippert/archive/2011/11/07/breaking-changes-and-named-arguments.aspx#10236699</link><pubDate>Mon, 14 Nov 2011 04:56:22 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10236699</guid><dc:creator>Gabe</dc:creator><description>&lt;p&gt;One of the great uses for named parameters is for making it clear what parameters you&amp;#39;re passing. For example,&lt;/p&gt;
&lt;p&gt;CalculateMotion(velocity: aircraftVelocity, position: aircraftPosition, acceleration: aircraftAcceleration);&lt;/p&gt;
&lt;p&gt;is obviously correct, while &lt;/p&gt;
&lt;p&gt;CalculateMotion(aircraftVelocity, aircraftPosition, aircraftAcceleration);&lt;/p&gt;
&lt;p&gt;requires looking at the documentation or the declaration of CalculateMotion to tell if it&amp;#39;s correct.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10236699" width="1" height="1"&gt;</description></item><item><title>re: Breaking changes and named arguments</title><link>http://blogs.msdn.com/b/ericlippert/archive/2011/11/07/breaking-changes-and-named-arguments.aspx#10236263</link><pubDate>Fri, 11 Nov 2011 18:14:14 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10236263</guid><dc:creator>UseTheRightToolForTheJob</dc:creator><description>&lt;p&gt;Tergiver: if you like a more verbose syntax (with &amp;quot;and&amp;quot; instead of &amp;quot;&amp;amp;&amp;amp;&amp;quot;, etc.) then you can use VB.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10236263" width="1" height="1"&gt;</description></item><item><title>re: Breaking changes and named arguments</title><link>http://blogs.msdn.com/b/ericlippert/archive/2011/11/07/breaking-changes-and-named-arguments.aspx#10236185</link><pubDate>Fri, 11 Nov 2011 15:44:29 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10236185</guid><dc:creator>Tergiver</dc:creator><description>&lt;p&gt;Named parameters are a great feature to help make code more readable.&lt;/p&gt;
&lt;p&gt;Now when are the C# authors going to finally get around to implementing ISO646 in C#? C and C++ have had it for a very long time: &lt;a rel="nofollow" target="_new" href="http://www.cplusplus.com/reference/clibrary/ciso646/"&gt;www.cplusplus.com/.../ciso646&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;While I personally don&amp;#39;t use the named operators for bit operations, I much prefer&lt;/p&gt;
&lt;p&gt;if (ready and enabled)&lt;/p&gt;
&lt;p&gt;to&lt;/p&gt;
&lt;p&gt;if (ready &amp;amp;&amp;amp; enabled)&lt;/p&gt;
&lt;p&gt;Oh right.. it&amp;#39;s too late. It would now be a breaking change to the language =(&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10236185" width="1" height="1"&gt;</description></item><item><title>re: Breaking changes and named arguments</title><link>http://blogs.msdn.com/b/ericlippert/archive/2011/11/07/breaking-changes-and-named-arguments.aspx#10235930</link><pubDate>Thu, 10 Nov 2011 20:28:21 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10235930</guid><dc:creator>Shawn Hargreaves - MSFT</dc:creator><description>&lt;p&gt;&amp;gt; But now with this new C# 4 feature you introduced a breaking change: I must be bothered with parameter names.&lt;/p&gt;
&lt;p&gt;You are making two conflicting arguments at the same time:&lt;/p&gt;
&lt;p&gt;1) &amp;nbsp;&amp;quot;This is a breaking change because it forces me to bother with parameter names.&amp;quot;&lt;/p&gt;
&lt;p&gt;2) &amp;nbsp;&amp;quot;It did not use to be a breaking change because I chose not to care about VB developers or people using reflection.&amp;quot;&lt;/p&gt;
&lt;p&gt;If you are in a position where you can selectively choose to ignore things, why not choose to ignore named parameters, just the same as you already chose to ignore VB and reflection users? &amp;nbsp;Or if you are in a position where you have to make things work for all possible consumers of your library, then you already had to bother with parameter names, for the reasons already given. &amp;nbsp;You can&amp;#39;t have it both ways.&lt;/p&gt;
&lt;p&gt;&amp;gt; &amp;nbsp;Eric&amp;gt; Professional VB developers are an enormous market and have been for almost two decades now.&lt;/p&gt;
&lt;p&gt;&amp;gt; &lt;/p&gt;
&lt;p&gt;&amp;gt; I don&amp;#39;t agree with that, and that is based on my experience, probably different from yours.&lt;/p&gt;
&lt;p&gt;This has nothing to do with subjective personal experiences. &amp;nbsp;The size of the VB developer market is easily quantified by counting the number of VB developers who exist in the world. &amp;nbsp;If you do that, you will find it is a very large number.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10235930" width="1" height="1"&gt;</description></item><item><title>re: Breaking changes and named arguments</title><link>http://blogs.msdn.com/b/ericlippert/archive/2011/11/07/breaking-changes-and-named-arguments.aspx#10235716</link><pubDate>Thu, 10 Nov 2011 10:26:42 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10235716</guid><dc:creator>PleaseDontLowerCSharpQualityBar</dc:creator><description>&lt;p&gt;hype8912&amp;gt; I&amp;#39;ve never found a use for this capability to begin with. It just makes the calling string that much longer. &lt;/p&gt;
&lt;p&gt;Consider this case:&lt;/p&gt;
&lt;p&gt;// #1 - named arguments&lt;/p&gt;
&lt;p&gt;new StreamWriter(&lt;/p&gt;
&lt;p&gt; &amp;nbsp;path: filename,&lt;/p&gt;
&lt;p&gt; &amp;nbsp;append: true,&lt;/p&gt;
&lt;p&gt; &amp;nbsp;encoding: someEncoding&lt;/p&gt;
&lt;p&gt;)&lt;/p&gt;
&lt;p&gt;// #2&lt;/p&gt;
&lt;p&gt;new StreamWriter(&lt;/p&gt;
&lt;p&gt; &amp;nbsp;filename,&lt;/p&gt;
&lt;p&gt; &amp;nbsp;true,&lt;/p&gt;
&lt;p&gt; &amp;nbsp;someEncoding&lt;/p&gt;
&lt;p&gt;)&lt;/p&gt;
&lt;p&gt;#1 uses named arguments, and IMHO is too much verbose.&lt;/p&gt;
&lt;p&gt;The only semantic information added by #1 and kind of lacking in #2 is about the append mode.&lt;/p&gt;
&lt;p&gt;But the problem here is due to an unfortunate design decision in the StreamWriter constructor prototype.&lt;/p&gt;
&lt;p&gt;In fact, instead of using a bool, I&amp;#39;d have used a descriptive enum to specify append vs. overwrite mode.&lt;/p&gt;
&lt;p&gt;If an enum was used in this case, named arguments would have added nothing, because we&amp;#39;d have read some &amp;quot;append&amp;quot; in the calling code, instead of the more opaque &amp;#39;true&amp;#39;.&lt;/p&gt;
&lt;p&gt;Another example that some people consider in favor of named arguments is for methods taking several parameters.&lt;/p&gt;
&lt;p&gt;But IMHO it is a design horror: if a method has too much parameters, this may be a red flag, and some refactoring may be needed. For example:&lt;/p&gt;
&lt;p&gt;void CalculateMotion( &lt;/p&gt;
&lt;p&gt; &amp;nbsp;double positionX, double positionY, double positionZ, &lt;/p&gt;
&lt;p&gt; &amp;nbsp;double velocityX, double velocityY, double velocityZ,&lt;/p&gt;
&lt;p&gt; &amp;nbsp;double accelerationX, double accelerationY, double accelerationY&lt;/p&gt;
&lt;p&gt;)&lt;/p&gt;
&lt;p&gt;is a coding horror. The double&amp;#39;s should be better packed in a more meaningful Vector3 class, and the method refactored something like this:&lt;/p&gt;
&lt;p&gt;void CalculateMotion( &lt;/p&gt;
&lt;p&gt; &amp;nbsp;Vector3 position,&lt;/p&gt;
&lt;p&gt; &amp;nbsp;Vector3 velocity,&lt;/p&gt;
&lt;p&gt; &amp;nbsp;Vector3 acceleration &lt;/p&gt;
&lt;p&gt;);&lt;/p&gt;
&lt;p&gt;And then, when argument names are choosen carefully, there is no need to repeat the parameter names:&lt;/p&gt;
&lt;p&gt;CalculateMotion( aircraftPosition, aircraftVelocity, aircraftAcceleration );&lt;/p&gt;
&lt;p&gt;is just better then:&lt;/p&gt;
&lt;p&gt;CalculateMotion( &lt;/p&gt;
&lt;p&gt; &amp;nbsp;position : aircraftPosition, &lt;/p&gt;
&lt;p&gt; &amp;nbsp;velocity: aircraftVelocity,&lt;/p&gt;
&lt;p&gt; &amp;nbsp;acceleration: aircraftAcceleration&lt;/p&gt;
&lt;p&gt;); &lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10235716" width="1" height="1"&gt;</description></item></channel></rss>