<?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>Fabulous Adventures In Coding : Code Quality</title><link>http://blogs.msdn.com/ericlippert/archive/tags/Code+Quality/default.aspx</link><description>Tags: Code Quality</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Always write a spec, Part Two</title><link>http://blogs.msdn.com/ericlippert/archive/2009/11/23/always-write-a-spec-part-two.aspx</link><pubDate>Mon, 23 Nov 2009 15:01:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9921574</guid><dc:creator>Eric Lippert</dc:creator><slash:comments>19</slash:comments><comments>http://blogs.msdn.com/ericlippert/comments/9921574.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ericlippert/commentrss.aspx?PostID=9921574</wfw:commentRss><description>&lt;DIV class=mine&gt;
&lt;P&gt;Upon submitting that specification for review, even before seeing my code, Chris found a bug and an omission.&lt;/P&gt;
&lt;P&gt;The omission is that I neglected to say what happens when the ref variable is an index into a fixed-size array buffer. As it turns out, that case is also rewritten as a pointer dereference by the time we get to this point in the code, so missing that case turned out to not be a big deal.&lt;/P&gt;
&lt;P&gt;The bug is that this line is wrong:&lt;/P&gt;
&lt;HR&gt;

&lt;DIV class=spec&gt;
&lt;P&gt;if x is ref/out instance.field then&amp;nbsp;add var temp=instance&amp;nbsp;to&amp;nbsp;L and ref/out temp.field to A2. &lt;/P&gt;&lt;/DIV&gt;
&lt;HR&gt;

&lt;P&gt;because it does not meet the stated goal of the method,&amp;nbsp;namely, to&amp;nbsp;produce the same results after the transformation:&lt;/P&gt;&lt;SPAN class=code&gt;
&lt;P&gt;struct S { public int q; }&lt;BR&gt;static void M(ref int r) { r = r + 1; }&lt;BR&gt;static int Zero() { Console.WriteLine("hello!"); return 0; }&lt;BR&gt;...&lt;BR&gt;S[] arr = new[] { new S() };&lt;BR&gt;M(ref arr[Zero()].q);&lt;BR&gt;Console.WriteLine(arr[0].q); // 1&lt;/P&gt;&lt;/SPAN&gt;
&lt;P&gt;Here we have a variable expression of the form instance.field which has a side effect; it prints "Hello". So according to the spec, we rewrite that as&lt;/P&gt;&lt;SPAN class=code&gt;
&lt;P&gt;S&amp;nbsp;temp = arr[Zero()];&lt;BR&gt;M(ref temp.q);&lt;BR&gt;Console.WriteLine(arr[0].q); // 0 !&lt;/P&gt;&lt;/SPAN&gt;
&lt;P&gt;Once more, mutable value types are revealed to be pure concentrated evil. We just mutated temp.q, which is a &lt;EM&gt;copy&lt;/EM&gt; of arr[0].&lt;/P&gt;
&lt;P&gt;The interesting thing here is that Chris found the bug by &lt;STRONG&gt;reading the spec&lt;/STRONG&gt; and thinking about whether I'd missed any interesting cases. Bugs are always cheaper to fix the earlier you find them; a bug you fix as a result of reading the spec is a bug that you don't have to pay QA to find, it's a bug that your customers never see, it's a bug that never causes a backwards-compatibility breaking change, and so on.&lt;/P&gt;
&lt;P&gt;I thought hard about this and realized that the painful case only happens when we have ref/out instance.field with a side effect and &lt;STRONG&gt;instance is a variable of value type&lt;/STRONG&gt;. That's the missing case. Furthermore, we can solve this by recursing!&lt;/P&gt;
&lt;HR&gt;

&lt;DIV class=spec&gt;
&lt;UL&gt;
&lt;LI&gt;if x is ref/out instance.field and&amp;nbsp;instance is&amp;nbsp;a variable of value type, then recurse. Compute T("ref instance"), as if we were invoking a method that takes a single parameter of this ref type. That will give us a resulting declaration list, which we merge onto the end of L, and a resulting argument list with one element on it, r. Add "ref r.field" to A2.&lt;/LI&gt;
&lt;LI&gt;if x is a ref/out instance.field but instance is not a variable of value type, then generate var temp=instance on L, &amp;nbsp;ref temp.field on A2.&lt;/LI&gt;&lt;/UL&gt;&lt;/DIV&gt;
&lt;HR&gt;

&lt;P&gt;So if we have, say, &lt;SPAN class=code&gt;M(ref arr[index].q)&lt;/SPAN&gt;, that ultimately is generated as &lt;SPAN class=code&gt;var t1 = arr, var t2 = index, M(ref t1[t2].q)&lt;/SPAN&gt;, which is correct.&lt;/P&gt;
&lt;P&gt;I fixed up my implementation, wrote some test cases to test the spec, and sent it off to testing for further analysis.&lt;/P&gt;
&lt;P&gt;There's &lt;EM&gt;still&lt;/EM&gt; a bug in this spec, which our tester David found during his analysis of my spec plus proposed implementation. Hint: the problem has nothing to do with ref/out semantics; it's a more fundamental error in reasoning.&lt;/P&gt;
&lt;P&gt;Feel free to pause and reflect for a moment if you want a chance to figure it out for yourself.&lt;/P&gt;
&lt;P&gt;...&lt;/P&gt;
&lt;P&gt;The error is in the default case:&lt;/P&gt;
&lt;HR&gt;

&lt;DIV class=spec&gt;
&lt;P&gt;First, if x has no side effects, then simply keep x in the appropriate place in A2.&lt;/P&gt;&lt;/DIV&gt;
&lt;HR&gt;

&lt;P&gt;Whether x has side effects or not is completely irrelevant; what is relevant is whether the computation of x depends on the order of every other side effect!&lt;/P&gt;
&lt;P&gt;For example, our spec would have that &lt;/P&gt;&lt;SPAN class=code&gt;
&lt;P&gt;int k = 0;&lt;BR&gt;M( k, arr[k=1] );&lt;/P&gt;&lt;/SPAN&gt;
&lt;P&gt;is the same as&lt;/P&gt;&lt;SPAN class=code&gt;
&lt;P&gt;int k = 0;&lt;BR&gt;var t1&amp;nbsp;= arr;&lt;BR&gt;var&amp;nbsp;t2 = (k = 1);&lt;BR&gt;M(k,&amp;nbsp;t1[t2]);&lt;/P&gt;&lt;/SPAN&gt;
&lt;P&gt;which is clearly not correct; the first argument&amp;nbsp;is supposed to be zero, not one. The fact that the first argument has no side effects is irrelevant; it depends on the second argument's side effect taking effect &lt;EM&gt;after&lt;/EM&gt; the first argument's &lt;EM&gt;value&lt;/EM&gt; is computed.&lt;/P&gt;
&lt;P&gt;We have to fix up the spec (and implementation) to say that if the argument corresponds to a &lt;EM&gt;value&lt;/EM&gt; parameter, you &lt;EM&gt;always&lt;/EM&gt; save away the value in a temporary, regardless of whether it has side effects or not. If it corresponds to, say, a ref parameter, and the argument is a ref to a local then we can still simply generate the ref to the local normally; regardless of whether the contents of the local change, the managed address of the local is always the same, and computing the address of a local causes no side effects.&lt;/P&gt;
&lt;P&gt;I note that we could also make an exception in the case that the value is, say, a compile-time constant. Clearly the computation of its value does not have any dependency on the ordering of other side effects. But for the sake of not further complexifying my spec and implementation, I glossed over this detail. The optimizer will probably take care of it.&lt;/P&gt;
&lt;P&gt;Commenter Pavel Minaev found several more corner cases&amp;nbsp;that none of us found. The big one is that we should be clear about what happens with multidimensional arrays, and in fact, I had a bug in my implementation for multidimensional arrays, so thanks Pavel! He also found some obscure ones. One is that accessing a static field does have a side effect; if this is the first access to the class then&amp;nbsp;that causes the static class constructor to run, which could have observable side effects.&amp;nbsp;Another is that the exact semantics of when bad arguments cause exceptions is slightly changed by this transformation.&lt;/P&gt;
&lt;P&gt;At this point I now believe that I have a correct specification of my function T (modulo Pavel's weird issues, which I'm still mulling over. But those are bizarre corner cases that might require some language spec work to sort out.) Since the implementation is a straightforward transformation of the specification into C++, I also believe I have a correct implementation. I've written test cases that verify every case in the spec, and handed it off to testing to look for practical cases I missed.&lt;/P&gt;
&lt;P&gt;Anyone see any other problems with this spec? Anyone?&lt;/P&gt;
&lt;P&gt;And anyone have any guesses as to why I needed this function implemented?&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;&lt;/DIV&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9921574" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ericlippert/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Code+Quality/default.aspx">Code Quality</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Software+development+methodology/default.aspx">Software development methodology</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Value+Types/default.aspx">Value Types</category></item><item><title>Always write a spec, part one</title><link>http://blogs.msdn.com/ericlippert/archive/2009/11/19/always-write-a-spec-part-one.aspx</link><pubDate>Thu, 19 Nov 2009 14:45:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9921545</guid><dc:creator>Eric Lippert</dc:creator><slash:comments>27</slash:comments><comments>http://blogs.msdn.com/ericlippert/comments/9921545.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ericlippert/commentrss.aspx?PostID=9921545</wfw:commentRss><description>&lt;DIV class=mine&gt;
&lt;P&gt;Joel had a great series of articles many years ago about &lt;A href="http://www.joelonsoftware.com/articles/fog0000000036.html" mce_href="http://www.joelonsoftware.com/articles/fog0000000036.html"&gt;the benefits of writing functional specifications&lt;/A&gt;, that is, specifications of how the product looks to its users. I want to talk a bit about technical specifications, that is, a specification of how something actually works behind the scenes. A while back, I described how I wrote a seemingly-trivial piece of code by &lt;A href="http://blogs.msdn.com/ericlippert/archive/2009/06/01/bug-psychology.aspx" mce_href="http://blogs.msdn.com/ericlippert/archive/2009/06/01/bug-psychology.aspx"&gt;first writing the technical spec, and then writing the code to exactly implement the spec&lt;/A&gt;, and the test cases to test each statement of the spec. I use this technique frequently; it almost always saves me time and effort. If you don't write a spec first, it's very easy to get bogged down in a morass of bugs, and hard to know when you're actually done writing the code.&lt;/P&gt;
&lt;P&gt;Here's an example of another seemingly-trivial function that &lt;A href="http://stackoverflow.com/questions/921180/c-round-up/926806#926806" mce_href="http://stackoverflow.com/questions/921180/c-round-up/926806#926806"&gt;smart people gave a good half-dozen completely wrong answers to because they didn't write a spec&lt;/A&gt;. Notice how once I state what the spec is, the code follows very naturally.&lt;/P&gt;
&lt;P&gt;I thought I might go through a recent example of a slightly less trivial problem. I needed a function in the compiler which could take a valid list of arguments A1 to an applicable method M, and give me back two lists. The first result list is a list of &lt;EM&gt;local variable declarations&lt;/EM&gt;, L, the second, another argument list, A2. Here's the kicker: evaluating each member of A2 &lt;EM&gt;must have no side effects&lt;/EM&gt;. And, the combination of executing L followed by M(A2) must give &lt;EM&gt;exactly the same results&lt;/EM&gt; as executing M(A1). (I also knew that the members of A1 had already had all the conversions generated to make them match the types of M's parameter list, and so on. Overload resolution was completely done at this point.)&lt;/P&gt;
&lt;P&gt;Why I needed such a function is not particularly important; perhaps you can speculate as to why I needed it.&lt;/P&gt;
&lt;P&gt;So I started writing a spec, starting with the description above: the function T takes a valid list... blah blah blah.&lt;/P&gt;
&lt;P&gt;That's not enough of a spec to actually write code. &lt;/P&gt;
&lt;P&gt;I realized that some expressions in A1 might have side effects, and some might not. If an expression had side effects, then I could declare a temp local, execute the side effects, assign the result to the local, and then use the local as the expression in the same place in A2.&lt;/P&gt;
&lt;P&gt;Super. Now I can write more sentences in my spec.&lt;/P&gt;
&lt;HR&gt;

&lt;DIV class=spec&gt;
&lt;P&gt;For each argument x in A1, we potentially generate a temporary value as follows: First, if x has no side effects, then simply keep x in the appropriate place in A2. Otherwise, if x does have side effects, then generate a temporary variable declaration in L, var temp = x. Then put an expression that evaluates the temporary variable in the appropriate place in A2.&lt;/P&gt;&lt;/DIV&gt;
&lt;HR&gt;

