Monday, November 26, 2012

The all new coding guidelines for C# 3.0, 4.0 and 5.0 have been published!

After 50000 downloads and the recent final release of Visual Studio 2012 / C# 5.0 it was about time to do a thorough update of the C# Coding Guidelines. Although most of the guidelines have been preserved, I’ve tried to rewrite several of them using a less formal and less verbose writing style. I’ve also improved the rational for some of them, shortened the introduction section, but added a refactoring heuristic I picked up from this blog post.

Other minor improvements include getting rid of the vague term identifier, applying the correct usage of the terms arguments and parameters, adding more cross-references between guidelines and adding various links to relevant articles. On the esthetic side, I’ve changed all code snippets to Consolas, reduced the line-spacing a bit, and changed the accent color to allow this version to stand out more from previous versions.

So which guidelines were changed?

  • Added a warning to AV1001 “Only create a constructor that returns a useful object” about potentially violating AV1000 and AV1561.
  • AV1008 “Mark classes that only contain static members as static” now promotes avoiding static classes at all rather than just marking it as static.
  • Added a reference to the Command Query Separation principle to AV1105 “Use a method instead of a property”.
  • Added a reference to the new .NET 4.5 read-only collection interfaces to AV1130 “Return an IEnumerable<T> or ICollection<T> instead of a concrete collection class”.
  • Renamed AV1135 “String, list and collection properties should never return a null reference” to include methods and parameters in addition to properties
  • Rephrased AV1140 “Consider replacing properties using primitive types to use rich value objects” to clarify that I was talking about domain-specific types.
  • Remove the incorrect examples from AV1515 “Don’t use "magic” numbers”
  • Renamed AV1521 “Initialize variables at the point of declaration” to make it clear we want variables to be declared and initialized as a late as possible.
  • Removed the 'method' part of AV1532 “Don’t use nested loops in a method” to say that nested loops should be avoid everywhere.
  • Renamed AV1535 “Add a block after all flow control keywords, even if it is empty” so that it includes concrete keywords rather than the vague 'flow control keywords'
  • Added a reference to AV1553 “Only use optional parameters to replace overloads” about Eric Lippert’s post on optional parameter corner cases.
  • Removed an example from AV1720 “Name methods using verb-object pair” because it conflicted with some of the LINQ extension methods introduced in .NET 3.0.
  • Rewrote and renamed AV1725 “Name namespaces according a well-defined pattern” to clearly explain the valid aspects of a namespace rather than emphasizing a particular pattern.
  • Changed the example in AV1735 “Use a verb or verb phrase to name an event” to use a generic event handler delegate.
  • Added a reference to to the .NET 4.5 read-only collection classes to AV1800 “Consider using Any() to determine whether an IEnumerable<T> is empty”. 
  • Added a statement to AV2207 “Don’t hardcode strings that change based on the deployment” that deployment-specific settings should be in the application configuration files rather than hard-coded or in a custom configuration file.
  • Added a remark to AV2400 “Use a common layout” not to interweave the from expressions with where conditions.

And what did I add?
The following guidelines have been added, of which, for obviously reasons, most are related to C# 5.0.

  • Added AV1137 "Define parameters as specific as possible" to prevent people from passing in entire objects even though a method might only need a specific property’s value.
  • Added AV1215 "Properly handle exceptions in asynchronous code" to explain the differences between exceptions raised in Actions and await blocks.
  • Added AV1523 "Initialize each variable on a separate line" to prevent some ugly code.
  • Added AV1739 "Use an underscore for irrelevant lamba parameters" as suggested by James Lanng.
  • Added AV1755 "Postfix asynchronous methods with Async of TaskAsync"
  • Added AV1820 "Only use async for low-impact long-running activities"
  • Added AV1825 "Prefer Task.Run for CPU intensive activities"
  • Added AV1830 "Beware of mixing up await/async with Task.Wait"
  • Added AV1835 "Beware of async/await deadlocks in single-threaded environments"

