Lance's Whiteboard

Random scribbling about C#, ASP.NET, Sql Reporting, etc.

News

BlogMailr Enabled




Sponsored Ad
Sponsored Ad

Blogs I Read

Developer Sites

Googling, etc...

MSDN Resources

Tutorials & Reference

C# Coding Standards document

CSharp_Coding_Standards.pdf

Version 1.5  

Make payments with PayPal - it's fast, free and secure!

 Why create yet another coding standard document?

There have been numerous attempts to document C# Coding Standards since the language was released, but most are either overly verbose, too restrictive, or try to cover every single scenario.  This is my attempt to start from scratch and write a new standards document that is concise, simple to read & use, and creates a pragmatic balance of rule enforcement.  Having said that, I'm sure that many of you will have just as many disagreements with this document as I have with others, but I am always up for a good debate. :)

Goals and objectives:

  • Cover all major C# Language features.
  • Provide guidelines on coding style & language usage (but not syntax)
  • Demonstrate all rules in code where applicable.
  • Describe rules in structure that is easy to read & use.
  • Define clear & concise rules.
  • Use consistent terminology & rule patterns.
  • Only provide rules where there is a clear cut best practice.
  • Lead developers to a "pit of success" and avoid common "pits of failure"

Release Notes:

v1.15 -  03/01/2007
  • Added numeric datatype usage guidelines.
  • Adjusted guidelines for public, protected, and internal access modifiers.
  • Tweaked verbage throughout document.
v1.13/a -  08/17/2004
  • Modified layout & added License Agreement. Misc. grammar adjustments.
  • Removed rules on foreach vs for pending additional research.
  • Added rules on switch/case statements.
  • Revision(a) recommends custom Exceptions to subclass Exception not ApplicationException
v1.12 -  06/30/2004
  • Modified code commenting and formatting rules.
  • Modified various code examples.
  • Modified Exception Handling and Flow Control sections.
v1.11 -  06/08/2004
  • Modified formatting.
  • Restructured “Language Usage” section.
  • Corrected language, and added code examples.
  • Consolidated conflicting rules.
v1.10 -  05/25/2004
  • Modified naming conventions for Internal and Protected identifiers.
  • Added, modified, & removed misc rules.
  • Corrected grammar, code, and some verbiage.
v1.09 -  05/23/2004
  • Changed style & formatting.
  • Added Quick Summary tables.
  • Added/modified various standards.
v1.08 -  05/20/2004
  • Split .NET guidelines into separate document.
  • Changed overall scope and goals. Restructured sections. Improved Introduction.
  • Added Scope and Terminology sections.
v1.07 -  05/11/2004
  • Added Bibliography, Resources, and .NET Framework Guidelines.
v1.06 -  05/10/2004
  • Misc. adjustments to various rules.
v1.05 -  05/10/2004
  • Updated Naming Conventions.
v1.04 -  05/09/2004
  • Misc. grammar and syntax changes.
v1.03 -  05/09/2004
  • Changed formatting and organization.
v1.02 -  05/09/2004
  • Added code examples.
v1.01 -  05/09/2004
  • Added more Language Usage.
v1.00 -  03/01/2004
  • Created

Comments

Bastian Waidelich said:

Hello! I just realized, your C# Coding Standards document is more than two years old. Nevertheless it's still very usefull and inspiring. Thanks for that. One thing I didn't really comprehend is: "26. Avoid creating recursive methods. Use loops or nested loops instead." Why is that? I thought, recursive methods are very handy sometimes..
# October 10, 2006 4:03 AM

CodeSniper said:

Recursive method calls are the most common causes for stack overflow errors.  Also, if your parameter list contains any object references (any mutable type), then the GC cannot dispose of anything until the entire stack unwinds via completing all recursions or due to an exception.  All that stack walking and unraveling causes excessive overhead on the GC and the CLR.

Instead of forcing the CLR to maintain that stack, you can simply do the stack management yourself within a loop.

I'm not sure if I am explaining this very well, so here is a quick and dirty example:

public string[] GetAllFiles(string path, string fileMask, int max)

{

   Stack<string> folderList = new Stack<string>();

   List<string> fileList = new List<string>();

   string[] currentFolders = null;

   string[] currentFiles = null;

   string thisFolder = null;

   folderList.Push(path);

   while(folderList.Count > 0)

   {

       thisFolder = folderList.Pop();

       currentFiles = Directory.GetFiles(thisFolder, fileMask);

       foreach(string file in currentFiles)

       {

           if(fileList.Count < max)

           {

               fileList.Add(file);

           }

           else

           {

               folderList.Clear();

               break;

           }

       }

       currentFolders = Directory.GetDirectories(thisFolder);

       if(currentFolders != null && currentFolders.Length > 0)

       {

           foreach(string folder in currentFolders)

               folderList.Push(folder);

       }

   }

   return fileList.ToArray();

}

# October 18, 2006 10:46 AM

Bastian Waidelich said:

That makes it much clearer now! Thanks for the example and sorry for the late reply.

btw: the method Directory.GetFiles has a third optional parameter allowing to perform a recursive lookup. Internally that method works with nested loops rather than with recursion though!

# November 29, 2006 7:28 AM

Lance's Whiteboard said:

You can now download version 1.13 of my C# Coding Standards for .NET . I havent posted on this topic

# June 20, 2007 1:43 PM

Chillisoft Blog » Coding standards said:

Pingback from  Chillisoft Blog &raquo; Coding standards

# June 27, 2007 2:14 PM

Common development principles for xLim at Rinat Abdullin said:

Pingback from  Common development principles for xLim at Rinat Abdullin

# January 7, 2008 11:29 PM

Brij Mohan said:

Following the right Coding standars comes with Practice and proper Guidance if you are a starter, and

# October 10, 2008 5:17 AM

rascunho » Blog Archive » links for 2008-10-10 said:

Pingback from  rascunho  &raquo; Blog Archive   &raquo; links for 2008-10-10

# October 10, 2008 4:15 PM

bmdayal said:

Following the right Coding standars comes with Practice and proper Guidance if you are a starter, and

# November 22, 2008 2:52 AM

C#, .NET, and ASP.NET Coding Standards and Best Practices « CMS Squad said:

Pingback from  C#, .NET, and ASP.NET Coding Standards and Best Practices &laquo;  CMS Squad

# December 26, 2008 1:19 PM

Estandares de Codificacion para C# « ASPI SYSTEMS said:

Pingback from  Estandares de Codificacion para C# &laquo;  ASPI SYSTEMS

# May 17, 2009 1:15 PM

its coding time said:

.NET Coding Standards

# July 22, 2009 3:17 PM

C# coding standards « Glog said:

Pingback from  C# coding standards &laquo;  Glog

# October 11, 2009 12:25 AM