Top 7 Coding Standards & Guideline Documents For C#/.NET Developers

imageSome time back, I collated a list of 7 Must Read, Free EBooks for .NET Developers, and a lot of people found it useful. So, I thought about putting together a list of Coding Standard guidelines/checklists for .NET /C# developers as well.

As you may already know, it is easy to come up with a document - the key is in implementing these standards in your organization, through methods like internal trainings, Peer Reviews, Check in policies, Automated code review tools etc. You can have a look at FxCop and/or StyleCop for automating the review process to some extent, and can customize the rules based on your requirements.

Anyway, here is a list of some good Coding Standard Documents. They are useful not just from a review perspective - going through these documents can definitely help you and me to iron out few hidden glitches we might have in the programming portion of our brain.

So, here we go, the listing is not in any specific order.

1 – IDesign C# Coding Standards

IDesign C# coding standards is a pretty decent and compact (27 pages) Coding Standards Document. It covers a Naming conventions, Best practices and Framework specific guidelines. Example:

image

The document even has guidelines for project settings, build configuration, versioning etc. Good work by IDesign guys. You can download the document here

2 – Encodo C# Handbook

Encodo C# handbook is bit more recent, and has 72 pages of guidelines on Structure, Formatting, Naming. It also has a ‘Patterns and Best Practices’ section, which is a must read for any .NET/C# developer.

image

You can download the Handbook here.

3 – Microsoft Framework Design Guidelines

MSDN has a section on guidelines for Designing class libraries, which covers a set of best practices related to Type Design, Member Design etc. You can find it here.

4 – Denni’s C# Coding Standards document

Dennis created an initial version of C# coding standards, which was published as Philips Health Care C# coding standards document (~70 pages). The document categorizes the guidelines to categories like Naming, Exception Handling, Control Flow etc.

  • Update: Dennis kindly pointed that the Initial Version I linked here earlier has now been superseded by the Coding Guidelines for C# 3.0 and C# 4.0. Paul Jansen of Tiobe will update his site soon regarding the new version - But in meantime, download the guidelines and some companion documents here: http://csharpguidelines.codeplex.com/

5 – Microsoft’s All-In-One Code Framework Coding Guideline

Microsoft’s All In One Code framework has a Coding Style Guideline document. The Microsoft All-In-One Code Framework is a free, centralized code sample library provided by the Microsoft Community team. It has typical code samples for all Microsoft development technologies, and a code style guideline document with that. Thanks to Kevin for pointing out this guideline document with All In One Code Framework (See the comments)

6 – Brad’s Quick Post on Microsoft Internal Coding Guidelines