“But wait, didn’t you remove anything?”
Of course I did! Why do you think the number of pages went from 31 to 27? Well, yes, reducing line-spacing was part of that, but I actually did remove some guidelines.

  • Removed AV1120 "Use a public static readonly field to define predefined value objects " since I think it is not that important and not used very often.
  • Removed AV1245 “Don’t add extension methods to the same namespace as the extended class” because I think it is pretty farfetched if you create a class in a .NET namespace.
  • Removed AV1526 “Use an enumeration instead of a list of strings if the list of values is finite” since enumerations are so common these days, I hope nobody thinks of an alternative solution.
  • Merged AV1546 “Prefer conditional statements instead of simple if-else constructs” into AV1545 “Don’t use selection statements instead of a simple assignment or initialization”.
  • Removed AV1580 “Consider abstracting an external dependency or 3rd party component” because its essence is already covered by other guidelines and the introduction discussing SOLID principles.  
  • Removed AV2211 “Avoid suppressing specific compiler warnings” since I wonder how many developers even know how to suppress compiler warnings.
  • Removed AV2315 “Don’t use /* */ for comments” since we want to avoid inline comments anyhow.

You can download the PDFs of the document itself, the cheat sheet, the Visual Studio 2010/2012 Code Analysis rule sets, and the ReSharper 6/7 Code Analysis settings from the download page on the CodePlex landing page.

And don’t forget! If you have any feedback, additions or any remarks, contact me through Twitter or email.

Monday, November 05, 2012

Looking back at Build 2012

A question which was asked many times during the week was whether or not this trip to Build 2012 in Redmond was worth the time and money. image

