Sermon at the Soup Kitchen

Friends, I'd like your expert opinions on a C++ article I'm writing. I plan to submit it in a few hours to the community website Kuro5hin (http://www.kuro5hin.org/). I will leave it in "edit mode" until tomorrow, so I'll still have time to revise it to reflect your comments. From my introduction: "When I talk to those guys about how to write better code I have the sense that their experience of me is like going to church." "Many people go to church. How many are without sin?" "But I didn't learn to preach because I studied at the seminary. It is because I was a derelict on skid row until I was saved by..." "... smart pointers and automated testing." Sermon at the Soup Kitchen On C++ Software Quality and Institutional Resistance to Change http://www.goingware.com/tips/smart-pointers-and-automated-testing/ My questions: 0. Am I correct in what I say about the various Boost smart pointers? I say shared_ptr isn't thread safe. Am I right? If so, does Boost::Thread offer a way to make thread-safe reference counted smart pointers? If so, please point out any documentation to me so I can link to it from my piece. 1. I was hired specifically to revamp the company's software development methodology so they achieve high reliability, as even a single incident of failure of their Computer Aided Design product could cost end-users as much as a million dollars if the products designed by it failed to work as designed when manufactured. But when the chips were down, "Yoyodyne" founder "Jack" ordered everyone, me included, to cut corners so we could implement features faster. This led to my ultimatum quoted in the article. I'm now looking for a new job. My question for you is, given that Jack was not a programmer, and didn't have the first clue about how to achieve software quality (nor did the rest of the staff), how could I have overcome the company's resistance to change in such a way that it wouldn't have come to an ultimatum? 2. I have a few links to smart pointer and automated testing pages early in the article, but not many. Can you suggest others? I'm especially interested to hear about other test frameworks, not just for C++ but for *any* language, so that people who don't use C++ still have something to gain by reading my piece. How about frameworks for other kinds of testing than unit testing, like acceptance or integration testing? 3. Can you pick any nits with my assertions about C++ or the various smart pointers? Do you agree with me that the following is a case where auto_ptr is the best choice? Can you think of others? http://www.goingware.com/tips/smart-pointers-and-automated-testing/#when-to-... 4. I say early on that neither auto_ptr nor the Boost smart pointers provide all the different kinds of smart pointers one would want. What other kinds can you think of? How would they be implemented? I already have a couple completely implemented, and keep thinking of more, but I'd like ideas for others to discuss in my next installment. I won't tell you my answers yet because I want to stimulate discussion. I'll be placing all my smart pointers under the Boost license and posting a tarball on my website. You're welcome to include them in Boost, but if you don't want to, I'll host them at Sourceforge. 5. I've already been over my article several times looking for grammar and spelling errors, but if you can still find any, please let me know. 6. If you were a sinner before you came to my soup kitchen's service, have I saved your soul? If not, how could I have made my case more effectively? Was your free, hot supper satisfying? Would you come back for more? My article is going to appear in two or three installments at Kuro5hin. The next one, yet to be written, will cover automated testing. I'm also going to cover exception safety, which I feel I have finally come to understand. Depending on how long the second installment is, exception safety might appear there, or in a third installment. I'll discuss the connection between RAII, smart pointers and exception safety. As I deliver each sermon, I'll add it at the link above. Look for them in the table of contents, quoted below. Part I * How I Was Saved o Smart Pointers o Automated Testing * Ultimatum * auto_ptr and Its Alternatives o auto_ptr Doesn't Work for Arrays o Take My Memory. Please! o The Simple Solution o Standard Template Library-Compatible Smart Pointers o That's Great! Is It Thread-Safe Too? o But Arrays? What About the Arrays? o So is auto_ptr Completely Useless? o Without Further Ado * The Right Tool for the Job Part II * The Path of Righteousness Part III * The Holy Grail Thanks for your help! -- Michael D. Crawford GoingWare Inc. - Expert Software Development and Consulting http://www.goingware.com/ crawford@goingware.com Tilting at Windmills for a Better Tomorrow.

Michael D. Crawford wrote:
0. Am I correct in what I say about the various Boost smart pointers? I say shared_ptr isn't thread safe. Am I right?
No. http://boost.org/libs/smart_ptr/shared_ptr.htm#ThreadSafety

On 11/2/05, Michael D. Crawford <crawford@goingware.com> wrote: 0. Am I correct in what I say about the various Boost smart pointers? I
say shared_ptr isn't thread safe. Am I right? If so, does
No. See Peter's response. 3. Can you pick any nits with my assertions about C++ or the various
smart pointers? Do you agree with me that the following is a case where auto_ptr is the best choice? Can you think of others?
http://www.goingware.com/tips/smart-pointers-and-automated-testing/#when-to-...
You say in this section that shared_ptr doesn't have a reset() method. It does. See: http://boost.org/libs/smart_ptr/shared_ptr.htm#Synopsis So it would work just as well as auto_ptr in this situation. There's also a typo in the comment at the top of the sample app: * ... and scoped_ptr, which permits them * by using reference counting" This should be "shared_ptr" not "scoped_ptr" (which you mention first). Also, I think the example could use some rewriting so it more clearly illustrates the strengths of the smart pointer approach. I'd suggest throwing an exception and using a class with a "noisy" ctor/dtor so the user can see what is happening without using valgrind. I'm attaching a stab at this to this message. 4. I say early on that neither auto_ptr nor the Boost smart pointers
provide all the different kinds of smart pointers one would want. What other kinds can you think of? How would they be implemented? I already have a couple completely implemented, and keep thinking of more, but I'd like ideas for others to discuss in my next installment. I won't tell you my answers yet because I want to stimulate discussion.
There is a flexible policy-based smart pointer ("policy_ptr") scheduled for review soon that provides customization options for things like the reference count implementation, copy semantics, etc. I suspect you've implemented some similar things. 6. If you were a sinner before you came to my soup kitchen's service,
have I saved your soul? If not, how could I have made my case more effectively? Was your free, hot supper satisfying? Would you come back for more?
I'd suggest correcting the factual errors, and linking to some other resources on smart pointers (e.g. Sutter, Alexandrescu, et al) before I could say I would walk away from the table feeling full :) Sutter on auto_ptr: http://www.gotw.ca/publications/using_auto_ptr_effectively.htm Alexandrescu on Smart Pointers: http://www.informit.com/articles/article.asp?p=31529 -- Caleb Epstein caleb dot epstein at gmail dot com
participants (3)
-
Caleb Epstein
-
Michael D. Crawford
-
Peter Dimov