Tuesday, March 16, 2010

Want to become a better VB.NET programmer? Read C# In Depth!

I have been focusing lately on improving my VB.NET expertise. After reviewing the available books I decide to re-read C# in Depth by Jon Skeet (first edition) and really focus on how that translates to VB.NET. C# in Depth is definitely more theoretical than most other books. It is probably one of the few books that help to prepare for reading the actual C# language specification. I strongly recommend porting his example code to VB.NET and playing with these examples. Don’t just read the book.


I’d suggest start by working with a snippet editor. I spent a little time porting Jon’s snippet compiler/runner Snippy to be a VB.NET and will blog about that later in a bit. Alternatively, LinqPad is an excellent Snippet compiler and isn’t just for Linq.


Jon also spent a day presenting a deep dive into C# in Copenhagen and made the material available here. A good chunk of this actually goes deeper in depth than his book. It also just how well he did on focusing his book when you see how tangential he is in these talks. For the most part I would suggest, reading the book first. However, when you reach the promised land of Linq in the book, I suggest you view the first part of Session 6 which illustrates the characteristics of Linq using people as query operators. This beautifully illustrates important concepts like streaming vs buffering and deferred vs immediate execution.


For the most part, Skeet’s examples work well in VB.NET. One gotcha is that in VB.NET events must be associated with an instance of a class whereas Jon’s examples deal with a static (static in C# is synonymous with Shared in VB) class with static methods. Simply changing his examples to use an instance of a class allowed his examples of events to work properly. A more significant problem is the lack of support for iterator blocks with VB.NET 10 (i.e. VS 2010). This means most of chapter 6 is not directly applicable to VB. I also found the type inference in C# to be superior to VB.NET. A few of Skeet’s Linq examples had to be tweaked or explicit typing added to work properly. I found this problem mostly in determining the type of the loop variable in a For Each block. I worked through the examples using VB.NET 10 which rounds out VB.NET’s support of Lambdas. Using any earlier version of VB is likely to be frustrating. I have a nearly complete set of converted examples for those who are interested.


Now to a review of the book itself. Jon creates a narrative structure by showing the evolution of C# from the perspective of examining the language and compiler underpinnings. C# moved to language support for lambdas in two steps. First, it introduced anonymous methods in C# 2 (VS 2005) and subsequently added full lambda support in C# 3 (VS 2008). Covering both approaches is somewhat redundant for C# purposes, and even more so for VB which moved to lambda syntax without an intermediate step. Although the C# syntax is redundant, Jon’s coverage does minimize the redundancy. Don’t skip Chapter 5,which covers anonymous methods. His coverage of closure in the section on capturing variables is excellent and representative of his approach to the more challenging theoretical parts of .NET programming. Once you grasp his coverage of capturing variables, he has a nice followup article on the web here. He provides a gentle approach that warns of the complexity of the material and doesn’t get lost in academic jargon. Lambda calculus has been a favorite topic in computer science theory for many years. Jon doesn’t lose precision in his writing by trying to write in a style that is down to earth as possible. He is a stickler for consistent usage of terminology.


A key technique in the book is analyzing the code generated by the compiler. Although new language syntax has been created for C#, it is amazing just how much extra work is done by the compiler to make Linq and its underpinnings happen. Jon makes it clear what is part of the language, what are extension methods, and what is compiler magic. An important note for VB programmers is that VB does introduce more syntax directly into the language relative to C#. Jon happens to favor the C# approach which has a smaller set of syntax.


Although the focus of the book is on Linq and its underpinnings, you will learn a good deal about two crucial topics: deferred execution and functional programming. These topics will, in my opinion, reshape the future of .NET development. The increasing availability of multiple CPUs in Windows will demand that enterprise programmers take advantage of these features. Deferred execution via async enumerators has application to threading that Jeffrey Richter and the parallel processing folks within Microsoft have exploited recently, particularly in .NET 4.0. See Richter’s CLR via C# third edition for threading coverage that is all-new to readers of previous editions. Interest in functional programming has exploded in recent years and a lot of that is due to the expectation that functional programming will allow programs to be written in a way that increases parallelism. The introduction of PLinq is one way that using a functional approach to .NET can be accomplished while still using VB.NET. C# in depth will prepare you for a functional approach that, while still VB.NET is idiomatically and conceptually quite different.

1 comment:

  1. Great post full of useful tips! My site is fairly new and I am also having a hard time getting my readers to leave comments. Analytics shows they are coming to the site but I have a feeling “nobody wants to be first”.
    net software development

    ReplyDelete