Date: Wed, 22 May 2013 12:38:08 +0200
Quote:
- Richard Hale Shaw's Blog
How I failed the Blog Screen (non-feedback from a non-reader)
http://richardhaleshawgroup.com/RHSGroup/Community/blogs/richard_hale_shaws_blog/archive/2006/02/06/215.aspx
Text:
- The Dispose pattern is about strong ownership, not shared ownership
- The using statement is about easing a specific usage of the Dispose pattern
- The fact that you're not required to allocate an object in the using statement is a red-herring
- Anyone that even hints at suggesting the use of a finalizer instead of using/Dispose is just not qualified to touch code on a large dynamic system, let alone have a position of higher authority
- using(){} is less complex than addref/release, malloc/free, and new/delete/delete[]
- Most mere mortals have difficulty coding a proper substitute for using(){}. In fact, if they have trouble with using, I assert that there's no way that they'll be able to properly implement the required try/finally code in all cases.
- He doesn't define "strong ownership" or "shared ownership" but I assume he means that a "strong" owner is has exclusive access to the resource (and owns the only outstanding reference) as opposed to a "shared" owner who owns one of many references. There's nothing about my criticism of the using statement that has to do with this issue: if you are the exclusive owner of the resource, you should (ideally) dispose of it as soon as you can once you're finished using it. Whether you use using or you use a try-finally to call IDisposable.Dispose on such a resource is irrelevant: it should be done either way if you're the exclusive owner, and should not be done if other references are still outstanding.
- When he objects to my blog entry, saying "using statement is about easing a specific usage of the Dispose pattern", it's a straw-man: if you re-read the original blog entry, it implies this when I talked about using as a means of semi-automating the call the Dispose (which is a good practice but not required).
- He doesn't like my criticism of "the fact that you're not required to allocate an object in the using statement", calling it a red-herring. I found this particularly funny. The gist of my argument against using is that with that particular syntax, it leaves an object in scope after the using statement or block, and that unless someone understood that the object was now disposed (and unusable) there's a good case that they'll try to use it after the statement or block. And, a correct implementation of the Dispose Pattern calls for all public operations to throw ObjectDisposedException if they're utilized after the object has been disposed (because after it's been disposed it's considered unusable).
- My objection to using is actually quite similar to the ones I made in past years about "addref/release, malloc/free, and new/delete/delete[] ", and that using is supposed to be more like COM smart pointers, which simplified the AddRef/Release mechanism but was vulnerable in a similar way if the smart pointer had too long a lifetime or scope. Because of the similar vulnerability with using, it doesn't reduce the complexity of using Dispose.
Browsing the Internet recently, I came across the following which references my concerns with the using statement for early object disposal in C#:
"As a practice manager, I usually spend a portion of my week interviewing candidates. Occasionally I'll get an incorrect answer delivered with such confidence that I wonder if the candidate has any real experience whatsoever. RHS recently gave some very bad advice about the using statement and is so incorrect that I frankly might immediately flip the no-hire bit if we were in an interview. In fact, he is so off the mark, I hardly know where to begin, but I'll just note that:
Go, read -- it's really just scary in an entertaining way."
I found it interesting to read (assuming you go back and read/re-read the original post), for a number of reasons:
Then he gets on his high horse: "Anyone that even hints at suggesting the use of a finalizer instead of using/Dispose..."
Read the blog entry, Luke. I advocated calling Dispose directly instead of relying on using. At no point did I say: "the object will get GC'd by the CLR later, so don't even bother to call Dispose." Of course if he was looking for a target that day, and happened to choose me...
I'll give him credit for the phrase that follows; I'm going to get bumper stickers and mugs printed up that say, "I'm not qualified to touch code on a large dynamic system, let alone have a position of higher authority."
I love it. Ok, I'll leave the rhetoric alone; back to content:
He finally gets to some meat (instead of rhetoric or blatant misquoting) on his last point, and he's right: if someone is going to have trouble with using using correctly, they'll have to learn the proper way to use try/finally to trigger the Dispose call. If they can't set up a try/finally properly, maybe they shouldn't be in this line of work.
In any case, I'm sorry to hear that he won't be hiring me, and will go back to my position as a checker at Safeway.
RHS
PS--Of course, maybe he was having a bad day as a "practice manager" and chose me as a target!
Via FeedShow.com