in

Platinum Bay

Peace, Love, Team System, and Community

This Blog

Syndication


.NETicated

Tips for Releasing Sample Code

When I’m learning a new technology, I’ll frequently download code samples that help me better understand the concepts. Often though I find myself frustrated by having to deal with formatting and extraneous information before even looking at the code. To that end I have some rules for releasing sample code.

Format Every Document
The keyboard shortcut for this is Ctrl+K+D. Nothing is more frustrating than looking at unformatted code.

Code Order
Code should be ordered in the following structure from top to bottom:

  • events
  • fields and automatic properties
  • constructors
  • abstract methods
  • methods
  • abstract properties
  • properties
  • subclasses

Code Statements
Include a blank line before return statements and before and after decision blocks (including using, if, switch, while). In C#, opening and closing brackets should be on their own line.

Remove Excess Blank Lines
There should never be more than one consecutive blank line between any lines of code. With consecutive closing brackets as well as #endregion notations in C#, there should be no blank lines between them. There should also be no blank lines between fields or at the beginning or end of method or property bodies.

Separate Code
There should be one blank link between properties and methods to allow the user to see logical breaks in the code.

Code Files
There should only be one class, interface or enumeration per file.

XML Comments
Always remove unnecessary XML comments from your code files. For example, this XML comment should be removed:

/// <param name="param"></param>

Import Namespace Placement
Import (C#) or using (VB) directives should be placed above the namespace. Also use the Organize Usings > Remove and Sort refactoring built-in to Visual Studio 2008 and Visual Studio 2010 to remove any unnecessary directives. This also helps to make the directives more readable.

Wrapping Lines
Don’t manually wrap long lines of code. Visual Studio has a feature to allow a user to wrap long lines if they so choose.

Properties
Use automatic properties in .NET 3.0 code and above whenever possible.

These are a few of the rules. I’ll try to keep the list updated as I run across more frustrations.

Comments

December 23, 2009 3:45 PM

My preference would be for projects to make use of style guidelines/tools that already exist.  So C# projects should be using StyleCop.  The key is to not change the default settings that are installed on the machine, but instead customize them per solution/project.

 

December 24, 2009 4:51 AM

Pingback from  Dew Drop &#8211; December 24, 2009 | Alvin Ashcraft&#039;s Morning Dew

 

December 30, 2009 8:36 PM

Web Performance of JavaScript Looping Techniques Unpack packed Javascript code - eval(function(p,a,c

 

December 30, 2009 8:40 PM

WebPerformanceofJavaScriptLoopingTechniquesUnpackpackedJavascriptcode-eval(functi...

 

Leave a Comment

(required )  
(optional )
(required )  
Add

About Steve

Steve Andrews is a Team System MVP and INETA Speaker, and has been working as a developer for more than 9 years. During this time, he has designed and developed applications in such widely varying areas as trust accounting, medical information management, supply chain management, and retail systems. Steve is also an MCP, ICSOO, Speaker Liaison for the Philly .NET User Group, and community fanatic.
Powered by Community Server (Commercial Edition), by Telligent Systems
© Platinum Bay | Some Rights Reserved Creative Commons License

Disclaimer: The information in this weblog is provided "AS IS" with no warranties, and confers no rights. This weblog does not represent the thoughts, intentions, plans or strategies of my employer. It is solely my opinion. Feel free to challenge me, disagree with me, or tell me I'm completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or annonymous comments) - so keep it polite, please.