A New Internet Library: Add Your Website/Blog or Suggest A Website/Blog to our Free Web Directory http://anil.myfunda.net.

Its very simple, free and SEO Friendly.
Submit Now....

Monday, December 1, 2008

Book Review: Programming C# 3.0 by Jesse Liberty and Donald Xie

Resources

Disclaimer

One reader commented that a previous book review was too full of "this is only my personal opinion" and other such disclaimers. I think it's still important to declare the situation, but I can see how it can get annoying if done throughout the review. So instead, I've lumped everything together here. Please bear these points in mind while reading the whole review:

  • Obviously this book competes with C# in Depth, although probably not very much.
  • I was somewhat prejudiced against the book by seeing that the sole 5-star review for it on Amazon was by Jesse Liberty himself. Yes, he wanted to explain why he wrote the book and why he's proud of it, but giving yourself a review isn't the right way to go about it.
  • I've seen a previous edition of the book (for C# 2.0) and been unimpressed at the coverage of some of the new features.
  • I'm a nut for technical accuracy, particularly when it comes to terminology. More on this later, but if you don't mind reading (and then presumably using) incorrect terminology, you're likely to have a lot better time with this book than I did.
  • I suspect I have higher expectations for established, prolific authors such as Jesse Liberty than for newcomers to the world of writing.
  • I'm really not the target market for this book.

Okay, with all that out of the way, let's get cracking.

Contents and target audience

According to the preface, Programming C# 3.0 (PC# from now on) is for people learning C# for the first time, or brushing up on it. There's an expectation that you probably already know another language - it wouldn't be impossible to learn C# from the book without any prior development experience, but the preface explicitly acknowledges that it would be reasonably tough. That's a fair comment - probably fair for any book, in fact. I have yet to read anything which made me think it would be a wonderful way to teach someone to program from absolute scratch. Likewise the preface recommends C# 3.0 in a Nutshell for a more detailed look at the language, for more expert readers. Again, that's reasonable - it's clearly not aiming to go into the same level of depth as Accelerated C# 2008 or C# in Depth.

The book is split into 4 parts:

  • The C# language: pretty much what you'd expect, except that not all of the language coverage is in this part (most of the new features of C# 3.0 are in the second part) and some non-language coverage is included (regular expressions and collections) - about 270 pages
  • C# and Data: LINQ, XML (the DOM API and a bit of LINQ to XML), database access (ADO.NET and LINQ to SQL) - about 100 pages
  • Programming with C#: introductions to ASP.NET, WPF and Windows Forms - about 85 pages
  • The CLR and the .NET Framework: attributes, reflection, threading, I/O and interop - about 110 pages

As you can tell, the bulk of it is in the language part, which is fine by me and reflects the title accurately. I'll focus on that part of the book in this review, and the first chapter of part 2, which deals with the LINQ parts of C# 3.0. To be honest, I don't think the rest of the book actually adds much value, simply because they skim over the surface of their topics so lightly. Part 3 would make a reasonable series of videos - and indeed that's how it's written, basically in the style of "Open Visual Studio, start a new WinForms project, now drag a control over here" etc. I've never been fond of that style for a book, although it works well in screencasts.

The non-LINQ database and XML chapters in part 2 seemed relatively pointless too - I got the feeling that they'd been present in older editions and so had just stayed in by default. With the extra space available from cutting these, a much better job could have been done on LINQ to SQL and LINQ to XML. The latter gets particularly short-changed in PC#, with a mere 4 pages devoted to it! (C# in Depth is much less of a "libraries" book but I still found over 6 pages to devote to it. Not a lot, I'll grant you.)