Even after four days of raw content, there is no short answer here. As Dennis Vroegop clearly illustrated, the organization was definitely not up to the task just yet. The big problem is that I was expecting to visit a conference in the style of the Professional Developers Conference (which it clearly wasn't). Since Build is organized by the Windows division I should have known that they would primarily focus on Windows 8 and Windows Phone 8. On the other, the session list wasn't disclosed until a day ahead, which fed my hope to learn a lot of really new stuff. Many attendees I spoke to had the exact same thing.

On the other hand, we did receive 1200 USD worth of gadgets, and me being there give me a myriad of opportunities to network with other people in the community. The time in Redmond also kept me away from the daily challenges of my current project which allowed me the necessary peace to actually dig a bit deeper into the content. Granted, I won't go home with the mind-blowing ideas and insights such as those you get after visiting a QCon or the Norwegian Developers Conference. But at least Microsoft managed to motivate me to start the Boiling Point RT project, a revival of the Silverlight Cookbook for Windows 8.

Although Windows 8 is a major improvement over Windows 7 (just read the Building Windows 8 blog post series if you don’t believe me), but I’ve always had some doubts about the added value of the new Windows Store on an ordinary desktop PC. But this all changed after a week of using my newly acquired Surface RT. Windows 8 on a tablet is absolutely awesome. Almost everybody who I showed the device to was genuinely impressed with the sleek design, the operating system and some of the better apps. The same can be said about the Lumia 920. I really liked my Lumia 800 and found Windows Phone 7 a major improvement over Android. But I’ve always felt that WP7 was a bit limited on the customization level and lacked some decent apps. For instance, WhatsApp and Facebook were painfully slow. But this all changed with the combination of Windows Phone 8 and the Lumia 920. And the fact of the matter is that I would never have know this if I hadn’t been there.

But the question remains if it was really worth the time and money? For me it did, but that's mainly because the content was not the most important aspect for me. There were so many opportunities for meeting new people, extending my social network, and having in-depth discussions with other experienced developers, that alone makes it worth the trouble. However, it is questionable whether I will visit another Build if I get the choice. I’ll probably choose a more advanced conference such as QCon. On the other hand, maybe Microsoft will improve on itself next year or reintroduce the PDC. You’ll never know….

Sunday, November 04, 2012

Build Day 4: WP8, Line-of-Business, Web Essentials and lots of laughs

Here we are at the fourth and last day of Microsoft Build 2012. That so many people showed up at the first session is quite surprising considering the attendee party that went on until midnight in downtown Seattle. It was a typical attendee party; free food and drinks and live music. Since being amongst 2000 dudes is not our something we enjoy a lot, after grabbing something to eat, Jonne and I bailed out and took a cab to Queen Anne's to enjoy a few drinks at Peso’s.

Anyway, this first session was supposed to be a deep-dive on Windows Phone 8 internals. Although we never actual submerged, it was interesting to learn what they did to significantly decrease the start-up time of apps. And it shows, even painstakingly slow apps like FaceBook and WhatsApp start within a fraction of a second on our Lumia 920. I particularly like the way they employ the cloud to create optimized assemblies from the ones we submit through the App store. And did you know WP8 is still largely build on top of Silverlight? In fact, this was probably the only reference to Silverlight at all.

I have to mention one big issue with this conference though….Indian speakers. I would expect that Microsoft would at least make sure that all their speakers are experienced, trained and….understandable. I have nothing against Indian speakers, but guys, at least try to pronounce the things the way they should be. Looking at the Build twitter stream proved that I was not the only one suffering from this.

With another of those annoying bus trips done, we moved to a line-of-business talk with a huge line of people waiting to get in. Dennis Vroegop already shared his opinions about the transportation problem, so I won’t have to. Fortunately a majority of the Dutch community was part of this line, so that solved my problem a bit. The room was packed, even so that they decided to move the entire audience to another room just before the talk started. The talk was about building LOB apps, but spend the majority of its slot on synchronizing the app database (using a modified version of SQLite) using a customized version of the now deprecated Sync framework. The tooling look quite impressive and will become available soon.

I then went to a small meet up organized by Elizabeth Ayer from RedGate to talk about open-source projects. To my surprise Phil Haack, the author of NuGet a big catalyst for my Fluent Assertions project, also showed up as well as fellow Developer Developer Developer speaker Dave Evans. We had an interesting conversation on how companies can and should sponsor OSS developers. We didn't actually agree whether this should be done in the form of money or extra time. Money wouldn’t solve the problem of lack of time, so in my opinion, I would prefer some dedicated time. We also talked about the future of .NET, about Git and about Scott Guthrie's influence on getting a lot of Microsoft's products open-sourced. The hour passed by too quickly, because me and Elizabeth felt like we could have continued for hours.

The third session I attended was led by Mads Kristensen of the ASP.NET development team and covered the new features of the Fall Update of ASP.NET introduced. I've heard and seen most of it already throughout the other sessions. Nevertheless, the most noticeable part of his talk was the demonstration of Web Essentials 2012, a small add-on for Visual Studio 2012 that introduces a shipload of little HTML and JavaScript options. I'll most definitely going to install that little gem as soon as I get back to work.

Fortunately, in all their wisdom, the conference organizers did manage to schedule the perfect session for concluding this conference. Scott Hanselman and John Galloway took stage and managed to give the most entertaining (and still interesting) session of the entire conference. We got to play an on-line space game hosted using SignalR and watched how Scott completely bypassed the session's script, thereby utterly confusing his co-host Josh. All in all a brilliant session that you should most definitely watch.

By the way, the party was next to the Space Needle, so check out this night shot made with our new Lumia 920. Neat, isn't it?


Friday, November 02, 2012

Build Day 3: ASP.NET MVC, TypeScript, SignalR and Node.Js

So at this 3rd day here at Microsoft Build 2012, I'm actually getting lucky. Three great sessions in a row is obviously not something I've seen before. Hey, maybe I'll get to see four great sessions tomorrow. Anyway, with some of the technical debt from last night's Beerfest and a Halloween party in Bellevue, I managed to attend most of Scott Hanselman's session. Regardless of whether the content was new or not, each of his sessions are pure entertainment.

But the highlight of the day is most definitely the TypeScript talk by technical fellow Anders Hejlsberg. It's ironic to consider that the guy that is practically the embodiment of C# is now passionately selling JavaScript. But, all things equal, he managed to fully and utterly convince me. Those existing blog posts simply don't do justice to the power of TypeScript as was demonstrated in his (recorded) session. I think it's one of the best things since C#.

Apart from the content, a conference on this scale is an excellent opportunity to meet new people and enlarge your community network. As of today, my community even includes people from Romania. I also met the guy behind the SilverlightCream and WindowsDevNews. Since I don't blog about Silverlight anymore, he kindly reminded me of his renewed focus on anything Windows 8. Just in time to submit my plans for a Windows Store Cookbook…

I never really looked at SignalR before, so a level 300 session is quite welcome, especially if it is performed by two dudes that manage to keep the talk light and humorous. SignalR is pretty cool if you want to add some simple asynchronous communication between multiple browser windows, even if you use different browsers. It feels much more elegant than using DOM navigation to hack yourself from one window to another. And the best of this all is the fact that SignalR is a Microsoft supported open-source product with a really bright future.

In the remainder of the day I wandered from one session to the other, just to find something that interests me. One of them had a memorable intro that showed the Commodore 64 emulator running an infinite Basic loop. But that session was for the most part repeating the TypeScript introduction earlier that day. Looking at the large number of people leaving and entering the session, once again reinforced my suspicion that more people were looking for some decent content.

Well, at least we had the attendee party in downtown Seattle to look forward to.

Thursday, November 01, 2012

Build Day 2: Azure, JavaScript and even more XAML and Windows Phone

Because day 2 of the Build conference wasn't any different, it seems that I must learn to accept that a maximum of two good sessions per day is going to be the standard. Fortunately the keynote was top notch with both Scott Hanselman and Scott Guthrie taking stage. They showed some of the power the Windows Azure platform provides these days. It really feels like that every time I check the Azure website they've added another shipload of features. Especially the integration between the services is something that really shines. And did I already mention how I love the look-and-feel of the Azure management portal?

One other cool thing that happened is that Microsoft announced that the Team Foundation Service is now in production. It will be a free service for teams up to 5 developers…forever. Since this is something our company has been looking forward to for a while now, I was a bit disappointed that the pricing is still to be announced. In fact, the landing page for this service states that this will not be announced until the beginning of 2013. Bummer….

After bailing out from an extremely boring XAML session I happened to run into a JavaScript session led by Amanda Silver, one of the rare female speakers at this conference. And boy, she knew what she was talking about. I'm not yet an JavaScript expert, but I was blown away hoe much one construct can make a difference compare to another performance wise. It's incredible to learn how Microsoft managed to optimize the way the IE10 JavaScript engine works internally. If you have ever attended an in-depth session on LINQ providers by Bart de Smet or a talk by Eric Meijer on the duality of IQuerable, you know what I mean. I'm in no way qualified for sharing some of her content here, so make sure you check out the slides.

Regardless of where you are, you always run into Dutch people somewhere somehow. So during lunch we ran into some 'colleagues' from a competitor and ended up discussing Event Sourcing and synchronization challenges when building large scale systems. Similarly, I was also recognized by some people which I only know through Twitter. It definitely feels a bit awkward if you don't recognize somebody, at least, not until you get to know their Twitter IDs….

The two others sessions were both about apps again. The one hosted by Chris Anderson clearly out-leveled most other sessions even though the content wasn't particularly new. As I said yesterday, it seems as if the primary audience is developers who haven't been paying a lot of attention the last year. Nonetheless, Chris once again illustrated how easy it is to integrate the search charm into your Windows Store app. I can highly recommend viewing the recording. The other one was hosted by a British chap and dealt mostly with the (powerful) combination of Azure Mobile Services and WP8 devices. It’s ridiculously easy to connect a WP device with an Azure backend without the need to build your own server. Once gain proof Microsoft wants people to start building apps (hence the free Surface and Lumina 920). What did disturb me (and I did knew about this upfront) is the fact that you are supposed to tweak the Azure services using JavaScript. Definitely not something I'm looking forward to.


Talking about that Surface, it's a remarkable device that managed to completely replace my Acer Aspire that I've been bringing to conferences and events. In fact, I've written this blog post using OneNote MX, the Windows Store equivalent for Microsoft OneNote but optimized for touch. The only thing I had to do on my laptop is posting the blog using Windows Live Writer. It will just be a matter of time before we'll get a decent blogging app…. and a Twitter app, and a Facebook app, and…but that's an other story…