&lt;P&gt;Then I thought "can I correctly implement this spec?"&lt;/P&gt;
&lt;P&gt;I started thinking about the possible cases for the input, A1. The members of A1 could be passed by value, out, or ref; I already knew they were correct, since the particular method M was an applicable method of argument list A1. In this particular point in the compilation process, there was no need to worry about whether there were "params" arguments, whether there were missing arguments with default values, and so on; that had already been taken care of. I also noted that whether they were "ref" or "out" was irrelevant; ref and out are the same thing behind the scenes. As far as the compiler is concerned, the only difference between them is that the definite assignment rules are different for each.&amp;nbsp;So we had two cases for each argument: it's being passed by value, or by ref/out. &lt;/P&gt;
&lt;P&gt;If it's being passed by ref/out, then what we actually do is we pass&amp;nbsp;the managed address of&amp;nbsp;a variable to the method. In M(ref int x), the type of x is actually a special type int&amp;amp;, "managed reference to variable of type int", which is not a legal type in C#. That's why we hide it from you by requiring a goofy "ref" syntax any time you want to talk about a managed reference to a variable.&lt;/P&gt;
&lt;P&gt;Unfortunately, our IL generation pass was written with the assumption that all local variables are never of these magic managed-reference-to-variable types. I was then faced with a choice. Either come up with a technique for working with this limitation, or rewrite the most complicated code in the IL gen to support this scenario. (The code which figures out how to deal with managed references is quite complex.) I decided to opt for the former strategy. Which meant more spec work, since our spec now doesn't handle these cases!&lt;/P&gt;
&lt;HR&gt;

&lt;DIV class=spec&gt;
&lt;P&gt;For each argument x in A1, we potentially generate a temporary value as follows: &lt;/P&gt;
&lt;P&gt;First, if x has no side effects, then simply keep x in the appropriate place in A2. &lt;/P&gt;
&lt;P&gt;Otherwise, if x does have side effects &lt;STRONG&gt;and corresponds to a value parameter&lt;/STRONG&gt;, then generate ... etc.&lt;/P&gt;
&lt;P&gt;Otherwise, if x does have side effects and corresponds to a ref/out parameter, &lt;STRONG&gt;then a miracle happens&lt;/STRONG&gt;.&lt;/P&gt;&lt;/DIV&gt;
&lt;HR&gt;

&lt;P&gt;Clearly that last bit needed some work.&lt;/P&gt;
&lt;P&gt;So I wrote down all the possible cases I could think of. Clearly x has to be a variable if its type is "reference to variable", so that makes for a small number of cases:&lt;/P&gt;
&lt;HR&gt;

&lt;DIV class=spec&gt;
&lt;P&gt;Otherwise, if x does have side effects and corresponds to a ref/out parameter, then the possible cases are:&lt;/P&gt;
&lt;P&gt;* x is a local variable&lt;BR&gt;* x is a value parameter&lt;BR&gt;* x is an out parameter&lt;BR&gt;* x is a ref parameter&lt;BR&gt;* x is a field of an instance&lt;BR&gt;* x is a static field&lt;BR&gt;* x is an array element&lt;BR&gt;* x is a pointer dereferenced with *&lt;BR&gt;* x is a pointer dereferenced with [ ]&lt;/P&gt;
&lt;P&gt;and for each, a miracle happens.&lt;/P&gt;&lt;/DIV&gt;
&lt;HR&gt;

&lt;P&gt;Again, clearly this is not good enough. I re-examined my list to see if I could eliminate any of these cases. Locals, parameters and&amp;nbsp;static fields never have side effects, so we can eliminate them.&lt;/P&gt;
&lt;P&gt;Also, at this point in our analysis, I knew that pointer dereferences of the form "pointer[index]" had already been rewritten into the form "*(pointer + index)", which meant that in practice, we would never actually hit that last case in this algorithm; the second-last case would take care of both.&lt;/P&gt;
&lt;HR&gt;

&lt;DIV class=spec&gt;
&lt;P&gt;Otherwise, if x does have side effects and corresponds to a ref/out parameter, then the possible cases are:&lt;/P&gt;
&lt;P&gt;* x is a field of an instance&lt;BR&gt;* x is an array element&lt;BR&gt;* x is a pointer dereferenced with *&lt;BR&gt;&lt;BR&gt;and for each, a miracle happens.&lt;/P&gt;&lt;/DIV&gt;
&lt;HR&gt;

&lt;P&gt;I then started to think of what side effects could happen for each.&amp;nbsp;We could have "ref instance.field", "ref array[index]", or "ref *pointer", and&amp;nbsp;"instance", "array", "index" and "pointer" can all be expressions that have a&amp;nbsp;side effect. ("field" cannot, it merely names a field.) So now we can use the same specification as before:&lt;/P&gt;
&lt;HR&gt;

&lt;P class=spec&gt;Otherwise, if x does have side effects and corresponds to a ref/out parameter, then the possible cases are:&lt;/P&gt;
&lt;DIV class=spec&gt;
&lt;UL&gt;
&lt;LI&gt;x is ref/out instance.field: in this case,&amp;nbsp;add var temp=instance&amp;nbsp;to&amp;nbsp;L and ref/out temp.field to A2.&lt;/LI&gt;
&lt;LI&gt;x is ref/out array[index]: in this case, add var t1 = array and&amp;nbsp;var t2 = index to L and ref/out t1[t2] to A2.&lt;/LI&gt;
&lt;LI&gt;x is ref/out *pointer: in this case, add var temp = pointer to L and ref/out *temp to A2.&lt;/LI&gt;&lt;/UL&gt;&lt;/DIV&gt;
&lt;HR&gt;