Brad had a post on Microsoft Internal coding standards (I’m not sure whether he still follow that in Google, if at all he uses C# there). It is a short post, and is mainly on Styling and Naming conventions.

7 – Mike’s C# Coding Style Guide

Mike Kruger (Sharpdevelop) had published a 13 page C# Coding Style guide. Again, the focus is on Casing, Naming conventions, Declaration style etc. A short and simple Style Guide.

So, if you are still confused about which document to choose - my recommendation is here for you - Based on your landscape, organizational climate, project and domain, go through these documents and pick the relevant recommendations – to formulate your very own 10 page ‘.NET/C# Coding standards/guidelines’ for your team.

Also, if you think I missed any prominent guideline document, list down the same in the comments section, and I’ll include that in the main post if it is relevant – My initial post was about 6 documents, but I expanded/modified the list later based on some feedback I received. Happy Coding.

Submit this story to DotNetKicks

Also, don’t miss these related posts.

Read more >>

I always viewed Silverlight as a platform with great potential, and long time back I blogged about Why Silverlight is going to be *The* Development platform.

How ever, imageLast week, post PDC2010, a lot of debate happened on Silver-light’s future. This was triggered by Bob Miguel's “Strategy Shift” comment  which followed by reports from Mashable along with a number of other Tech medias along the lines of “Microsoft Shifts from Silverlight to HTML”.

This caused a big dilemma and confusion in the community about Silverlight’s future (My perspectives here) – Though Bob came up few good clarifications on the entire drama (Read this post from Bob Muglia ), according to me, a couple of things were still hanging in the air. 

I concluded my last post saying

Don’t let Silverlight go down under the carpet, don’t limit it just ‘for phone’ or only for other ‘sweet spots’. Commit towards investing more in Silverlight, to implement more features and tooling around them - so that it’ll stay on top of HTML5, as a value added platform. Say that publicly. Continue the efforts for making it cross browser and cross platform. Continue supporting Moonlight initiatives for Linux. Promote Silverlight based OS platforms (Windows Phone 7 OS) more – so that it can get more adoption for thin devices and IP TVs. Provide better support for Silverlight in Mac for OOB scenario

Silverlight and Windows Embedded

I was so keen to know whether the ‘Strategy shift’ has affected Microsoft’s commitment to bring the full power of Silverlight beyond the PC and the Phone, to other devices too (especially, a variety of handheld devices that runs on Windows Embedded). Microsoft already has a Silverlight runtime available for Windows Embedded devices. How ever, the current implementation don’t support the .NET stack, you were expected to use the Native controls and C++, which means you can’t multi target your apps.

Now, Scott Gu has posted his reply to the Silverlight questions in his blog, and I got pleasantly surprised to see a clarification from him for some of the readers in the Comments section.

image

So, that is news. Microsoft will be enabling a full .NET programming model for Silverlight in Windows Embedded, and it seems that a Silverlight version that can support managed programming model is under development for Windows Embedded (Probably for Windows Embedded Compact 7 ?). This will certainly accelerate the adoption of Silverlight as a platform for a wide variety of applications.

Read more >>

imageBased on a recent announcement from Microsoft, Visual Studio Installer will be retiring after VS2010. So it seems that in future versions of Visual Studio, Visual Studio installer won’t be available.

Background Information

From ClickOnce and Setup & Deployment projects thread

In Visual Studio 2010, we have partnered with Flexera, makers of InstallShield, to create InstallShield Limited Edition 2010 just for Visual Studio 2010 customers. The InstallShield Limited Edition 2010 offers comparable functionality to the Visual Studio Installer projects. In addition, you can build your deployment projects using Team Foundation Server and MSBuild. For more information, see this post. With InstallShield available, the Visual Studio Installer project types will not be available in future versions of Visual Studio.

So, you are expected to switch to Install Shield for your setup and deployment, if you are presently relying on Visual Studio Installer. In this post, let us explore Nullsoft Scriptable Installer System or NSIS as an alternate, open source option for packaging and deploying your applications.

Nullsoft Scriptable Installer System or NSIS – A Quick Look

If you’ve ever installed WinAmp, NSIS is the installer used by WinAmp. I used Null Soft Installer for the first time way back in 2004, when I was doing some shareware business. I had a product called Smart Editor Professional (Still available as a freeware in sites Tucows) which was developed in VC++ and VB – and I used Nullsoft NSIS for scripting the installation procedure, which included copying files, showing a splash screen, registering ActiveX dlls, providing optional install components, Creating Program menu entries, Associating file types etc. One of the most interesting aspect of NSIS is the small installer footprint, and the easy to understand scripting aspect.

Now, to start with, you need to download and install NSIS from here. Once  you finish the installation, select NSIS from your StartMenu->Programs, to bring up the NSIS menu.

image 

A lot of resources are already listed there, but I’ll provide a quick start. You’ll find two options under the compiler – to (1) Compile NSI scripts, or to (2) Create an installer based on a Zip file. If your requirements are minimum or if you need to create an installer that’ll simply extract files from a Zip file to some where, you can try the second option. Otherwise, you can write a NSIS script (a simple text based script file with extension *.nsi) to configure the installer as you need.

Once you have an NSIS file, you can create the installer by ‘compiling’ it. Click ‘Compile NSI scripts’, to bring up NSI Script compiler.

image

Now, As you can imagine, you need to create an *.NSI script that directs your installer how to package your components, that can be compiled with MakeNSISW. There are tones of examples available in Program Files\NSIS\Examples folder and the User Manual is pretty comprehensive - so I’m not going to actual scripting part here. Basically, an NSIS script file has the following components.

  • Attributes – Defines the basic attributes like Name, UI etc of your installer
  • Pages – Predefined Wizard pages like directory (to allow users to select target folder for installation), components (for allowing users to select components/sections to install), etc
  • Sections – Lets you logically group your installation components, so that users can choose optional items to install. The script inside a Section is executed only if the user select that component.
  • Functions – Functions contains code like Sections, and can be called using the ‘Call’ keyword from with in sections. Also, there are callback functions with special names that the installer will invoke by itself (eg: .onInit);

Creating a simple script and an Installer

With that information, let us create a simple NSI file. Open notepad, and copy and paste the below script. If you want a decent editor for NSIS scripts, Notepad++ already supports NSIS syntax. The below script demonstrates most of the points I described earlier. Basically, it’ll create an installer by packaging all files from your c:\inst folder, and will deploy those files to a target folder specified by your user. Note that in NSIS a ; is for specifying a single line comment.

;--------------------------------
; Installer Attributes
;--------------------------------

; Name of installer
Name "NsisDemo"

; The file to write
OutFile "NsisDemo.exe"

; The default installation directory
InstallDir $PROGRAMFILES\NsisDemo

; Request application privileges for Windows Vista
RequestExecutionLevel user

;--------------------------------
; Pages
;--------------------------------

Page components
Page directory
Page instfiles

;--------------------------------
;Sections
;--------------------------------

; The stuff to install
Section "Stuff" 
 
 ; Set output path to the installation directory.
  SetOutPath $INSTDIR
  
  ;Include files from this location, and copy that to the current
  ;out path
  
  File c:\inst\*.*
  
SectionEnd ; end the section

;--------------------------------
; Functions
;--------------------------------
Function .onInit
  MessageBox MB_YESNO "This will install Xyz. Do you want to continue?" IDYES startnow
    Abort
  startnow:
FunctionEnd

Note that we’ve three ‘Pages’ - components, directory and instfiles specified. Also, we’ve a section “Stuff”, where we use File directive for informing the NSIS compiler which all Files to package (all files in your c:\inst\*.*) - so that those files will be carried by our installer. Also, there is a callback function .onInit, which will be called automatically by installer, to get an assertion from the user – Whether to continue or not - when the installer is initialized .

Now,

  • Save the above script to a *.NSI file.
  • Place few files in the c:\inst folder (Create that folder if it is not there), so that the MakeNSIS can pick files from there to package.
  • Then compile the NSI file either by right clicking and choosing ‘Compile with NSIS’ or by opening it in the MakeNSISW Script compiler (see above).

You’ll see the generated installer exe, NsisDemo.exe, as we specified in the Installer Attributes. Now, if you run the Exe, you’ll see the following screens.

1 - This is triggered by our script in .onInit

image

2 – Once you click Yes, this is the ‘component’ Page. See the section ‘Stuff’ inside that.

image 

3 – Click Next to see the ‘directory’ Page. See that what ever we specified using InstallDir installer attribute has appeared as the default destination folder.

image

Please note that NSIS is customizable and open source. A lot of other UIs and Plugins are also available – so don’t get deceived by the above ‘minimal’ look of the installer.

Also, you should, see this post where I explain How to use NSIS to create an offline installer for a Silverlight application. Happy Coding!!

Read more >>

top