I first heard about Bill Wagner in October 2008 when listening to .NET Rocks! episode 223 in which he spoke about his Effective C# book and the More Effective C# title he was in the process of writing. He sounded like he knew what he was talking about and knowing the More Effective C# book would’ve been released already I pointed my browser at Amazon and purchased both books.
I’ve since spent time reading through the first of the two books and thought that it would be interesting, after the suggestion from Craig Murphy, to write a review from the perspective of a developer that is relatively new to C#.
Why should you buy this book?
If like me you are pretty new to C# and managed code in general, but you’ve got a good grasp of the principles behind software development then this book is a great resource. Ok some of the topics are a little more advanced but this just means the book stays relevant as you master your craft. You’ll find that you read the book several times cover to cover unlocking new content each time, improving both your C# skills and your understanding of the dos and don’ts of the language in the process.
At a glance
The book is split into 6 chapters each targeting different aspects of the language and how to tweak your code to get the best from the framework. The introduction to the book suggests the reader have experience with one of the C languages at a bare minimum, and to really gleam the best from it some more extensive knowledge of MSIL, the C# compiler as well as the JIT compiler, all of which I knew almost nothing about. Following up from the pre-requisites the book summarized each of the 6 chapters explaining the sorts of things that would be tackled, and completed with the styling and code conventions section describing what the reader should expect to see whilst reading. I ended up reading the introduction a few times before diving into the book although only to try get into the writer’s frame of mind.
Chapter 1 covers C# language elements talking about System.Object, the base of all the types you write, and gives great tips to improve your classes. It uses simple topics like “Always Use Properties Instead of Accessible Data Members”, “Prefer readonly to const”, “Always Provide ToString()” and “Prefer foreach loops” along with in-depth reasoning and demo code to help explain each one. All of the points in this chapter flowed well, seemed to make a great deal of sense and after reading through it I knew of several places I could put the topics to good use. To me that was an instant win!
The second chapter touches on .NET resource management, looking at the Garbage Collector and how to minimise the work it has to do. Coming from a Pascal/Delphi background I only knew the basics about it, but the chapter introduction described the way it worked in enough detail to enable me to understand each of the points. It helps with baby steps like “Prefer Variable Initializers to Assignment Statements”, “Initialize Static Class Members with Static Constructors”, “Utilize Constructor Chaining” and “Utilize using and try/finally for Resource Cleanup” then brings out the big guns with “Minimizing Garbage” and “Minimizing Boxing and Unboxing” to top off the chapter. As with the previous chapter the points all made sense and once again I could see how they could be applied in several places throughout the projects I’ve work on.
Chapter 3 looked at ways of expressing designs with C# helping developers to communicate their intent to those who use or maintain the software they create. All of the topics in this chapter like “Prefer Defining and Implementing Interfaces to Inheritance”, “Distinguish Between Implementing Interfaces and Overriding Virtual Functions”, “Express Callbacks with Delegates”, “Avoid Returning References to Internal Class Objects” and “Avoid ICloneable” touched on some really simple and obvious statements – making code that is understandable and less likely to be used incorrectly.
In the forth chapter Bill turned his attention to creating binary components and how by following certain steps any developer can create assemblies which are usable from any of the .NET languages with a surface area that doesn’t attract change. This means by implementing such patterns as “Prefer CLS-Compliant Assemblies”, “Prefer Small, Simple Functions”, “Prefer Smaller, Cohesive Assemblies”, “Limit Visibility of Your Types” and “Create Large Grain Web APIs” you can, dependant on your assembly de-coupling, replace individual assemblies without the need to rebuild your whole solution. This chapter on its own is worth its weight in gold as after reading it I was able to address a number of issues in the code I was working on that, until now, had forced me to do a full build each and every time.
Chapter 5 looks at ways of working with the framework, uncovering some not-so-hidden gems that you can leverage and helps to reduce the code you write in the long run. This chapters topics “Prefer Overrides to Event Handlers”, “Use the Standard Configuration Mechanism”, “Utilize and Support Data Binding”, “Match Your Collection to Your Needs” and “Prefer Datasets to Custom Structures” in themselves have helped me save many lines of code, and opened my eyes to some of the time savings I could make in the future. In addition Bill looked at using reflection with “Utilize Attributes to Simplify Reflection” and “Don’t Overuse Reflection” and touched on exception handling with “Create Complete Application-Specific Exception Classes” to complete the chapter.
In the last chapter Bill talks about all the miscellaneous topics that didn’t seem to fit in chapters 1 through 5. This includes “Prefer the Strong Exception Guarantee”, “Minimize Interop”, “Prefer Safe Code” and “Prepare for C# 2.0” along with a bunch of other really great topics that will help you in your day to day development life.
All-in-all this book has been a real eye opener and has taught me a great many things which I have been able to apply in the real world. I would recommend it to the beginner all the way through to the seasoned developer as it’s a great learning tool and reference book all in one. I for one have returned it on more than one occasion to refresh my memory on some of the great topics.
Some useful links