Manning Publications has just released a new title, Metaprogramming in .NET by Kevin Hazzard and Jason Bock. The PDF eBook is currently available in its final form (ePub and Kindle formats due on January 15th) and the print book will be available on December 28th. I recently had the opportunity to ask them a few question about the release of the book.
About the Authors
Kevin Hazzard is a Microsoft MVP, consultant, teacher, and developer community leader in the mid-Atlantic USA. Jason Bock is an author, Microsoft MVP, and the leader of the Twin Cities Code Camp.
Alvin: What motivated you to write this book?
Jason: I’ve been fortunate with the books I’ve written in the past that they’ve been based on topics that I was passionate about. However, it takes a lot of effort to write a book, and that’s why I decided to stop doing them for a while after I co-authored “Applied .NET Attributes” in 2003. I said to myself that I wouldn’t write another book unless I loved its theme, and that’s exactly what happened with this book. Metaprogramming sounds like a fancy term, but to me it comes down to using techniques such that you write less code to do more. I wanted to take some of the fear away from Reflection and other dynamic coding techniques that some people have in the .NET world where both C# and VB are primarily used as static languages. Being able to write a book that could do that was very appealing to me.
Kevin: The opportunity to work with Jason was a strong motivator for Kevin. Also, we looked around the blogosphere and the book-o-sphere (is that a real word) and saw that there just wasn’t much available regarding metaprogramming. Some books do exist but none that we could find that addressed anything beyond specific tools and techniques. Our book is also carved up into tools and techniques at the chapter level but we tried hard to make the writing address more fundamental questions about metaprogramming. We wanted to convey that metaprogramming isn’t just a set of tools. It’s a mindset: a way of thinking about software development from the start of design all the way through to the end. We didn’t aim to convey that metaprogramming should be used for all your development though. Software creation should be approached like the development of a car, for example. Some parts are rigid by design, some parts are flexible and some parts can be classified as supple. Combining these in the right way makes the automobile safe, durable and comfortable. Software can be safe, durable and comfortable, too. We simply looked at the way we were using metaprogramming in our own designs and realized that there should be a book that helped others to understand it, too.
Alvin: Who do you see at your target audience? Is the book intended for expert developers only?
Kevin: I remember the first phone call we had with the folks at Manning about this book. Jason and I wrestled with this very question during that call. This isn’t a book with mass appeal like those from Jon Skeet or Jesse Liberty. But it’s also not strictly for experts. Metaprogramming is a way of thinking about software development and you don’t have to be an expert developer to think that way. It’s true that many of the best software developers you’ll ever meet know how to metaprogram and, more importantly, they know when to do it. But this book isn’t necessarily aimed at them either. The content is aimed at the middle: those who have good development skills and those who understand that adaptable, flexible code is usually better than rigid, brittle code. After all, we call it SOFT ware. This book is for those developers who want to develop software that remains pliable after it ships and you don’t have to be an expert to appreciate the value of that.
Alvin: How did you divide up the topics in the book? Do you each have any particular facets of metaprogramming that are strengths or of particular interest?
Jason: That was pretty easy. Kevin and I had strengths in certain areas of the material, so we assigned the chapters based on that level of understanding. It ended up being close to a 50/50 split. Kevin did the introduction, CodeDOM, T4 and DLR chapters and I handled the rest.
Kevin: Our skills and experience were somewhat different. Jason has a lot of experience deep in the .NET CLI that helped him write and Kevin brought real-world experience with technologies like the DLR and T4 to his writing. I think we learned a lot from each other as we wrote the book which was great. A lot of co-authored technology books have a disjoint feel to them because the individual authors deliver their chapters individually to the publisher. But we worked hard to make sure that ours didn’t feel that way. We had weekly conference calls with each other and with our development editors. We edited each others chapters and code. It was a great experience that brought our individual strengths to bear in a collaborative way.
Alvin: Which topics in the book do you use most in your day-to-day development work?
Kevin: Nearly every .NET developer uses the Reflection API every day, whether they realize it or not. That’s why we opened the book with an examination of it. Reflective, metadata-driven programming techniques add tremendous value to the software development process for most developers, including us. This is why WinRT is so important to the future of Microsoft’s tools. Moving the metadata model down into the operating system core means that we can share conventions and abstract expressions between programs after the moment of compilation. That’s a fundamentally "meta" way of thinking and working. I suppose that the other common practices that we use the most are Expression Trees and IL re-writing. Expressions are used throughout LINQ, of course, but they are used rather statically in that context, i.e. not as a metaprogramming tool. However, Expression Trees are the underpinning of a lot of dynamic capability that I add to my code. I used generic Expression<T> and Lazy<T> all over the place in my code, in places where I need flexibility and performance. As we mentioned throughout the book, higher-order functions and lazy evaluation are the hallmarks of functional programming. As it turns out, metaprogramming and functional programming intersect greatly through classes like Expression<T> and Lazy<T>. So I find myself using them all the time both for functional reasons and to make my code more pliable and adaptable. Most of the IL-rewriting that I do is through IOC/DI frameworks that allow for AOP interceptors and such. But I occasionally find myself doing that by hand, so to speak.
Alvin: We heard a lot about the Dynamic Language Runtime when it was released several years ago. Where does it stand today?
Alvin: Did you have access to Microsoft’s language and compiler teams while writing the book?
Kevin: I think we had better access than the average developer and even the average MVP. They knew that we were working on the book and pulled us aside at the MVP Summit to talk about our work. We also exchanged a bunch of e-mails and had some phone calls with various team members. Of course, we stayed true to our Non-Disclosure Agreements and only wrote about the material that was public or was going to become public. The various relevant teams at Microsoft working on WinRT were under a very strict gag order of sorts so we didn’t learn much about it until the rest of the world did. In hindsight, I wish we had shifted more of the book’s content to WinRT and Roslyn based on what we learned but it was a bit late for that. Furthermore, the metaprogramming capabilities in the new development stack are very different from .NET so we would have needed to rethink the book to address all of the changes in full. In the end, we ended up with a very .NET-centric book with an eye toward the future.
Alvin: What does the future hold for metaprogramming in .NET? Do you see some new tools and techniques coming down the line?
Jason: I see Project Roslyn having a big impact in the .NET world, even if a fair amount of .NET developers never use those APIs directly. Having a common set of APIs to analyze, change and compile code will open more doors for .NET developers to do some amazing things. There’s a lot of cool work already done in the .NET world to facilitate metaprogramming, but I can see Roslyn being a core part of that work in a lot of areas.
Kevin: .NET has had rich metadata and a delegate class since day one. Metaprogramming is baked into the soul of .NET, you might say. With the advent of Expression Trees, metaprogramming took a big leap forward. The next frontier is access to the Abstract Syntax Tree (AST) of code. In this respect, we see hints of what will be available in the future through the Roslyn project. The capability to inspect ASTs will come first, allowing some truly revolutionary development tools to be created. The logical step after that is opening access to the ASTs at runtime. When that happens and after we’ve addressed the obvious security concerns, metaprogramming will become much more popular. Reflection against metadata to drive program behavior is fairly common in .NET development today. When ASTs, which are really just algorithmic metadata, become available, we expect developers to embrace this way of working as a natural extension of what they already know. You see this mindset emerging today in jQuery, for example, albeit at a higher level. It’s common in jQuery to test for the existence of a function or even attach one on the fly to accomplish a task. This is a hallmark of dynamic languages but will soon become a common capability in traditionally statically-typed languages. After all, functions are just building blocks that are made up of smaller building blocks. The assembly of functions in the right order and at the right time is really the only thing that separates traditionally imperative languages from declarative ones. The lines between those types of languages are blurring and it will be metaprogramming frameworks like Roslyn and its derivatives that drive it.
Metaprogramming in .NET: Kevin Hazzard, Jason Bock: 9781617290268: Books