&lt;P&gt;And now we have something I can implement. So I sent this proposed spec around for review, while I started plugging away at the implementation.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;This spec is wrong.&lt;/STRONG&gt; Can you spot the&amp;nbsp;bugs in my implementation&amp;nbsp;that my coworkers found by reading my spec?&lt;/P&gt;
&lt;P&gt;Next time, the thrilling conclusion.&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;&lt;/DIV&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9921545" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ericlippert/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Puzzles/default.aspx">Puzzles</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Code+Quality/default.aspx">Code Quality</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Software+development+methodology/default.aspx">Software development methodology</category></item><item><title>Events and Races</title><link>http://blogs.msdn.com/ericlippert/archive/2009/04/29/events-and-races.aspx</link><pubDate>Wed, 29 Apr 2009 15:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9571319</guid><dc:creator>Eric Lippert</dc:creator><slash:comments>22</slash:comments><comments>http://blogs.msdn.com/ericlippert/comments/9571319.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ericlippert/commentrss.aspx?PostID=9571319</wfw:commentRss><description>&lt;DIV class=mine&gt;
&lt;P&gt;Here’s a question &lt;A href="http://stackoverflow.com/questions/786383/c-events-and-thread-safety" mce_href="http://stackoverflow.com/questions/786383/c-events-and-thread-safety"&gt;similar&lt;/A&gt; to one I saw on &lt;A href="http://www.stackoverflow.com/" mce_href="http://www.stackoverflow.com"&gt;stackoverflow&lt;/A&gt; the other day. Suppose you have an event:&lt;/P&gt;&lt;SPAN class=code&gt;
&lt;P&gt;public event Action Foo; &lt;/SPAN&gt;
&lt;P&gt;The standard pattern for firing this event is: &lt;SPAN class=code&gt;
&lt;P&gt;Action temp = Foo;&lt;BR&gt;if (temp != null)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; temp();&lt;/P&gt;&lt;/SPAN&gt;
&lt;P&gt;What the heck is up with that? Why not just call “&lt;SPAN class=code&gt;Foo()&lt;/SPAN&gt;” ? &lt;/P&gt;
&lt;P&gt;First off, this pattern ensures that the thing that is invoked is not the null delegate reference, which would cause a null reference exception to be thrown. But if that’s what we want, then surely we could have skipped the temporary variable – just “&lt;SPAN class=code&gt;if (Foo != null) Foo();&lt;/SPAN&gt;” would do. Why the temp?&lt;/P&gt;
&lt;P&gt;The temporary variable ensures that this is “thread safe”. If the event’s handlers are being modified on another thread it is possible for Foo to be non-null at the point of the check, then set to null on a different thread, and then the invocation throws.&lt;/P&gt;
&lt;P&gt;Using the temporary variable effectively makes a copy of the current set of event handlers. Remember, &lt;STRONG&gt;multi-cast delegates are immutable&lt;/STRONG&gt;; when you add or remove a handler, you &lt;EM&gt;replace&lt;/EM&gt; the existing multi-cast delegate object with a &lt;EM&gt;different&lt;/EM&gt; delegate object that has different behaviour. You do not modify an existing &lt;EM&gt;object&lt;/EM&gt;, you modify the &lt;EM&gt;variable&lt;/EM&gt; that stores the event handler. Therefore, stashing away the current reference stored in that variable into a temporary variable effectively makes a copy of the current state.&lt;/P&gt;
&lt;P&gt;Is that clear? Make sure it is, because now things start to get really confusing.&lt;/P&gt;
&lt;P&gt;A common criticism of this pattern is that it trades one race condition for another. Let’s consider that scenario again more carefully:&lt;/P&gt;
&lt;P&gt;The event handler contains a single handler, H. Thread Alpha makes a copy of the delegate to H in temp and determines that it is not null. Thread Beta decides that H must no longer be called when the event is fired, so it sets the handler to null. Thread Beta then assumes that H will never be called, and destroys a bunch of state that H needs to execute successfully. Thread Alpha then regains control and executes H, which behaves crazily since its necessary state has been destroyed. Thread Beta’s attempt to ensure that H is not called has been defeated by the race condition.&lt;/P&gt;
&lt;P&gt;A frequently-stated principle of good software design is that code which calls attention to bugs by throwing exceptions is better than code which hides bugs by muddling on through and doing the wrong thing. This code has a race condition that results in incorrect behaviour. Perhaps an application of that principle is to stop using a temporary and instead crash if it races. That is to say, this code has a failure mode; surely it is better to highlight that failure with a crisp exception than to turn it into crazy wrong behaviour.&lt;/P&gt;
&lt;P&gt;That seems plausibly argued, I agree, but the conclusion is wrong. &lt;/P&gt;
&lt;P&gt;Suppose we remove the temporary variable but keep the null check. Does that &lt;EM&gt;solve&lt;/EM&gt; the problem? No! We still have the same race conditions. Suppose the event handler delegate contains a reference to H. Thread Alpha checks to see whether it is null; it is not. Thread Alpha pushes the object to invoke on the runtime stack. &lt;EM&gt;Between the push of the delegate value and the call to invoke it, thread Beta sets the event handler to null.&lt;/EM&gt; And once again, H will be invoked after thread Beta has removed the handler.&lt;/P&gt;
&lt;P&gt;Suppose we remove the temporary &lt;EM&gt;and&lt;/EM&gt; the null check and just invoke the delegate directly. Does that help? No, we &lt;EM&gt;still&lt;/EM&gt; have the same race condition. The contents of the event handling variable can still be changed between the push of the delegate object onto the stack and the invocation of the delegate.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Removing the code around the call site does not decrease the number of race conditions in the code, and it does increase the number of crashes. Worse, doing so makes the race condition harder to detect by shrinking the window in which the race can occur without eliminating it. &lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;Removing the null check and/or the temporary is a bad idea; the already-bad situation is only made worse.&lt;/P&gt;
&lt;P&gt;Essentially there are two problems here that are being conflated. The two problems are:&lt;/P&gt;
&lt;P&gt;1) The event handler delegate can be null at any time.&lt;BR&gt;2) A “stale” handler can be invoked even after it has been removed.&lt;/P&gt;
&lt;P&gt;These two problems are actually orthogonal and have different solutions. The onus for solving the first problem is laid upon the &lt;EM&gt;code which does the invocation&lt;/EM&gt;; it is required to ensure that it does not dereference null. The store-in-temporary-and-test pattern ensures that null is never dereferenced. (There are other ways to solve this problem; for example, initializing the handler to have an empty action that is never removed. But doing a null check is the standard pattern.)&lt;/P&gt;
&lt;P&gt;The onus for solving the second problem is laid upon &lt;EM&gt;the code being invoked&lt;/EM&gt;; &lt;STRONG&gt;event handlers are required to be robust in the face of being called even after the event has been unsubscribed.&lt;/STRONG&gt; In the scenario I described, &lt;STRONG&gt;the bug is actually in H&lt;/STRONG&gt;. It needs to be robust enough to check to see whether the state it needs is still there, and bail out cleanly if it is not. (Or, alternatively,&amp;nbsp;some additional locking mechanism needs to be implemented which ensures that the&amp;nbsp;code which fires the event cooperates with the code that changes the event handlers to ensure&amp;nbsp;the desired behaviour.)&lt;/P&gt;
&lt;P&gt;The point though is that the "null ref problem" and the "stale handler problem"&amp;nbsp;are two&amp;nbsp;separate problems that are easily confused because the symptoms of both arise at the exact same call site code. You've got to solve &lt;EM&gt;both&lt;/EM&gt; problems if you want to do threadsafe events. How you do so is up to you; just don't confuse the solution of one problem&amp;nbsp;for a solution of&amp;nbsp;the other.&lt;/P&gt;
&lt;P&gt;(Thanks to Microsofties Levi Broderick, Chris Burrows, Curt Hagenlocher and Wolf Logan; this article is based on a conversation about their analysis of this pattern.)&lt;/P&gt;&lt;/DIV&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9571319" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ericlippert/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Code+Quality/default.aspx">Code Quality</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Threading/default.aspx">Threading</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Immutability/default.aspx">Immutability</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Metablogging/default.aspx">Metablogging</category></item><item><title>Good Names</title><link>http://blogs.msdn.com/ericlippert/archive/2009/04/06/good-names.aspx</link><pubDate>Mon, 06 Apr 2009 19:30:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9519486</guid><dc:creator>Eric Lippert</dc:creator><slash:comments>15</slash:comments><comments>http://blogs.msdn.com/ericlippert/comments/9519486.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ericlippert/commentrss.aspx?PostID=9519486</wfw:commentRss><description>&lt;div class="mine"&gt; &lt;p&gt;Imagine a door with an unusual handle. The handle is five feet off the ground and rotates upwards to open the door. The door has no lock. Is this a good door design?&lt;/p&gt; &lt;p&gt;Sorry. That’s not an answerable question. The purpose of almost every door is to prevent &lt;em&gt;something&lt;/em&gt; from going through it without preventing &lt;em&gt;everything&lt;/em&gt; from going through it. Some doors exist only to mitigate heat loss while allowing anything else through. Some doors exist to keep everyone out except authorized personnel. The goodness or badness of a door design depends entirely on how well or poorly it performs the task of preventing undesired access and allowing desired access.&lt;/p&gt; &lt;p&gt;If I were describing a jail cell door or bank vault door, that would be an absurdly bad design. But for the teacher’s supply room in a kindergarden classroom, it’s pretty good. It doesn’t prevent access to any adults, but the kids are unlikely to be able to get through without adult supervision. &lt;strong&gt;Purpose matters.&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;Last time I talked a bit about what makes a book title better or worse. These ideas can be extended to naming just about anything, but it is important when considering the goodness or badness of a name to keep in mind the purpose of the name. Frequently when we name types and methods, it’s like my book example. The purpose of the code, like the purpose of the book, is to provide a benefit to the consumer. The purpose of the name is usually (*) to make it easy for the potential consumer to &lt;strong&gt;find&lt;/strong&gt; the thing, and then quickly and accurately &lt;strong&gt;evaluate&lt;/strong&gt; whether it is likely to provide the desired benefit. &lt;/p&gt; &lt;p&gt;With those purposes in mind, here are some guidelines that I use when trying to come up with a public name for something:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;Everything in my &lt;a href="http://blogs.msdn.com/ericlippert/archive/2007/06/12/bad-names.aspx"&gt;&lt;strong&gt;Bad Names&lt;/strong&gt;&lt;/a&gt; post.&lt;br&gt;&lt;/li&gt; &lt;li&gt;The name should describe what the thing &lt;em&gt;is&lt;/em&gt; (classes) or &lt;em&gt;does&lt;/em&gt; (methods) or is &lt;em&gt;used for&lt;/em&gt; (interfaces), as opposed to &lt;em&gt;how&lt;/em&gt; it achieves any of those things.&lt;br&gt;&lt;/li&gt; &lt;li&gt;Names should describe the &lt;strong&gt;unchanging&lt;/strong&gt; aspects of the nature of the thing.&lt;br&gt;&lt;br&gt;A small example of how I got this wrong recently: the style that I use for adding responses to comments in this blog is called “yellowbox”. If I ever decide to change it the look of the blog and want to make it blue, I’m going to look pretty silly having a style called “yellowbox” that draws a blue box. I should have called it “response”; it’s always going to be for responses.&lt;br&gt;&lt;/li&gt; &lt;li&gt;The name should use the &lt;strong&gt;vocabulary of the consumer&lt;/strong&gt;, not the &lt;strong&gt;jargon of the mechanisms&lt;/strong&gt; used in the implementation. &lt;br&gt;&lt;/li&gt; &lt;li&gt;The name should be as &lt;strong&gt;unique&lt;/strong&gt; as possible, so that searching for the words in it rapidly narrows the field down. &lt;br&gt;&lt;/li&gt; &lt;li&gt;The name should be as &lt;strong&gt;precise&lt;/strong&gt; as possible. &lt;br&gt;&lt;br&gt;How many “HandleSomething” methods have you seen? Usually these do something a lot more specific than “handling”. It might be important to the consumer to have a better idea of what you’re doing in there.&lt;br&gt;&lt;/li&gt; &lt;li&gt;The name should not have any &lt;strong&gt;non-standard abbreviations&lt;/strong&gt;; FindCustRec is unlikely to be found by searching for “customer” or “record”. &lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Those are just a few off the top of my head. What are some of the criteria you use to come up with good names for types and methods?&lt;/p&gt; &lt;p&gt;**************************&lt;/p&gt; &lt;p&gt;(*) There are unusual cases where names are deliberately chosen to work &lt;em&gt;against&lt;/em&gt; those typical goals. Sometimes code is very special-purpose, designed to be used very rarely and then only by subject experts. In those cases, you do not want the code to be accidentally found by people who are unlikely to need it, and unlikely to use it correctly. In those cases, it’s desirable for the name to be laden with the jargon of the experts who will be using it. Doing so sends the message to potential users “if you do not know what these words mean, you probably should not be using this code”. Again, purpose matters.&lt;/p&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9519486" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Code+Quality/default.aspx">Code Quality</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Software+development+methodology/default.aspx">Software development methodology</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/naming/default.aspx">naming</category></item><item><title>Every Problem Looks Like A Nail</title><link>http://blogs.msdn.com/ericlippert/archive/2009/03/30/every-problem-looks-like-a-nail.aspx</link><pubDate>Mon, 30 Mar 2009 18:10:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9504535</guid><dc:creator>Eric Lippert</dc:creator><slash:comments>14</slash:comments><comments>http://blogs.msdn.com/ericlippert/comments/9504535.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ericlippert/commentrss.aspx?PostID=9504535</wfw:commentRss><description>&lt;DIV class=mine&gt;
&lt;P&gt;I wish all the questions I got were this straightforward:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;FONT color=#000000&gt;“I need to compare two strings for non-culture-sensitive equality. I notice that there are methods String.Equals and String.Compare which can both do that. What is the guideline on which one I should use?”&lt;/FONT&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I’ll answer your question, but first, a funny story (*). &lt;/P&gt;
&lt;P&gt;&lt;A href="http://www.hammersource.com/Specialty_Trades2.html" mce_href="http://www.hammersource.com/Specialty_Trades2.html"&gt;&lt;IMG title="Roofing Hammer" style="BORDER-RIGHT: 0px; BORDER-TOP: 0px; DISPLAY: inline; MARGIN: 0px 10px 0px 0px; BORDER-LEFT: 0px; BORDER-BOTTOM: 0px" height=240 alt="Roofing Hammer" src="http://blogs.msdn.com/blogfiles/ericlippert/WindowsLiveWriter/EveryProblemLooksLikeANail_7B33/Roofing%20Hammer_6.gif" width=107 align=left border=0 mce_src="http://blogs.msdn.com/blogfiles/ericlippert/WindowsLiveWriter/EveryProblemLooksLikeANail_7B33/Roofing%20Hammer_6.gif"&gt;&lt;/A&gt;My buddy Steve and I were fixing the flashing that is leaking around the seal between my roof and my chimney the other day. “Steve,” I said, “hand me that screwdriver.” Steve handed me his roofing hammer. “No, dude, the &lt;EM&gt;screwdriver&lt;/EM&gt;. Thin circular shaft, handle at one end, Robertson bit on the other.”&lt;/P&gt;
&lt;P&gt;“Dude, if you wanted the &lt;EM&gt;screw remover&lt;/EM&gt; you should have said so.”&lt;/P&gt;
&lt;P&gt;Ha ha ha ha ha, I crack myself up. That joke never gets old.&lt;/P&gt;
&lt;P&gt;Anyway, I seem to have digressed slightly from the topic of today’s blog. &lt;/P&gt;
&lt;P&gt;The documentation in MSDN clearly states that the purpose of String.Equals is &lt;STRONG&gt;for comparing strings for equality&lt;/STRONG&gt;, and the purpose of String.Compare is for &lt;STRONG&gt;sorting a list of strings into a culture-sensitive alphabetical order&lt;/STRONG&gt;. &lt;/P&gt;
&lt;P&gt;You can use a hammer to drive screws, and in a world without screwdrivers, you might have to. But in a world with screwdrivers, why would you? You would use the tool that was designed for the task.&lt;/P&gt;
&lt;P&gt;You can use String.Compare to test for equality if you want, but why would you? You have a method that is designed to do exactly and specifically what you want to do, so use it.&lt;/P&gt;
&lt;P&gt;Unless there is a really compelling reason to do otherwise, &lt;STRONG&gt;always use the tool that was specifically designed for your problem&lt;/STRONG&gt;, if there is one. Don’t use some other tool designed for a different task that just happens to work; that’s a recipe for bugs.&lt;/P&gt;
&lt;P&gt;*******************&lt;/P&gt;
&lt;P&gt;(*) &lt;A href="http://www.netfunny.com/rhf/jokes/92q3/scremov.html" mce_href="http://www.netfunny.com/rhf/jokes/92q3/scremov.html"&gt;Not a true story&lt;/A&gt;, except for the part about my roof leaking at the flashing. I need to fix that.&lt;/P&gt;&lt;/DIV&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9504535" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Code+Quality/default.aspx">Code Quality</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/bad+jokes/default.aspx">bad jokes</category></item><item><title>Santalic tailfans, part two</title><link>http://blogs.msdn.com/ericlippert/archive/2009/02/06/santalic-tailfans-part-two.aspx</link><pubDate>Fri, 06 Feb 2009 21:19:31 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9402662</guid><dc:creator>Eric Lippert</dc:creator><slash:comments>36</slash:comments><comments>http://blogs.msdn.com/ericlippert/comments/9402662.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ericlippert/commentrss.aspx?PostID=9402662</wfw:commentRss><description>&lt;div class="mine"&gt; &lt;p&gt;As I have said before &lt;a href="http://blogs.msdn.com/ericlippert/archive/tags/Performance/default.aspx"&gt;many times&lt;/a&gt;, there is &lt;a href="http://blogs.msdn.com/ericlippert/archive/2003/10/17/53237.aspx"&gt;only one sensible way&lt;/a&gt; to make a performant application. (As an aside: perfectly good word, performant, deal with it!) That is:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;Set meaningful, measurable, customer-focused goals.  &lt;li&gt;Write the code to be as clear and correct as possible.  &lt;li&gt;Carefully measure your performance against your goals.  &lt;li&gt;Did you meet your goal? Great! Don't waste any time on performance analysis. Spend your valuable time on features, documentation, bug fixing, robustness, security, whatever.  &lt;li&gt;If you did not meet your goal, use tools to discover what the worst-performing fixable thing is, and fix it.&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Iterate on that process, updating your goals if it proves necessary, until you either have something that meets your goals or you give up.&lt;/p&gt; &lt;p&gt;My explicit goal for my &lt;a href="http://blogs.msdn.com/ericlippert/archive/2009/02/04/a-nasality-talisman-for-the-sultana-analyst.aspx"&gt;little dictionary search program&lt;/a&gt; was that it give results fast enough that I would not be sitting there impatiently waiting for more than a few seconds. That's a very customer-focused goal, me being the only customer of this program. With only a very small amount of tweaking my program met that goal right away, so why would I spend any more time on it? The program takes just under two seconds to report the results for a typical query, which is faster than I can read.&lt;/p&gt; &lt;p&gt;But suppose that we did want to improve the performance of this program for some reason. How? Well, let's go down the list. We have goals, we have very clear code, we can measure the performance easily enough. Suppose we didn't meet the goal. The last thing on the list is "use tools to discover what the slowest fixable thing is".&lt;/p&gt; &lt;p&gt;A commenter conjectured that the performance bottleneck of this program was in the disk I/O. As you can see, every time we do a query we re-read the two megabyte dictionary file dozens of times. This has the benefit of using very little memory; we never have more than one line of the dictionary in memory at a time, instead of the whole four megs it would take to store the dictionary (remember, the dictionary is in ASCII on disk but two-byte Unicode if in strings in memory, so the in-memory size will double.)&lt;/p&gt; &lt;p&gt;That's a conjecture -- a reasonable conjecture -- but nevertheless, it's just a guess. If I've learned one thing about performance analysis it's that my guesses about where the bottleneck is are often wrong. I'll tell you right now that yes, the disk-hitting performance is bad, but it is not the worst thing in this program, not by far. &lt;strong&gt;Where's the real performance bottleneck? Any guesses? Could you know without using tools?&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;Here's the result of a timing profile run of a seven-letter queries with one blank, repeated 20 times:&lt;/p&gt; &lt;p&gt;&lt;strong&gt;43%: Contains&lt;br&gt;&lt;/strong&gt;21%: ReadLine&lt;br&gt;14%: Sort&lt;br&gt;7%: ToUpperInvariant&lt;br&gt;15%: everything else&lt;/p&gt; &lt;p&gt;Holy goodness! The call to the Contains extension method in the query to test whether the dictionary word is in the rack set array is almost half the cost of this program! &lt;/p&gt; &lt;p&gt;Which makes sense, once you stop to think about it. The "Contains" method is by its highly general nature necessarily very naive. When given an array, 99.9% of the time it has to look through the entire 26-item array because 99.9% of the time, the word is not actually going to match any of the possible racks. It cannot take advantage of any "early outs" like you could do if you were doing a linear search on a sorted list. And each time through the array it has to do a full-on string comparison; there's no fancy-pants checks in there that take advantage of string immutability or hash codes or any such thing.&lt;/p&gt; &lt;p&gt;We have a data structure that is designed to rapidly tell you whether a member is contained in a set. And even better, it already does the "distinct" logic. When I replace&lt;/p&gt;&lt;span class="code"&gt; &lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; var racks = (from rack in ReplaceQuestionMarks(originalRack)&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; select Canonicalize(rack)).Distinct().ToArray(); &lt;/span&gt; &lt;p&gt;with&lt;/p&gt;&lt;span class="code"&gt; &lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; var racks = new HashSet&amp;lt;string&amp;gt;(&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; from rack in ReplaceQuestionMarks(originalRack)&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; select Canonicalize(rack)); &lt;/span&gt; &lt;p&gt;suddenly performance improves massively. The "Contains" drops down to 3% of the total cost, and of course, the total cost is now half of what it was before.&lt;/p&gt; &lt;p&gt;Another subtle point here: notice how when I changed the type of the variable "racks" from "array of string" to "set of string", I didn't have to redundantly change the type thanks to implicit typing of local variables. I want to emphasize the semantics here, not the storage mechanism. If I felt that communicating the storage mechanism was an important part of this code -- because it has such a strong effect on performance -- perhaps I would choose to emphasize the storage by eschewing the "var".&lt;/p&gt; &lt;p&gt;With this change, the program performance improves to about one second per query and the profile now looks like this:&lt;/p&gt; &lt;p&gt;39%: ReadLine&lt;br&gt;23%: Sort&lt;br&gt;11%: ToUpperInvariant&lt;br&gt;7%: the iterator block goo in FileLines&lt;br&gt;5%: ToArray (called by Join)&lt;br&gt;15%: everything else&lt;/p&gt; &lt;p&gt;Now the bottleneck is clearly the combination of repeated file line reading (48%) and the string canonicalization of every dictionary line over and over again (37%). &lt;/p&gt; &lt;p&gt;With this information, we now have data with which to make sensible investments of time and effort. We could cache portions of the dictionary in memory to avoid the repeated disk cost. Is the potential increase in speed worth the potentially massive increase in memory usage? We could be smart about it and, say, only cache the seven- and eight-letter words in memory.&lt;/p&gt; &lt;p&gt;We could also attack the canonicalization performance problem. Should we perhaps precompute an index for the dictionary where every string is already in canonical form? This in effect trades increased disk space, increased program complexity and increased redundancy for decreased time. Should we use a different canonicalization algorithm entirely?&lt;/p&gt; &lt;p&gt;All of these decisions are driven by the fact that I have already exceeded my performance goal, so the answer is "no". Good enough is, by definition, good enough. If I were using this algorithm to actually build a game AI, it would not be good enough anymore and I'd go with some more clever solution. But I'm not. &lt;/p&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9402662" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Performance/default.aspx">Performance</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Code+Quality/default.aspx">Code Quality</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Scrabble/default.aspx">Scrabble</category></item><item><title>Properties vs. Attributes</title><link>http://blogs.msdn.com/ericlippert/archive/2009/02/02/properties-vs-attributes.aspx</link><pubDate>Tue, 03 Feb 2009 00:02:26 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9391497</guid><dc:creator>Eric Lippert</dc:creator><slash:comments>11</slash:comments><comments>http://blogs.msdn.com/ericlippert/comments/9391497.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ericlippert/commentrss.aspx?PostID=9391497</wfw:commentRss><description>&lt;div class="mine"&gt; &lt;p&gt;Here is yet another question I got from a C# user recently:&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;&lt;font color="#000080"&gt;I have a class that represents a business rule. I want to add some rule metadata that could be used by consumers to retrieve a friendlier rule name, description, and anything else that makes sense. Should this information be exposed as an &lt;em&gt;attribute&lt;/em&gt; or &lt;em&gt;property&lt;/em&gt; on the class?&lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;I would say to absolutely go for a property in this case, for four reasons.  &lt;p&gt;First, properties are highly discoverable. Consumers of this class can use IntelliSense to see that there is a property on the class called "Description" much more easily than they can see that there is an attribute. &lt;p&gt;Second, properties are much easier to use than attributes. You don't want to muck around with the code to extract strings from metadata attributes unless you really have to.  &lt;p&gt;Third, data such as names and descriptions is highly likely to be localized in the future. Making it a property means that the property getter code can read the string out of a resource, a resource which you can hand off to your localization experts when it comes time to ship the Japanese version.  &lt;p&gt;And fourth, let's go back to basic object-oriented design principles. You are attempting to model something -- in this case, the class is modeling a "business rule".  &lt;p&gt;Note that &lt;em&gt;a business rule is not a class&lt;/em&gt;. Nor is a rule an interface. A rule is neither a property nor a method. A rule isn't &lt;em&gt;any&lt;/em&gt; programming language construct. A rule is a &lt;em&gt;rule&lt;/em&gt;; classes and structs and interfaces and whatnot are &lt;em&gt;mechanisms&lt;/em&gt; that we use to &lt;em&gt;implement&lt;/em&gt; model elements that &lt;em&gt;represent&lt;/em&gt; the desired semantics in a manner that we as software developers find &lt;em&gt;amenable to our tools&lt;/em&gt;.&amp;nbsp; &lt;strong&gt;But let's be careful to not confuse the thing being modeled with the mechanisms we use to model it.&lt;/strong&gt;  &lt;p&gt;Properties and fields and interfaces and classes and whatnot are part of the model; each one of those things should represent something in the model world. If a property of a "rule" is its "description" then there should be something &lt;em&gt;in the model&lt;/em&gt; that you're implementing which represents this. We have invented properties specifically to model the "an x has the property y" relationship, so use them.  &lt;p&gt;That's not at all what attributes are for. Think about a typical usage of attributes: &lt;span class="code"&gt; &lt;p&gt;[Obsolete]&lt;br&gt;[Serializable]&lt;br&gt;public class Giraffe : Animal &lt;br&gt;{ ... &lt;/span&gt; &lt;p&gt;Attributes typically do not have anything to do with the semantics of the thing being modeled. Attributes are &lt;strong&gt;facts about the mechanisms&lt;/strong&gt; - the classes and fields and formal parameters and whatnot. Clearly this does not mean "a giraffe &lt;em&gt;has an&lt;/em&gt; obsolete and a serializable." This also does not mean that giraffes are obsolete or serializable. That doesn't make any sense. This says that &lt;em&gt;the class&lt;/em&gt; named Giraffe is obsolete and &lt;em&gt;the class&lt;/em&gt; named Giraffe is serializable.  &lt;p&gt;In short: &lt;strong&gt;use attributes to describe your mechanisms, use properties to model the domain.&lt;/strong&gt;&lt;/p&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9391497" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ericlippert/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Code+Quality/default.aspx">Code Quality</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/localization/default.aspx">localization</category></item><item><title>Future-Proofing A Design</title><link>http://blogs.msdn.com/ericlippert/archive/2009/01/16/future-proofing-a-design.aspx</link><pubDate>Fri, 16 Jan 2009 20:21:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9308334</guid><dc:creator>Eric Lippert</dc:creator><slash:comments>9</slash:comments><comments>http://blogs.msdn.com/ericlippert/comments/9308334.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ericlippert/commentrss.aspx?PostID=9308334</wfw:commentRss><description>&lt;DIV class=mine&gt;
&lt;P&gt;Last time on FAIC a user asked for guidance on the potential pitfalls of refactoring an automatic property into a regular explicit property. This is just an example of a far more general problem: how can we design programs so that they are easy to get right when things inevitably change in the future?&lt;/P&gt;
&lt;P&gt;This is an incredibly difficult question to answer, one which whole books could be written on. Today, I'll just give three general points to think about.&lt;/P&gt;
&lt;P&gt;First: &lt;U&gt;Premature generality is expensive.&lt;/U&gt; 
&lt;P&gt;&lt;B&gt;&lt;/B&gt;
&lt;P&gt;Designing code to be future-proof without a clear understanding of what the future holds often leads to excess generality; generality has very real costs, and they might be higher than the benefit accrued. 
&lt;P&gt;If you do a good job of solving the problem at hand with the tools at hand, if the code is clean and organized and has few moving parts, then it will be easier to generalize it in the future if you need to in order to solve a problem then. 
&lt;P&gt;&lt;STRONG&gt;Design your code to solve the well-understood problem.&lt;/STRONG&gt; If you design it to solve an unknown future problem, odds are good that you're going to solve it poorly, making even more work for the future. 
&lt;P&gt;Second: &lt;U&gt;Represent in your &lt;I&gt;model&lt;/I&gt; only those things which are &lt;I&gt;always&lt;/I&gt; in the problem domain and whose class relationships are &lt;I&gt;unchanging&lt;/I&gt;.&lt;/U&gt; 
&lt;P&gt;The relationships between the classes TrafficLight, TrafficSign, Vehicle, Car, Truck, Pedestrian, Roadway, Intersection, SignalTiming and RightOfWayLaw are likely to be eternal and unchanging. 
&lt;P&gt;Things change. The &lt;I&gt;semantics&lt;/I&gt; of a particular RightOfWayLaw might change subtly or grossly as the city council does its job. There might be &lt;EM&gt;new subclasses&lt;/EM&gt; of Vehicle created in the future. A specific traffic light might have a &lt;EM&gt;faulty implementation&lt;/EM&gt; that the rest of the system needs to handle gracefully. But each of these situations is about changing the implementation details of a class, never changing its &lt;EM&gt;relationships&lt;/EM&gt; with other classes. 
&lt;P&gt;If the class &lt;I&gt;relationships&lt;/I&gt; are future-proofed then it is a lot easier to edit the &lt;I&gt;implementations&lt;/I&gt; of those classes without having to worry that the whole system will thereby get messed up. 
&lt;P&gt;The easiest way to keep the relationships straight is to &lt;STRONG&gt;base them as much as possible on concepts directly from the problem domain&lt;/STRONG&gt;. 
&lt;P&gt;Third: &lt;U&gt;Keep your policies away from your mechanisms.&lt;/U&gt; 
&lt;P&gt;The &lt;EM&gt;mechanism&lt;/EM&gt; of a traffic light works the same way no matter what &lt;EM&gt;policies&lt;/EM&gt; like light timings at rush hour are.&amp;nbsp; If mechanisms (TrafficLight) are separated from policies (SignalTiming) then you can change the mechanism to a more efficient one without worrying that you’re going to inadvertently change policy, and can change policy without worrying that you’re going to break mechanism. 
&lt;P&gt;My earlier example of a bank balance was deliberately an example of what goes wrong when mechanism and policy become conflated. The “Balance” getter was originally a &lt;EM&gt;mechanism&lt;/EM&gt;, but after the edit, it became a &lt;EM&gt;security policy enforcement tool&lt;/EM&gt;. 
&lt;P&gt;You must then ask yourself, “where in this code do I care about &lt;EM&gt;enforcing&lt;/EM&gt; the policy, and where do I care about &lt;EM&gt;executing&lt;/EM&gt; the mechanism?” and make the appropriate edits. After the edit, everywhere that needs policy enforcement needs to use “Balance”, everywhere that needs to effect the action of the mechanism needs to use “balance”.&amp;nbsp; 
&lt;P&gt;What are the odds that there is going to be a bug introduced, given that we now have a difference which is important and almost invisible? Pretty high! This difference should probably be made more visible by renaming the backing store to something that calls it out as semantically different from the property accessor. 
&lt;P&gt;Let's bring this back to the question at hand. What guidance do we propose for future-proofing turning automatic properties into regular properties?&amp;nbsp; The guidance that I am proposing here is: 
&lt;UL&gt;
&lt;LI&gt;First, think about whether making the design general now to solve an unknown problem that you might not even have in the future is worthwhile.&lt;/LI&gt;
&lt;LI&gt;Second, make sure that the design in general makes the relationships between classes unchanging, even if the specifics of each class change.&lt;/LI&gt;
&lt;LI&gt;Third, consider whether that hypothetical future change to the property will be splitting apart its role as a mechanism from its role as an enforcer of policy; if this is going to be an important distinction, then consider getting that distinction into the class implementation early so that you don’t have to retrofit it in a tedious and error-prone manner later.&lt;/LI&gt;&lt;/UL&gt;&lt;/DIV&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9308334" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ericlippert/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Breaking+Changes/default.aspx">Breaking Changes</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Code+Quality/default.aspx">Code Quality</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Software+development+methodology/default.aspx">Software development methodology</category></item><item><title>Automatic vs Explicit Properties</title><link>http://blogs.msdn.com/ericlippert/archive/2009/01/14/automatic-vs-explicit-properties.aspx</link><pubDate>Wed, 14 Jan 2009 20:11:25 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9308211</guid><dc:creator>Eric Lippert</dc:creator><slash:comments>16</slash:comments><comments>http://blogs.msdn.com/ericlippert/comments/9308211.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ericlippert/commentrss.aspx?PostID=9308211</wfw:commentRss><description>&lt;div class="mine"&gt; &lt;p&gt;Here's a question I got from a C# user last year, a question I get fairly frequently:&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;&lt;font color="#000080"&gt;User: With “regular” explicit properties, I tend to use the private backing field directly from within the class. Of course, with an automatic property, you can’t do this. My concern is that in the future, if I decide I need an explicit property for whatever reason, I’m left with the choice of changing the class implementation to use the new private field, or continue going through the property. I’m not sure what the right thing to do in this case is.&lt;/font&gt; &lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;You say “for whatever reason”, and that's key. The answer to your question will depend &lt;b&gt;entirely&lt;/b&gt; upon what the reason was that motivated the change.  &lt;p&gt;If the reason that motivated the change from automatically implemented property to explicitly implemented property was to &lt;strong&gt;change the semantics&lt;/strong&gt; of the property then you should evaluate whether the desired semantics when accessing the property &lt;i&gt;from within the class&lt;/i&gt; are identical to or different from the desired semantics when accessing the property &lt;i&gt;from outside the class&lt;/i&gt;.  &lt;p&gt;If the result of that investigation is “from within the class, the desired semantics of accessing this property are &lt;i&gt;different&lt;/i&gt; from the desired semantics of accessing the property from the outside”, then your edit has introduced a bug. &lt;b&gt;You should fix the bug&lt;/b&gt;. If they are &lt;i&gt;the same&lt;/i&gt;, then your edit has not introduced a bug; &lt;b&gt;keep the implementation the same&lt;/b&gt;.  &lt;p&gt;That is a bit abstract. Let’s get more concrete. Suppose you have &lt;span class="code"&gt; &lt;p&gt;sealed class BankAccount&lt;br&gt;{ &lt;br&gt;&amp;nbsp; public decimal Balance { get; set; } &lt;/span&gt; &lt;p&gt;and somewhere in the class you have a calculation: &lt;span class="code"&gt; &lt;p&gt;&amp;nbsp; if (Balance &amp;gt; 0) … &lt;/span&gt; &lt;p&gt;Then one day you decide to change the semantics of Balance: &lt;span class="code"&gt; &lt;p&gt;sealed class BankAccount &lt;br&gt;{ &lt;br&gt;&amp;nbsp; private decimal balance; &lt;br&gt;&amp;nbsp; public decimal Balance &lt;br&gt;&amp;nbsp; { &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; get &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; { &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (!CurrentUser.HasReadAccess(this))&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; throw new SecurityException(); &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return balance;&lt;br&gt;… &lt;/span&gt; &lt;p&gt;OK, you’ve changed the semantics of Balance. Now, everywhere inside your class, &lt;i&gt;do you intend all calculations that at present use Balance to &lt;b&gt;have&lt;/b&gt; these security semantics, or do you intend them to &lt;b&gt;not have&lt;/b&gt; these security semantics?&lt;/i&gt;  &lt;p&gt;If the former, &lt;b&gt;keep the calling code the way it is; it is correct; changing it will introduce a bug.&lt;/b&gt;  &lt;p&gt;If the latter, &lt;b&gt;change it; it is now wrong; changing it will fix a bug.&lt;/b&gt;  &lt;p&gt;If the reason that you changed the property from automatic to explicit was NOT to modify the semantics then… then… then &lt;strong&gt;why on Earth did you change it?&lt;/strong&gt; And why are you contemplating making &lt;i&gt;further&lt;/i&gt; inessential changes?  &lt;p&gt;I presume that in this case there must be &lt;i&gt;some&lt;/i&gt; motivation for changing correct working code; if you have some motivation that is causing you to change correct, working&amp;nbsp; code, I suppose it can be applied equally well to all the other correct, working code in the class that calls that property.  &lt;blockquote&gt; &lt;p&gt;&lt;font color="#000080"&gt;User: Thanks. This is on my mind because I’m starting my first C# 3.0 project, and am experimenting with the new features. The scenario that tripped me up is exactly as you state; someone making a future semantic change. &lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;Then your question is actually an instance of a more general question: &lt;strong&gt;“how do I future-proof a design?”&lt;/strong&gt; That is, how do I design and implement a class hierarchy now so that inevitable changes in the future are easier and less bug-prone? &lt;p&gt;That's a hard question, one which someone more knowledgeable than I am could write a whole book on. Next time on FAIC I'll give a few musings on this topic.&lt;/p&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9308211" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ericlippert/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Breaking+Changes/default.aspx">Breaking Changes</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Code+Quality/default.aspx">Code Quality</category></item><item><title>Arrays considered somewhat harmful</title><link>http://blogs.msdn.com/ericlippert/archive/2008/09/22/arrays-considered-somewhat-harmful.aspx</link><pubDate>Mon, 22 Sep 2008 21:04:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8961437</guid><dc:creator>Eric Lippert</dc:creator><slash:comments>58</slash:comments><comments>http://blogs.msdn.com/ericlippert/comments/8961437.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ericlippert/commentrss.aspx?PostID=8961437</wfw:commentRss><description>&lt;DIV class=mine&gt;
&lt;P&gt;I got a moral question from an author of programming language textbooks the other day requesting my opinions on whether or not beginner programmers should be taught how to use arrays.&lt;/P&gt;
&lt;P&gt;Rather than actually answer that question, I gave him a long&amp;nbsp;list of my opinions about&amp;nbsp;arrays, how I use arrays, how&amp;nbsp;we expect arrays to be used in the future, and so on. This gets a bit long, but like Pascal, I didn't have time to make it shorter.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Let me start by saying when you definitely should not use arrays, and then wax more philosophical about the future of modern programming and the role of the array in the coming world.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;You probably should not return an array as the value of a public method or property&lt;/STRONG&gt;, particularly when the information content of the array is logically immutable. Let me give you an example of where we got that horridly wrong in a very visible way in the framework.&amp;nbsp; If you take a look at the documentation for System.Type, you'll find that&amp;nbsp;just looking at the method descriptions gives one a sense of existential dread. One sees a whole lot of sentences like &lt;EM&gt;"Returns an array of Type objects that represent the constraints on the current generic type parameter." &lt;/EM&gt;Almost every method on System.Type returns an array it seems.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;Now think about how that must be implemented. When you call, say, GetConstructors() on typeof(string), the implementation cannot possibly do this, as sensible as it seems.&lt;/P&gt;&lt;SPAN class=code&gt;
&lt;P&gt;public class Type { &lt;BR&gt;&amp;nbsp;&amp;nbsp; private ConstructorInfo[] ctorInfos;&lt;BR&gt;&amp;nbsp;&amp;nbsp; public ConstructorInfo[] GetConstructors()&lt;BR&gt;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (ctorInfos == null) ctorInfos = GoGetConstructorInfosFromMetadata();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return ctorInfos;&lt;BR&gt;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;&lt;/SPAN&gt;
&lt;P&gt;Why? Because now the caller can take that array and &lt;EM&gt;replace the contents of it with whatever they please&lt;/EM&gt;. Returning an array means that &lt;STRONG&gt;you have to make a fresh copy of the array every time you return it&lt;/STRONG&gt;. You get called a hundred times, you’d better make a hundred array instances, no matter how large they are. It’s a performance nightmare – particularly if, like me, you are considering&amp;nbsp;using reflection to &lt;EM&gt;build a compiler&lt;/EM&gt;. Do you have any idea how many times a second I try to get type information out of reflection?&amp;nbsp; Not nearly as many times as I could; every time I do it’s another freakin’ array allocation!&lt;/P&gt;
&lt;P&gt;The frameworks designers were not foolish people; unfortunately, we did not have generic types in .NET 1.0. clearly the sensible thing now for GetConstructors() to return is IList&amp;lt;ConstructorInfo&amp;gt;. You can build yourself a nice read-only collection object once, and then just pass out references to it as much as you want. &lt;/P&gt;
&lt;P&gt;What is the root cause of this malaise? It is simple to state: The caller is requesting &lt;EM&gt;values&lt;/EM&gt;.&amp;nbsp; The callee fulfills the request by handing back &lt;EM&gt;variables&lt;/EM&gt;. &lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;An array is a collection of variables.&lt;/STRONG&gt; The caller doesn’t &lt;EM&gt;want&lt;/EM&gt; variables, but it’ll take them if that’s the only way to get the values. But in this case, as in most cases, &lt;EM&gt;neither the callee nor the caller wants those variables to ever vary&lt;/EM&gt;. Why on earth is the callee passing back variables then? &lt;STRONG&gt;Variables vary&lt;/STRONG&gt;. Therefore, a fresh, different variable must be passed back every time, so that if it does vary, nothing bad happens to anyone else who has requested the same values.&lt;/P&gt;
&lt;P&gt;If you are writing such an API, wrap the array in a ReadOnlyCollection&amp;lt;T&amp;gt; and return an IEnumerable&amp;lt;T&amp;gt; or an IList&amp;lt;T&amp;gt; or something, but not an array.&amp;nbsp; (And of course, do not simply cast the array to IEnumerable&amp;lt;T&amp;gt; and think you’re done!&amp;nbsp; That is still passing out variables; the caller can simply cast back to array!&amp;nbsp; Only pass out an array if it is wrapped up by a read-only object.)&lt;/P&gt;
&lt;P&gt;That’s the situation at present. What are the implications of array characteristics for the future of programming and programming languages?&lt;/P&gt;
&lt;P&gt;&lt;U&gt;Parallelism Problems&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;The physics aspects of Moore’s so-called “Law” are failing, as they eventually must. Clock speeds have stopped increasing, transistor density has stopped increasing. The laws of thermodynamics and the Uncertainty Principle are seeing to that. But manufacturing costs per chip are still falling, which means that our only hope of Moore’s "Law" continuing to hold over the coming decades is to cram more and more processors into each box.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;We’re going to need programming languages that allow mere mortals to write code that is parallelizable to multiple cores.&lt;/P&gt;
&lt;P&gt;Side-effecting change is the enemy of parallelization. Parallelizing in a world with observable side effects means locks, and locks means choosing between implementing lock ordering and dealing with random crashes or deadlocks. Lock ordering requires global knowledge of the program. Programs are becoming increasingly complex, to the point where one person cannot reasonably and confidently have global knowledge. Indeed, we prefer programming languages to have the property that programs in them can be understood by understanding one part at a time, not having to swallow the whole thing in one gulp.&lt;/P&gt;
&lt;P&gt;Therefore we tools providers need to create ways for people to program effectively &lt;EM&gt;without causing observable side effects&lt;/EM&gt;. &lt;/P&gt;
&lt;P&gt;Of all the sort of “basic” types, &lt;STRONG&gt;arrays most strongly work against this goal&lt;/STRONG&gt;. An array’s whole purpose is to be a mass of mutable state. Mutable state is hard for both humans and compilers to reason about. It will be hard for us to write compilers in the future that generate performant multi-core programs if developers use a lot of arrays.&lt;/P&gt;
&lt;P&gt;Now, one might reasonably point out that List&amp;lt;T&amp;gt; is a mass of mutable state too. But at least one could create a threadsafe list class, or an immutable list class, or a list class that has transactional integrity, or&amp;nbsp;uses some form of isolation&amp;nbsp;or whatever. We have an extensibility model for lists because &lt;EM&gt;lists are classes&lt;/EM&gt;. We have no ability to make an “immutable array”. Arrays are what they are and they’re never going to change.&lt;/P&gt;
&lt;P&gt;&lt;U&gt;Conceptual Problems&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;We want C# to be a language in which one can draw a line between code that implements a mechanism and code that implements a policy. &lt;/P&gt;
&lt;P&gt;The “C” programming language is all about mechanisms. It lays bare almost exactly what the processor is actually doing, providing only the thinnest abstraction over the memory model. And though we want you to be able to write programs like that in C#, most of the time people should be writing code in the “policy” realm. That is, code that emphasizes &lt;EM&gt;what the code is supposed to do, &lt;/EM&gt;not &lt;EM&gt;how it does it.&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;Coding which is more declarative than imperative, coding which avoids side effects, coding which emphasizes algorithms and purposes over mechanisms, that kind of coding is the future in a world of parallelism. (And you’ll note that LINQ is designed to be declarative, strongly abstract away from mechanisms, and be free of side effects.)&lt;/P&gt;
&lt;P&gt;Arrays work against all of these factors. Arrays demand imperative code, arrays are all about side effects, arrays make you write code which emphasizes how the code works, not what the code is doing or why it is doing it. Arrays make optimizing for things like “swapping two values” easy, but destroy the larger ability to optimize for parallelism.&lt;/P&gt;
&lt;P&gt;&lt;U&gt;Practical Problems&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;And finally, given that arrays are mutable by design, the way an array restricts that mutability is deeply weird. All the &lt;EM&gt;contents&lt;/EM&gt; of the collection are mutable, but the &lt;EM&gt;size&lt;/EM&gt; is fixed.&amp;nbsp; What is up with that? Does that solve a problem anyone actually has?&lt;/P&gt;
&lt;P&gt;For this reason alone I do almost no programming with arrays anymore. Arrays simply do not model any problem that I have at all well – I rarely need a collection which has the rather contradictory properties of being &lt;EM&gt;completely mutable&lt;/EM&gt;, and at the same time, &lt;EM&gt;fixed in size&lt;/EM&gt;. If I want to mutate a collection it is almost always to add something to it or remove something from it, not to change what value an index maps to.&lt;/P&gt;
&lt;P&gt;We have a class or interface for everything I need. If I need a sequence I’ll use IEnumerable&amp;lt;T&amp;gt;, if I need a mapping from contiguous numbers to data I’ll use a List&amp;lt;T&amp;gt;, if I need a mapping across arbitrary data I’ll use a Dictionary&amp;lt;K,V&amp;gt;, if I need a set I’ll use a HashSet&amp;lt;T&amp;gt;. I simply don’t need arrays for anything, so I almost never use them. They don’t solve a problem I have better than the other tools at my disposal.&lt;/P&gt;
&lt;P&gt;&lt;U&gt;Pedagogic Problems&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;It is&amp;nbsp;important that beginning programmers understand arrays; it is an important and widely used concept. But it is also important to me that they understand the weaknesses and shortcomings of arrays. In almost every case, there is a better tool to use than an array.&lt;/P&gt;
&lt;P&gt;The difficulty is, pedagogically, that it is hard to discuss the merits of those tools without already having down concepts like classes, interfaces, generics, asymptotic performance, query expressions, and so on. It’s a hard problem for the writer and for the teacher. Fortunately, for me, it's not a problem that I personally have to solve.&lt;/P&gt;&lt;/DIV&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8961437" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Performance/default.aspx">Performance</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Rants/default.aspx">Rants</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Code+Quality/default.aspx">Code Quality</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Threading/default.aspx">Threading</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Software+development+methodology/default.aspx">Software development methodology</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Immutability/default.aspx">Immutability</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Arrays/default.aspx">Arrays</category></item><item><title>Vexing exceptions</title><link>http://blogs.msdn.com/ericlippert/archive/2008/09/10/vexing-exceptions.aspx</link><pubDate>Wed, 10 Sep 2008 22:28:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8941803</guid><dc:creator>Eric Lippert</dc:creator><slash:comments>35</slash:comments><comments>http://blogs.msdn.com/ericlippert/comments/8941803.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ericlippert/commentrss.aspx?PostID=8941803</wfw:commentRss><description>&lt;DIV class=mine&gt;
&lt;P&gt;Writing good error handling code is hard in any language, whether you have exception handling or not. When I'm thinking about what exception handling I need to implement in a given program, I first classify every exception I might catch into one of four buckets which I label &lt;STRONG&gt;fatal&lt;/STRONG&gt;, &lt;STRONG&gt;boneheaded&lt;/STRONG&gt;, &lt;STRONG&gt;vexing&lt;/STRONG&gt; and &lt;STRONG&gt;exogenous&lt;/STRONG&gt;.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Fatal&lt;/STRONG&gt; exceptions are &lt;EM&gt;not your fault&lt;/EM&gt;, you &lt;EM&gt;cannot prevent them&lt;/EM&gt;, and you &lt;EM&gt;cannot sensibly clean up from them&lt;/EM&gt;. They almost always&amp;nbsp;happen because the process is deeply diseased and is about to be put out of its misery. Out of memory, thread aborted, and so on. There is absolutely no point in catching these because nothing your puny user code can do will fix the problem. Just let your "finally" blocks run and hope for the best.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Boneheaded&lt;/STRONG&gt; exceptions are &lt;EM&gt;your own darn fault&lt;/EM&gt;, you &lt;EM&gt;could have prevented them&lt;/EM&gt; and therefore &lt;EM&gt;they are bugs in your code&lt;/EM&gt;. You should not catch them; doing so is hiding a bug in your code. Rather, you should write your code so that the exception cannot possibly happen in the first place, and therefore does not need to be caught. That argument is null, that typecast is bad, that index is out of range, you're trying to divide by zero – these are all problems that you could have prevented very easily in the first place, so prevent the mess in the first place rather than trying to clean it up.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Vexing&lt;/STRONG&gt; exceptions are the result of &lt;EM&gt;unfortunate design decisions&lt;/EM&gt;. Vexing exceptions are thrown in a completely non-exceptional circumstance, and therefore must be caught and handled all the time.&lt;/P&gt;
&lt;P&gt;The classic example of a vexing exception is &lt;SPAN class=code&gt;Int32.Parse&lt;/SPAN&gt;, which throws if you give it a string that cannot be parsed as an integer. But the 99% use case for this method is transforming strings input by the user, which could be any old thing, and therefore it is in no way &lt;EM&gt;exceptional&lt;/EM&gt; for the parse to fail. Worse, there is no way for the caller to determine ahead of time whether their argument is bad &lt;EM&gt;without implementing the entire method themselves&lt;/EM&gt;, in which case they wouldn't need to be calling it in the first place.&lt;/P&gt;
&lt;P&gt;This unfortunate design decision was so vexing that of course the frameworks team implemented &lt;SPAN class=code&gt;TryParse&lt;/SPAN&gt; shortly thereafter which does the right thing.&lt;/P&gt;
&lt;P&gt;You have to catch vexing exceptions, but doing so is vexing.&lt;/P&gt;
&lt;P&gt;Try to never write a library yourself that throws a vexing exception.&lt;/P&gt;
&lt;P&gt;And finally, &lt;STRONG&gt;exogenous&lt;/STRONG&gt; exceptions appear to be somewhat like vexing exceptions except that they are not the result of unfortunate design choices. Rather, they are the result of untidy external realities impinging upon your beautiful, crisp program logic. Consider this pseudo-C# code, for example:&lt;/P&gt;&lt;SPAN class=code&gt;
&lt;P&gt;try&lt;BR&gt;{&lt;BR&gt;&amp;nbsp; using ( File f = OpenFile(filename, ForReading) )&lt;BR&gt;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Blah blah blah&lt;BR&gt;&amp;nbsp; }&lt;BR&gt;}&lt;BR&gt;catch (FileNotFoundException)&lt;BR&gt;{&lt;BR&gt;&amp;nbsp; // Handle filename not found &lt;BR&gt;}&lt;/P&gt;&lt;/SPAN&gt;
&lt;P&gt;Can you eliminate the try-catch?&amp;nbsp; &lt;/P&gt;&lt;SPAN class=code&gt;
&lt;P&gt;if (!FileExists(filename))&lt;BR&gt;&amp;nbsp; // Handle filename not found &lt;BR&gt;else&lt;BR&gt;&amp;nbsp; using ( File f = ...&lt;/P&gt;&lt;/SPAN&gt;
&lt;P&gt;This isn't the same program. There is now a "race condition". Some other process could have deleted, locked, moved or changed the permissions of the file between the FileExists and the OpenFile.&lt;/P&gt;
&lt;P&gt;Can we be more sophisticated? What if we lock the file? That doesn't help. The media might have been removed from the drive, the network might have gone down… &lt;/P&gt;
&lt;P&gt;You’ve got to catch an exogenous exception because it always could happen no matter how hard you try to avoid it; it’s an exogenous condition outside of your control.&lt;/P&gt;
&lt;P&gt;So, to sum up:&lt;/P&gt;
&lt;P&gt;• Don’t catch fatal exceptions; nothing you can do about them anyway, and trying to generally makes it worse.&lt;BR&gt;• Fix your code so that it never triggers a boneheaded exception –&amp;nbsp;an "index out of range" exception should never happen in production code.&lt;BR&gt;• Avoid vexing exceptions whenever possible by calling the “Try” versions of those vexing methods that throw in non-exceptional circumstances. If you cannot avoid&amp;nbsp;calling a vexing method,&amp;nbsp;catch&amp;nbsp;its vexing&amp;nbsp;exceptions.&lt;BR&gt;• Always handle exceptions that indicate unexpected exogenous conditions; generally it is not worthwhile or practical to anticipate every possible failure. Just try the operation and be prepared to handle the exception.&lt;BR&gt;&lt;/P&gt;&lt;/DIV&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8941803" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ericlippert/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Code+Quality/default.aspx">Code Quality</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/exception+handling/default.aspx">exception handling</category></item><item><title>High maintenance</title><link>http://blogs.msdn.com/ericlippert/archive/2008/09/08/high-maintenance.aspx</link><pubDate>Mon, 08 Sep 2008 19:36:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8934196</guid><dc:creator>Eric Lippert</dc:creator><slash:comments>45</slash:comments><comments>http://blogs.msdn.com/ericlippert/comments/8934196.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ericlippert/commentrss.aspx?PostID=8934196</wfw:commentRss><description>&lt;DIV class=mine&gt;
&lt;P&gt;The other day I went to buy some snack from the snack machine in the kitchen. The snack I wanted was in slot B-10, so I put in my coins, press B - one - zero, hey wait a minute there's no zero button! And why is it serving me up the snack on the left end of the machine instead of the right?&amp;nbsp;Aha, there is a button marked "10", which is the one I was supposed to press. Instead I got snack B1. How irksome!&lt;/P&gt;
&lt;P&gt;And then I laughed at my plight, because of course Steve Maguire told &lt;EM&gt;the same story&lt;/EM&gt; about Microsoft vending machines in &lt;U&gt;Writing Solid Code&lt;/U&gt; &lt;EM&gt;fifteen years ago&lt;/EM&gt;. Maguire went on to make an analogy between bad candy machine interfaces and bad software interfaces; a "candy machine interface" is one which leads the caller to make a plausible but wrong choice.&lt;/P&gt;
&lt;P&gt;Coincidentally, I was asked to review a fragment of code recently that got me thinking again about candy machine interfaces. This isn't &lt;EM&gt;exactly&lt;/EM&gt; the kind of bad interface that Maguire was talking about -- but I'm getting ahead of myself. Let's take a look at the code:&lt;/P&gt;&lt;SPAN class=code&gt;
&lt;P mce_keep="true"&gt;public static class StreamReaderExtensions&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public static IEnumerable&amp;lt;string&amp;gt; Lines(this StreamReader reader)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (reader== null)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; throw new ArgumentNullException("reader");&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; reader.BaseStream.Seek(0, SeekOrigin.Begin);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; string line;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; while ((line = reader.ReadLine()) != null)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; yield return line;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;}&lt;/P&gt;&lt;/SPAN&gt;
&lt;P&gt;The&amp;nbsp;&lt;EM&gt;idea&lt;/EM&gt; of this code is awesome.&amp;nbsp;I use this technique all the time in my own programs when I need to manipulate a file. Being able to treat a text file as a sequence of lines is very handy. However, the &lt;EM&gt;execution&lt;/EM&gt; of it has some problems.&lt;/P&gt;
&lt;P&gt;The first flaw is the bug I discussed in my earlier post on &lt;A class="" href="http://blogs.msdn.com/ericlippert/archive/2007/09/05/psychic-debugging-part-one.aspx" mce_href="http://blogs.msdn.com/ericlippert/archive/2007/09/05/psychic-debugging-part-one.aspx"&gt;psychic debugging&lt;/A&gt;. Namely, the null check is not performed when the method is called, but rather, when the returned enumerator is moved for the first time. That means that the exception isn't thrown until possibly far, far away from the actual site of the error, which is potentially confusing. &lt;/P&gt;
&lt;P&gt;The second flaw is that the "while" loop condition is a bit hard to read because it tries to do so much in one line. Shorter code is not magically faster than longer code that does the same thing; write the code so that it is maximally readable.&lt;/P&gt;
&lt;P mce_keep="true"&gt;But there's a deeper flaw here. To get at it, first let me state a crucial fact about the relationship between a stream reader and a stream:&lt;/P&gt;
&lt;P&gt;A StreamReader “owns” its underlying stream. That is, once a stream has been handed to a stream reader by a caller, the caller should not muck with the stream ever again. The stream reader will be seeking around in the stream; mucking around with the stream could interfere with the stream reader, and the stream reader will interfere with anyone trying to use the stream. The stream reader emphasizes its ownership by &lt;EM&gt;taking responsibility for disposing the stream when the stream reader is itself disposed.&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;Now, “Lines” defines an object, and what does that object do right off the bat?&amp;nbsp; &lt;STRONG&gt;It mucks around with the underlying stream.&lt;/STRONG&gt; This is big red flag #1. This smells &lt;EM&gt;terrible&lt;/EM&gt;. No one should be messing with that stream but the reader.&lt;/P&gt;
&lt;P&gt;Furthermore, think about it from the caller’s point of view. Maybe the caller knows that there are a bunch of bytes that it wants to skip, so it deliberately hands a StreamReader to Lines() which has been positioned somewhere past the beginning of the file. But Lines() thinks that it knows best and &lt;STRONG&gt;ignores the information that the caller has given it&lt;/STRONG&gt;. This is big red flag #2.&lt;/P&gt;
&lt;P&gt;(The reason why the original code was seeking back was because the same reader was being "recycled" many times to read the same file over and over again, which is yet another red flag. Readers are cheap; you can have multiple readers on one file, there's no need to hoard them and reuse them.)&lt;/P&gt;
&lt;P&gt;The third big red flag for me here is the ownership issue. When you hand a stream to a reader, the reader takes care of everything for you from then on – that’s the “contract” between the reader and the caller.&amp;nbsp; “Lines()” does not have that contract. Lines()’s contract says &lt;EM&gt;“attention caller: I am taking ownership of this reader. I am going to muck with its underlying stream. You must never use this reader for anything ever again – except that I’m not going to dispose it for you. If you want the reader and its stream closed then you are going to have to keep a reference to it around until &lt;STRONG&gt;you&lt;/STRONG&gt; can prove that &lt;STRONG&gt;I &lt;/STRONG&gt;am done with it. And if you get it wrong, either I crash or you get a resource leak. So get it right.”&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;This is a &lt;EM&gt;terrible&lt;/EM&gt; contract to impose upon a caller, and of course, the imposition here is in no way represented by the signature of the method. You just have to know that “Lines()” owns the reader for the purposes of all its &lt;EM&gt;functionality&lt;/EM&gt;, but not for its &lt;EM&gt;cleanup&lt;/EM&gt;, which is deeply weird.&lt;/P&gt;
&lt;P&gt;In short, &lt;STRONG&gt;the caller needs to know all the details of what is happening in the method in order to use it correctly&lt;/STRONG&gt;; this is a violation of the whole purpose of creating methods. Methods are supposed to abstract away an operation so that you do not have to know what they are doing.&lt;/P&gt;
&lt;P&gt;A fourth red flag is that &lt;STRONG&gt;the task performed by Lines() does not have a clear meaning in terms of the logic of the program&lt;/STRONG&gt;. In looking at every caller of this method it became clear that the desired semantics&amp;nbsp;were “give me the lines of this &lt;STRONG&gt;text file&lt;/STRONG&gt; one at a time”. But we haven’t written a method that does that. In order to get the lines of a text file from Lines() the caller is required to do all kinds of work to make Lines() happy – open the stream, create a reader, call Lines() but keep the reader around, close the reader after we know that the iterator is done.&lt;/P&gt;
&lt;P&gt;This code makes the caller do a whole bunch of things -- things that have to be done in exactly the right order but potentially distributed out over long stretches of time and disparate code locations. This method is very "high-maintenance"! Everything has to be just right all the time in order for it to be happy and get along with others; anything out of place and things will start going wrong.&lt;/P&gt;
&lt;P&gt;Finally,&amp;nbsp;we have an example of premature generality here -- if the intention is to read a text file, then write a method that reads a text file. If you don't need the power of reading lines from an arbitrary stream, maybe don't implement that. Cut down on your testing burden.&lt;/P&gt;
&lt;P&gt;Some relatively simple changes fix it up:&lt;/P&gt;&lt;SPAN class=code&gt;
&lt;P&gt;public static class FileUtilities&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public static IEnumerable&amp;lt;string&amp;gt; Lines(string filename)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (filename == null)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; throw new ArgumentNullException("filename");&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return LinesCore(filename);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;private static IEnumerable&amp;lt;string&amp;gt; LinesCore(string filename)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Debug.Assert(filename != null);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; using(var reader = new StreamReader(filename))&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; while (true)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;{ &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; string line = reader.ReadLine();&lt;BR&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (line == null) &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;yield break;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;yield return line;&lt;BR&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;}&lt;/P&gt;&lt;/SPAN&gt;
&lt;P&gt;And now everything is crystal clear. The purpose of this thing is to read the lines out of a text file. The caller gives it the name of a file, it gives you the lines, and we’re done. The caller does not have to worry about anything else – the iterator takes care of opening the file, cleaning up when its done, and so on. There’s no messing around with streams at all; we have now provided &lt;STRONG&gt;an abstraction over the file system&lt;/STRONG&gt; to the caller.&lt;/P&gt;
&lt;P&gt;Whenever you write a method &lt;STRONG&gt;think about the contract of that method&lt;/STRONG&gt;. What burdens are you imposing upon the caller? Are they reasonable burdens?&amp;nbsp; The purpose of a method should be to make the caller’s life easier; the original version of Lines() makes life harder on the caller. The new version makes life easier. &lt;STRONG&gt;Don't write high-maintenance methods.&lt;/STRONG&gt;&lt;/P&gt;&lt;/DIV&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8934196" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Rants/default.aspx">Rants</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Code+Quality/default.aspx">Code Quality</category></item><item><title>Reading Code Over the Telephone</title><link>http://blogs.msdn.com/ericlippert/archive/2008/05/16/reading-code-over-the-telephone.aspx</link><pubDate>Fri, 16 May 2008 17:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8495414</guid><dc:creator>Eric Lippert</dc:creator><slash:comments>25</slash:comments><comments>http://blogs.msdn.com/ericlippert/comments/8495414.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ericlippert/commentrss.aspx?PostID=8495414</wfw:commentRss><description>&lt;DIV class=mine&gt;
&lt;P&gt;In my youth I once attended a lecture given by Brian Kernighan on the subject of code quality, which was very influential on my attitudes towards writing legible code. One of the things that Kernighan recommended was to endeavour write code that was so clear that it could be easily read over the phone and understood. Most people find code much easier to comprehend when read than when heard; if you can make it clear enough to be understood when heard, it's probably pretty clear code.&lt;/P&gt;
&lt;P&gt;I was reminded of this when I got the following question from&amp;nbsp;a colleague by email:&lt;/P&gt;&lt;SPAN class=code&gt;
&lt;P&gt;Subject: Stupid C# 3.0 lambda expression question&lt;/P&gt;
&lt;P&gt;How does one read the =&amp;gt; operator? &lt;/P&gt;&lt;/SPAN&gt;
&lt;P&gt;First off, I told my colleague that &lt;STRONG&gt;there are no stupid &lt;EM&gt;questions&lt;/EM&gt;, only stupid &lt;EM&gt;people&lt;/EM&gt;&lt;/STRONG&gt;. No stupid people work here, so don't stress about it. This is a perfectly sensible question.&lt;/P&gt;
&lt;P&gt;As far as I know, we do not have an "official" line on how to read this operator over the phone. In the absense of any other context, I personally would say &lt;SPAN class=code&gt;c=&amp;gt;c+1&lt;/SPAN&gt; as "see &lt;STRONG&gt;goes to&lt;/STRONG&gt; see plus one". Some variations that I've heard:&lt;/P&gt;
&lt;P&gt;For a projection, &lt;SPAN class=code&gt;(Customer c)=&amp;gt;c.Name&lt;/SPAN&gt;: "customer see &lt;STRONG&gt;becomes&lt;/STRONG&gt; see dot name"&lt;/P&gt;
&lt;P&gt;For a predicate, &lt;SPAN class=code&gt;(Customer c)=&amp;gt;c.Age &amp;gt; 21&lt;/SPAN&gt;: "customer see &lt;STRONG&gt;such that&lt;/STRONG&gt; see dot age is greater than twenty-one"&lt;/P&gt;
&lt;P&gt;An unfortunate conflation is that the =&amp;gt; operator looks a lot like ⇒, the "implies" operator in mathematics. Since =&amp;gt; does not have the same semantics as ⇒, it is probably a bad idea to read =&amp;gt; as "implies". (x⇒y&amp;nbsp;would have&amp;nbsp;the semantics of &lt;SPAN class=code&gt;!x|y&lt;/SPAN&gt; in C#.)&lt;/P&gt;
&lt;P&gt;Incidentally, it is a little known fact that VB6 and VBScript implemented the ⇒ operator with the &lt;SPAN class=code&gt;Imp&lt;/SPAN&gt; keyword and the ⇔ operator with the &lt;SPAN class=code&gt;Eqv&lt;/SPAN&gt; keyword. They disappeared in VB.NET. Where did they go? It is a mystery!&lt;/P&gt;&lt;/DIV&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8495414" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ericlippert/archive/tags/VBScript/default.aspx">VBScript</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Lambda+Expressions/default.aspx">Lambda Expressions</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Code+Quality/default.aspx">Code Quality</category></item><item><title>Translating intentions and mechanisms</title><link>http://blogs.msdn.com/ericlippert/archive/2008/03/25/translating-intentions-and-mechanisms.aspx</link><pubDate>Tue, 25 Mar 2008 20:26:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8336141</guid><dc:creator>Eric Lippert</dc:creator><slash:comments>12</slash:comments><comments>http://blogs.msdn.com/ericlippert/comments/8336141.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ericlippert/commentrss.aspx?PostID=8336141</wfw:commentRss><description>&lt;DIV class=mine&gt;
&lt;P&gt;Before I get into today's blogging, a quick note about my recent post on &lt;A href="http://blogs.msdn.com/ericlippert/archive/2008/02/20/how-to-not-get-a-question-answered.aspx" mce_href="http://blogs.msdn.com/ericlippert/archive/2008/02/20/how-to-not-get-a-question-answered.aspx"&gt;How To Not Get A Question Answered&lt;/A&gt;. That was certainly not &lt;EM&gt;intended&lt;/EM&gt; to be fishing for compliments or chiding people for never acknowledging help ten years ago; that said, I appreciate both. Thanks to everyone who made thoughtful comments.&lt;/P&gt;
&lt;P&gt;Moving on; a theme that seems to be coming up over and over again in my recent technical conversations is that of &lt;EM&gt;intention&lt;/EM&gt; vs. &lt;EM&gt;mechanism&lt;/EM&gt;. We have tried hard in C# 3.0 to make a language where there is a good balance between the code reading as a &lt;EM&gt;declaration&lt;/EM&gt; of what &lt;EM&gt;meaning&lt;/EM&gt; you intend the code to represent, and reading as a list of &lt;EM&gt;imperative instructions&lt;/EM&gt; specifying a &lt;EM&gt;mechanism&lt;/EM&gt; which achieves those intentions. I've been accumulating anecdotes about this tension between representing intentions vs. mechanisms; expect this to be a recurring theme in the blog for the next while.&lt;/P&gt;
&lt;P&gt;Here's a question I got recently which speaks to this tension with regards to the subject of porting code from one language to another:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;FONT color=#000080&gt;I have some C++ code with a macro in it. A typical usage looks like this:&lt;/FONT&gt; 
&lt;P&gt;&lt;FONT color=#000080&gt;TRY_WAIT_OP(Execute());&lt;/FONT&gt; 
&lt;P&gt;&lt;FONT color=#000080&gt;This macro expands to code similar to this:&lt;/FONT&gt; 
&lt;P&gt;&lt;FONT color=#000080&gt;for(int i=0; i&amp;lt;10; i++) {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (Execute()) break;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Sleep(i*1000);&lt;BR&gt;}&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color=#000080&gt;We are translating this code into C#, but C# does not have a #define directive. &lt;STRONG&gt;How do I do textual replacement of code in C#?&lt;/STRONG&gt; &lt;/FONT&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Once more, we have someone &lt;A href="http://blogs.msdn.com/ericlippert/archive/2003/11/03/53333.aspx" mce_href="http://blogs.msdn.com/ericlippert/archive/2003/11/03/53333.aspx"&gt;looking for a thin metal ruler&lt;/A&gt;. There's a problem -- represent the meaning of a common operation in a programming language. C++ provides a solution mechanism -- using preprocessor-based metaprogramming to create a nonstandard control flow primitive. The natural tendency when doing a translation is to find the identical mechanism in the new language, and then translate the code to use that mechanism. But what if there is no such mechanism?&lt;/P&gt;
&lt;P&gt;In that case, you've got to translate the &lt;EM&gt;intentions&lt;/EM&gt;, which after all, is what you are trying to translate in the first place. Presumably the new code is intended to do the same thing as the old code. Translating the mechanisms is just a particularly easy road to translating the intentions. What is the &lt;EM&gt;meaning&lt;/EM&gt; of this macro?&lt;/P&gt;
&lt;P&gt;Clearly TRY_WAIT_OP means "execute some arbitrary code that returns a Boolean. If it returns true, you're done. If it returns false, wait some amount of time and try again, up to ten times".&lt;/P&gt;
&lt;P&gt;Now &lt;STRONG&gt;think about how you would write code &lt;EM&gt;from scratch&lt;/EM&gt; that implemented those intentions in C#. &lt;/STRONG&gt;Don't think at all about how it was written in C++. Your goal here is to solve the same problem using a different tool, so don't use the same techniques that you used for the other tool if they're not appropriate. Use the techniques that are appropriate for &lt;EM&gt;this&lt;/EM&gt; tool. 
&lt;P&gt;The way I would write that in C# is to write a &lt;EM&gt;method&lt;/EM&gt; that takes as its argument &lt;EM&gt;some arbitrary code that returns a Boolean&lt;/EM&gt;. "Arbitrary code" is represented in C# by a &lt;EM&gt;delegate&lt;/EM&gt;. We can do a bit better than the macro while we're at it, and return a success code:&lt;SPAN class=code&gt; 
&lt;P&gt;private static bool AttemptMultiple(Func&amp;lt;bool&amp;gt; action) {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Debug.Assert(action != null);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; const int maxAttempts = 10; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; const int delay = 1000; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for (int attempt = 0 ; attempt &amp;lt; maxAttempts ; ++attempt) {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (action()) return true;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Sleep(attempt * delay); &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return false;&lt;BR&gt;} &lt;/SPAN&gt;
&lt;P&gt;Lambda expression syntax gives us a nice way to do the call: &lt;SPAN class=code&gt;
&lt;P&gt;AttemptMultiple(()=&amp;gt;Execute()); &lt;/SPAN&gt;
&lt;P&gt;A code porting project is a good opportunity to review the design fundamentals:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Why 10 retrys?&amp;nbsp; Should this be a parameter to AttemptMultiple? 
&lt;LI&gt;Why wait 1000 milliseconds instead of some other delay? Should this also be a parameter? 
&lt;LI&gt;Why is the increasing delay linear rather than constant, geometric, etc? Should the delay strategy be a parameter? &lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;The above questions are good but they rather miss the point. The important question is: &lt;STRONG&gt;is this functionality even a good idea in the first place?&lt;/STRONG&gt; 
&lt;P&gt;This last point is key. The "try it, fail, wait, try again" strategy is in general a dangerous one because &lt;STRONG&gt;it does not compose well with itself&lt;/STRONG&gt;. Consider the following: &lt;SPAN class=code&gt;
&lt;P&gt;bool SendPackets()&amp;nbsp;&amp;nbsp; { ... if (!AttemptMultiple(()=&amp;gt;{ ... })) return false; ... }&lt;BR&gt;bool TalkToSocket()&amp;nbsp; { ... if (!AttemptMultiple(()=&amp;gt;SendPackets())) return false; ... }&lt;BR&gt;bool SendData()&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; { ... if (!AttemptMultiple(()=&amp;gt;TalkToSocket())) return false;... }&lt;BR&gt;void HandleCommand() { ... if (command == SendData &amp;amp;&amp;amp; !AttemptMultiple(()=&amp;gt;SendData())) ReportErrorToUser();... } &lt;/SPAN&gt;
&lt;P&gt;Now suppose that the user has a bad network card and SendPackets is always going to fail. If you look at any &lt;EM&gt;one&lt;/EM&gt; of those lines, it looks like the attempt is being made ten times and will take a maximum of about one minute. In fact, the attempt to send the packet is made &lt;STRONG&gt;ten thousand&lt;/STRONG&gt; times and will not report the error to the user for about a week. 
&lt;P&gt;Usually the right thing to do when something fails is to go into a failure state &lt;EM&gt;immediately&lt;/EM&gt;. &lt;STRONG&gt;Tell the user that something failed and let them decide when and if to retry it.&lt;/STRONG&gt; 
&lt;P&gt;What are some examples of a poor mismatch between intentions and mechanisms that you guys have seen? I'm interested in stories about: 
&lt;UL&gt;
&lt;LI&gt;mechanisms that subtly did not implement the intentions of the programmer&lt;/LI&gt;
&lt;LI&gt;situations where the intention of the code was completely obscured by the mechanisms. How did you make the code better reflect the intentions?&lt;/LI&gt;
&lt;LI&gt;situations where the mechanism of the code was important, but obscured by unnecessary emphasis on representing the intention. What were the negative consequences of obscuring the mechanism behind some abstraction?&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;Thanks! 
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;&lt;/DIV&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8336141" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ericlippert/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Lambda+Expressions/default.aspx">Lambda Expressions</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Code+Quality/default.aspx">Code Quality</category></item><item><title>Bad Names</title><link>http://blogs.msdn.com/ericlippert/archive/2007/06/12/bad-names.aspx</link><pubDate>Tue, 12 Jun 2007 21:59:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:3255064</guid><dc:creator>Eric Lippert</dc:creator><slash:comments>10</slash:comments><comments>http://blogs.msdn.com/ericlippert/comments/3255064.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ericlippert/commentrss.aspx?PostID=3255064</wfw:commentRss><description>&lt;DIV class=mine&gt;
&lt;P&gt;Over the past year and a half of working on the C# compiler we’ve refactored a lot of crufty old code. But a surprising number of the “refactorings” have actually simply been renaming a structure, class, or method&amp;nbsp;to something more sensible. The compiler is showing its age; there are a lot of structures, classes and&amp;nbsp;methods&amp;nbsp;inside the compiler which no longer have sensible names (if, in fact, they ever did). Going through the whole thing all at once and renaming everything without changing the semantics of any of the code is a fairly cheap and easy way to massively improve code readability. After all, what is easier to understand, &lt;SPAN class=code&gt;FUNCBREC&lt;/SPAN&gt; or &lt;SPAN class=code&gt;StatementBindingContext&lt;/SPAN&gt;? (I think &lt;SPAN class=code&gt;FUNCBREC&lt;/SPAN&gt; at one point stood for “function binding record”.)&lt;/P&gt;
&lt;P&gt;One of the things we’ve been looking for is names that “smell bad”. Sometimes the thing simply needs to be renamed and that’s the end of the story. Sometimes, though, the existence of a bad name is a red flag in the code that points to some chunk of functionality which could use some serious thought and additional&amp;nbsp;code refactoring applied to it.&lt;/P&gt;
&lt;P&gt;Functions with names like &lt;SPAN class=code&gt;DoTheOtherThingToIt&lt;/SPAN&gt; are obviously badly named. There’s no way to look at the call site and have the faintest idea what’s happening on the callee side.&amp;nbsp; But there are more subtle signs of badness. I’ve been compiling a list of these red flag particles, and here is what we’ve come up with so far:&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Misc&lt;/STRONG&gt;:&lt;/P&gt;
&lt;P&gt;This is indicative that the &lt;A class="" href="http://en.wikipedia.org/wiki/Single_responsibility_principle" mce_href="http://en.wikipedia.org/wiki/Single_responsibility_principle"&gt;Single Responsibility Principle&lt;/A&gt; has been violated.&amp;nbsp; Functions or structures which do “miscellaneous” work almost certainly do two or more things which are unrelated.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Base/Core/Worker/Helper:&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;These are often a sign of too much or too little abstraction. Suppose &lt;SPAN class=code&gt;InferTypeSignature&lt;/SPAN&gt; calls &lt;SPAN class=code&gt;InferTypeSignatureWorker&lt;/SPAN&gt;. If &lt;SPAN class=code&gt;InferTypeSignatureWorker&lt;/SPAN&gt; is doing all the work of inferring a type signature then move it back into &lt;SPAN class=code&gt;InferTypeSignature&lt;/SPAN&gt;. If it is not doing all the work, then describe what work it is doing, don’t just say that it’s working on something.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Fake/Real:&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;In the compiler we had a structure called &lt;SPAN class=code&gt;FakeMethodSymbol&lt;/SPAN&gt;, deriving from &lt;SPAN class=code&gt;MethodSymbol&lt;/SPAN&gt;. OK, it’s a symbol, it represents a method, got it. But in what sense is it a “fake” method? Is it a compiler-generated method as opposed to a user-generated method? Is it some kind of stub for a missing method? When is it legal to use as a &lt;SPAN class=code&gt;MethodSymbol&lt;/SPAN&gt;? Who knows? I sure didn’t. “Fake” doesn’t tell you anything about its purpose. &lt;/P&gt;
&lt;P&gt;(It turned out that this is used when representing the method signature of an expanded method call that uses a C++ style “varargs” argument list. We’ve renamed it &lt;SPAN class=code&gt;ExpandedVarargsMethodSymbol&lt;/SPAN&gt;, and now it is extremely clear what that thing is for.)&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Special/Normal:&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;Describe the property that makes the case special, rather than just calling it out as special. And try not to call things “normal”, just call them things. Rather than &lt;SPAN class=code&gt;NormalFoo&lt;/SPAN&gt; and &lt;SPAN class=code&gt;SpecialFoo&lt;/SPAN&gt;, have &lt;SPAN class=code&gt;Foo&lt;/SPAN&gt; and &lt;SPAN class=code&gt;FrobbingFoo&lt;/SPAN&gt;. Or have a &lt;SPAN class=code&gt;NonFrobbingFoo&lt;/SPAN&gt; and a &lt;SPAN class=code&gt;FrobbingFoo&lt;/SPAN&gt; both derived from abstract base class &lt;SPAN class=code&gt;Foo&lt;/SPAN&gt;.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Ex/2:&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;Oh, the pain. These should be used to solve COM interface versioning problems, and even then only as a last resort.&lt;/P&gt;
&lt;P&gt;Those are the ones we've found in the compiler so far. What sort of bad-smelling name particles do you guys encounter in crufty code?&lt;BR&gt;&lt;/P&gt;&lt;/DIV&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=3255064" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ericlippert/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blogs.msdn.com/ericlippert/archive/tags/Code+Quality/default.aspx">Code Quality</category></item></channel></rss>