Part 4 has potential, and is more useful than the previous parts - reflection, threading, IO and interop are all important topics (although I'd probably drop interop in favour of internationalization or something similar) - but they're just not handled terribly well. The threading chapter talks about using lock or Monitor, but never states that lock is just shorthand for try/finally blocks which use Monitor; no mention is made of the memory model or volatility; aborting threads is demonstrated but not warned about; the examples always lock on this without explaining that it's generally thought to be a bad idea. The IO chapter uses TextReader (usually via StreamReader) but never mentions the crucial topic of character encodings (it uses Encoding.ASCII but without really explaining it) - and most damning of all, as far as I can tell there's not a single using statement in the entire chapter. There are calls to Close() at the end of each example, and there's a very brief mention saying that you should always explicitly close streams - but without saying that you should use a using statement or try/finally for this purpose.

Okay, enough on those non-language topics - let's look at the bulk of the book, which is about the language.

Language coverage

PC# starts from scratch, so it's got the whole language to cover in about 300 pages. It would be unreasonable to expect it to provide as much attention to detail as C# in Depth, which (for the most part) only looks at the new features of C# 2.0 and 3.0. (On the other hand, if the remaining 260 pages had been given to the language as well, a lot more ground could have been covered.) It's also worth bearing in mind that the book is not aimed at confident/competent C# developers - it's written for newcomers, and delving into tricky issues like generic variance would be plain mean. However, I'm still not impressed with what's been left out:

  • There's no mention of nullable types as far as I can tell - indeed, the list of operators omit the null-coalescing operator (??).
  • Generics are really only talked about in the context of collections - despite the fact that to understand any LINQ documentation, you really will need to understand generic delegates. Generic constraints are only likewise only mentioned in the context of collections, and only what I call a "derivation type constraint" (e.g. T : IComparable<T>) (as far as I can tell the spec doesn't give this a name). There's no coverage of default(T) - although the "default value of a type" is mentioned elsewhere, with an incorrect explanation.
  • Collection initializers aren't explained as far as I can tell, although I seem to recall seeing one in an example. They're not mentioned in the index.
  • Iterator blocks (and the yield contextual keyword) are likewise absent from the index, although there's definitely one example of yield return when IEnumerable<T> is covered. The coverage given is minimal, with no mention of the completely different way that this executes compared with normal methods.
  • Query expression coverage is limited: although from, where, select, orderby, join and group are covered, there's no mention of let, the difference between join and join ... into, explicitly typed range variables, or query continuations. The translation process isn't really explained clearly, and the text pretty much states that it will always use extension methods.
  • Expression trees aren't referenced to my knowledge; there's one piece of text which attempts to mention them but just calls them "expressions" - which are of course entirely different. We'll come onto terminology in a minute.
  • Only the simplest (and admittedly most common by a huge margin) form of using directives is shown - no extern aliases, no namespace aliases, not even using Foo = System.Console;
  • Partial methods aren't mentioned.
  • Implicitly typed arrays aren't covered.
  • Static classes may be mentioned in passing (not sure) but not really explained.
  • Object initializers are shown in one form only, ditto anonymous object initializer expressions
  • Only field-like events are shown. The authors spend several pages on an example of bad code which just has a public delegate variable, and then try to blame delegates for the problem (which is really having a public variable). The solution is (of course) to use an event, but there's little to no explanation of the nature of events as pairs of methods, a bit like properties but with subscribe/unsubscribe behaviour instead of data fetch/mutate.
  • Anonymous methods and lambda expressions are covered, but with very little text about the closure aspect of them. This is about it: "[...] and the anonymous method has access to the variables in the scope in which they are defined:" (followed by an example which doesn't demonstrate the use of such variables at all).

I suspect there's more, but you get the general gist. I'm not saying that all of these should have been covered and in great detail, but really - no mention of nullable types at all? Is it really more appropriate in a supposed language book to spend several pages building an asynchronous file server than to actually list all the operators accurately?

Okay, I'm clearly beginning to rant by now. The limited coverage is annoying, but it's not that bad. Yes, I think the poor/missing coverage of generics and nullable types is a real problem, but it's not enough to get me really cross. It's the massive abuse of terminology which winds me up.

Accuracy

I'll say this for PC# - if you ignore the terminology abuse, it's mostly accurate. There are definitely "saying something incorrect" issues (e.g. an implication that ref/out can only be used with value type parameters; the statement that reference types in an array aren't initialized to their default value (they are - the default value is null); the claim that extension methods can only access public members of target types (they have the same access as normal - so if the extension method is in the same assembly as the target type, for instance, it could access internal members)) but the biggest problem is that of terminology - along with sloppy code, including its formatting.

The authors confuse objects, values, variables, expressions, parameters, arguments and all kinds of other things. These have well-defined meanings, and they're there for a reason. They do have footnotes explaining that they're deliberately using the wrong terminology - but that doesn't make it any better. Here are the three footnotes, and my responses to them:

The terms argument and parameter are often used interchangably, though some programmers insist on differentiating between the parameter declaration and the arguments passed in when the method is invoked.

Just because others abuse terms doesn't mean it's right for a book to do so. It's not that programmers insist on differentiating between the two - the specification does. Now, to lighten things up a bit I'll acknowledge that this one isn't always easy to deal with. There are plenty of times where I've tried really hard to use the right term and just not ended up with a satisfactory bit of wording. However, at least I've tried - and where it's easy, I've done the right thing. I wish the authors had the same attitude. (They do the same with the conditional operator, calling it "the ternary operator". It's a ternary operator. Having three operands is part of its nature - it's not a description of its behaviour. Again, lots of other people get this wrong. Perhaps if all books got it right, more developers would too.) Next up:

Throughout this book, I use the term object to refer to reference and value types. There is some debate in the fact that Microsoft has implemented the value types as though they inherited from the root class Object (and thus, you may call all of Object's methods on any value type, including the built-in types such as int.)

To me, this pretty much reads as "I'm being sloppy, but I've got half an excuse." It's true that the C# specification isn't clear on this point - although the CLI spec is crystal clear. Personally, it just feels wrong to talk about the value 5 as an object. It's an object when it's boxed, of course (and if you call any Object methods on a value type which haven't been overridden by that type, it gets boxed at that point) but otherwise I really don't think of it as an object. An instance of the type, yes - but not an object. So yes, I'll acknowledge that there's a little wiggle room here - but I believe it's going to confuse readers more than it helps them.

It's the "confusing readers more than it helps them" part which is important. I'm not above a little bit of shortcutting myself - in C# in Depth, I refer to automatically implemented properties as "automatic properties" (after explicitly saying what I'm doing) and I refer to the versions of C# as 1, 2 and 3 instead of 1.0, 1.2, 2.0 and 3.0. In both these cases, I believe it adds to the readability of the book without giving any room for confusion. That's very different from what's going on in PC#, in my view. I've saved the most galling example of this for last:

As noted earlier, btnUpdate and btnDelete are actually variables that refer to the unnamed instances on the heap. For simplicity, we'll refer to these as the names of the objects, keeping in mind that this is just short-hand for "the name of the variables that refer to the unnamed instances on the heap."

This one's the killer. It sounds relatively innocuous until you see the results. Things like this (from P63):

ListBox myListBox;  // instantiate a ListBox object

No, that code doesn't instantiate anything. It declares a variable - and that's all. The comment isn't non-sensical - the idea of some code which does instantiate a ListBox object clearly makes sense - but it's not what's happening in this code (in C# - it would in C++, which makes it even more confusing). That's just one example - the same awful sloppiness (which implies something completely incorrect) permeates the whole book. Time and time again we're told about instances being created when they're not. From P261:

The Clock class must then create an instance of this delegate, which it does on the following line:

public SecondChangeHandler SecondChanged;

Why do I care about this so much? Because I see the results of it on the newsgroups, constantly. How can I blame developers for failing to communicate properly about the problems they're having if their source of learning is so sloppy and inaccurate? How can they get an accurate mental model of the language if they're being told that objects are being instantiated when they're not? Communication and a clear mental model are very important to me. They're why I get riled up when people perpetuate myths about where structs "live" or how parameters are passed. PC# had me clenching my fists on a regular basis.

These are examples where the authors apparently knew they were abusing the terminology. There are other examples where I believe it's a genuine mistake - calling anonymous methods "anonymous delegates" or "statements that evaluate to a value are called expressions" (statements are made up of expressions, and expressions don't have to return a value). I can certainly sympathise with this. Quite where they got the idea that HTML was derived from "Structured Query Markup Language" I don't know - the word "Query"should have been a red flag - but these things happen.

In other places the authors are just being sloppy without either declaring that they're going to be, or just appearing to make typos. In particular, they're bad at distinguishing between language, framework and runtime. For instance:

  • "C# combines the power and complexity of regular expression syntax [...]" - no, C# itself neither knows nor cares about regular expressions. They're in the framework.
  • (When talking about iterator blocks) "All the bookkeeping for keeping track of which element is next, resetting the iterator, and so forth is provided for you by the Framework." - No, this time it is the C# compiler which is doing all the work. (It doesn't support reset though.)
  • "Strings can also be created using verbatim string literals, which start the at (@) symbol. This tells the String constructor that the string should be used verbatim [...]" - No, the String constructor doesn't know about verbatim string literals. They're handled by the C# compiler.
  • "The .NET CLR provides isolated storage to allow the application developer to store data on a per-user basis." I very much doubt that the CLR code has any idea about this. I expect it to be in the framework libraries.

Again, if books don't get this right, how do we expect developers to distinguish between the three? Admittedly sometimes it can be tricky to decide where responsibility lies - but there are plenty of clearcut cases where PC# is just wrong. I doubt that the authors really don't know the difference - they just don't seem to think it's important to get it right.

Code

I'm mostly going to point out the shortcomings of the code, but on the plus side I believe almost all of it will basically work. There's one point at which the authors have both a method and a variable with the same name (which is already in the unconfirmed errata) and a few other niggles, but they're relatively rare. However:

  • The code frequently ignores naming conventions. Method and class names sometimes start with lower case, and there's frequent use of horrible names beginning with "my" or "the".
  • The authors often present several pages of code together, and then take them apart section by section. This isn't the only book to do this by a long chalk, but I wonder - does anyone really benefit from having the whole thing in a big chunk? Isn't it better to present small, self-contained examples?
  • As mentioned before, the uses of using statements are few and far between.
  • The whitespace is all over the place. The indentation level changes all the time, and sometimes there are outdents in the middle of blocks. Occasionally newlines have actually been missed out, and in other cases (particularly at the start of class bodies) there are two blank lines for no reason at all. (The latter is very odd in a book, where vertical whitespace is seen as extremely valuable.) Sometimes there's excessive (to my mind) spacing Just as an example (which is explicitly labelled as non-compiling code, so I'm not faulting it at all for that):
    using System.Console;
    class Hello
    {
        static void Main()
        {
        WriteLine("Hello World");
       }
    }
    I promise you that's exactly how it appears in the book. Now this may have started out as a fault of the type-setter, but the authors should have picked it up before publication, IMO. I could understand there being a few issues like this (proof-reading code really is hard) but not nearly as many as there are.
  • There are examples of mutable structs (or rather, there's at least one example), and no warning at all that mutable value types are a really, really bad idea.

Again, I don't want to give the impression I'm an absolute perfectionist when it comes to code in book. For the sake of keeping things simple, sometimes authors don't seal types where they should, or make them immutable etc. I'm not really looking for production-ready code, and indeed I made this very point in one of the notes for C# in Depth. However, I draw the line at using statements, which are important and easy to get right without distracting the reader. Likewise giving variables good names - counter rather than ctr, and avoiding those the and my prefixes - makes a competent reader more comfortable and can transfer good habits to the novice via osmosis.

Writing style and content ordering

Time for some good news - when you look beyond the terminology, this is a really easy book to read. I don't mean that everything in it is simplistic, but the style rarely gets in the way. It's not dry, and some of the real-world analogies are very good. This may well be Jesse Liberty's experience as a long-standing author making itself apparent.

In common with many O'Reilly books, there are two icons which usually signify something worth paying special attention to: a set of paw prints indicating a hint or tip, and a mantrap indicating a commonly encountered issue to be aware of. Given the rest of the review, I suspect you'd be surprised if I agreed with all of the points made in these extra notes - and indeed there are some issues - but most of them are good.

Likewise there are also notes for the sake of existing Java and C++ developers, which make sense and are useful.

I don't agree with some of the choices made in terms of how and when to present some concepts. I found the way of explaining query expressions confusing, as it interleaved "here's a new part of query expressions" with "here's a new feature (e.g. anonymous types, extension methods)." It will come as no surprise to anyone who's read C# in Depth that I prefer the approach of presenting all the building blocks first, and then showing how query expressions use all those features. There's a note explaining why the authors have done what they've done, but I don't buy it. One important thing with the "building blocks first" approach is to present a preliminary example or two, to give an idea of where we're headed. I've forgotten to do that in the past (in a talk) and regretted it - but I don't regret the overall way of tackling the topic.

On a slightly different note, I would have presented some of the earlier topics in a different order too. For instance, I regard structs and interfaces as more commonly used and fundamental topics than operator overloading. (While C# developers tend not to create their own structs often, they use them all the time. When was the last time you wrote a program without an int in it?) This is a minor nit - and one which readers may remember I also mentioned for Accelerated C# 2008.

There's one final point I'd like to make, but which doesn't really fit anywhere else - it's about Jesse Liberty's dedication. Most people dedicate books to friends, colleages etc. Here's Jesse's:

This book is dedicated to those who come out, loud, and in your face and in the most inappropriate places. We will look back at this time and shake our heads in wonder. In 49 states, same-sex couples are denied the right to marry, though incarcerated felons are not. In 36 states, you can legally be denied housing just for being q-u-e-e-r. In more than half the states, there is no law protecting LGBT children from harassment in school, and the suicide rate among q-u-e-e-r teens is 400 percent higher than among straight kids. And, we are still kicking gay heroes out of the military despite the fact that the Israelis and our own NSA, CIA, and FBI are all successfully integrated. So yes, this dedication is to those of us who are out, full-time.

(I've had to spell out q-u-e-e-r as otherwise the blog software replaces it with asterisks. Grr.) I'm straight, but I support Jesse's sentiment 100%. I can't remember when I first started taking proper notice of the homophobia in the world, but it was probably at university. This dedication does nothing to help or hinder the reader with C#, but to my mind it still makes it a better book.

Conclusion

In short, I'm afraid I wouldn't recommend Programming C# 3.0 to potential readers. There are much better books out there: ones which won't make it harder for the reader to talk about their code with others, in particular. It's not all bad by any means, but the mixture of sloppy use of terminology and poor printed code is enough of a problem to make me give a general thumbs down.

Next up will be CLR via C#, by Jeffrey Richter.

Response from Jesse Liberty

As normal, I mailed the author (in this case just Jesse Liberty - I confess I didn't look for Donald Xie's email address) and very promptly received a nice response. He asked me to add the following as his reaction:

I believe the book is very good for most real-world programmers and the publisher and I are dedicated to making the next revision a far better book, by correcting some of the problems you point out, and by beefing up the coverage of the newer features of the language.

Also as normal, I'll be emailing Jesse with a list of the errors I found, so hopefully they can be corrected for the next edition.



Source Click Here.

No comments:

Post a Comment

Post your comments here:

Originals Enjoy