C/C++ Users Journal, October 2005

Programmer's Book Review

By Peter N. Roth

Peter Roth is the president of Engineering Objects International, producers of commercial and custom C# components. Peter can be contacted at http://www.engineeringobjects .com/ and pete.roth@verizon.net.

C# Precisely
Peter Sestoft and Henrik I. Hansen
The MIT Press, 2004
204 pp., $19.95
ISBN 0262693178

Effective C#: 50 Specific Ways to Improve Your C#
Bill Wagner
Addison-Wesley, 2004
336 pp., $39.99
ISBN 0321245660

Having read through The C# Programming Language, by Anders Hejlsberg et al., and feeling quite proud of myself for that, I confess I approached C# Precisely with a bit of a chip on my shoulder. I questioned what I could possibly learn from a shorter book. It took all of two pages for me to realize that this new text says a lot worth reading, in little space. It wasn't until I'd been through the text that I discovered the (faintly printed) blurb on the back cover from Hejlsberg stating that "nothing was left out." Harumph!

There is no CD with this book, but like many modern presentations, it is the starting point for studying offline, with supplementary material and errata on a web site. And the supplements are great. The text is laid out with expository material on the left-hand leaves, and example code on the right-hand leaves. The text begins with brief instructions on compiling and executing C# programs and proceeds through the elements of the language to a conclusion comparing C# with Java.

As the title could as easily have been "C# Concisely," it reminded me of the classic Pascal User Manual And Report by Kathleen Jensen and Niklaus Wirth. The format is that of an engineering text, by which I mean that the text makes a statement in brief, clear text, once, and it is left to the student to read and understand that statement by working through the examples. I find that the labor I put in, learning, is what helps me discover the concepts for myself. There are no space-wasting prechapter outlines giving you the "Here's what we're going to tell you" paragraphs, and no post-chapter summaries of the form "Here's what we told you, and here's what's coming up in the next chapter." It's all meat. The diagrams are clear and a pleasure to examine.

The programming notation will astound readers expecting Hungarian notation. One character variable names! One character concept designators! It's refreshing, but this permits such density of meaning in code that you will want to download the example code and examine it in your programming IDE. Since the text discusses C# 2.0, I decided to explore the examples with the (free) beta product from Microsoft. You'll want to download the code anyway because a few of the examples are incomplete in the text. Versions are available for both .NET and Mono.

I highly recommend this book as one from which to learn to program using C#. But due to the bracing brevity of the text, I can only recommend it if, and only if, you are already a programmer.

Following the pattern established in 1935 by Strunk and Tenny in Elements of Style, Bill Wagner successfully extends Scott Meyers's series of "Effective" books with Effective C#. The pattern is: several chapters, each of which addresses a particular area of practice. Each chapter begins with a discussion of the area to be addressed. Within each chapter, aspects of the area are addressed with "Items," in which the recommended practice for that item is both discussed and illustrated.

Wagner makes manifold use of the words "prefer to" rather than "you must," which allows the developer to use judgment in those cases when "it depends" is an appropriate approach. There is no CD and no downloadable code; the text is it. You must engage the code as written. There is, however, a lot of additional material in Wagner's blog at http://www .srtsolutions.com/public/blog/79005/.

I picked a few Items that I thought I knew something about so I could judge my progress in learning C# and .NET against best practices.

Chapter 1 treats C# Language Elements, and the first Item is almost a requirement of anyone writing about object-oriented programming: "Always Use Properties Instead of Accessible Data Members." In addition to making programs easier to modify, Wagner demonstrates how easy it is to make classes thread-safe using properties rather than public data members.

Item 8: "Ensure That 0 Is a Valid State for Value Types" seemed to me to invoke the arguments that database developers throw around in their "null versus 0" controversy, so this one was a toss-up for me.

In the chapter ".NET Resource Management," I found Item 14: "Utilize Constructor Chaining" a practice that I could apply immediately. Because C# lacks default arguments, I had been factoring out common constructor code into an initializer function. Wagner convinced me that it is better to use the construct:

  class A {
    private int field;

    A() : this( 42 ) {
    }

    A( int aField ) {
      field = aField;
    }
  }

In Item 16: "Minimize Garbage," the recommendation is to hoist variables out of methods and make them instance variables to avoid unnecessary creation and destruction of objects. That seems obvious to me, but then again, it never hurts to state it.

"Creating Binary Components" (Chapter 4) contained Item 31: "Prefer Small, Simple Functions." Here, we lean on the compiler rather than our old code tricks. This is something that I've been trying to do all along—the smaller the better.

In the "Miscellaneous" area, I found a point of quasidisagreement in Item 46: "Minimize Interop." Although Wagner mainly discussed interoperation with COM objects, Microsoft offers Visual Studio Tools for Office, which are tools to deal with interoperation between your programs and Microsoft Office. In this case, one avoids interoperation by again leaning on the tools.

Readers are expected to have some experience in object-oriented programming, a C-syntax language, web services and ADO.NET, assemblies, Microsoft intermediate language, and executable code. This is too much to expect; an intelligent, curious, patient reader will be well served with this text. Cavils: leading underscore used to identify instance fields, and several run-on words that could easily have been found with a spell checker (for $40, I expect better copy editing). Nevertheless, I'll keep the book, and go back and study the rest of the items. q