in

Platinum Bay

Peace, Love, and...

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...

 

September 3, 2010 9:50 AM

Hi Steve,

I see it's an old post but I just found it and I have a question.

I really pay attention to the structure of code (some say too much) and I couldn't agree more about practicing Ctrl+K+D.

I put extra effort in following MS guidance / conventions about code formating, naming, etc., but there is one thing I could not find (maybe I didn't search enough)  and it is the "Code Order" you mentioned in the post.

I had my own convention and stuck with it in hope that I will find the official one someday.

So, Is this order of code, proposed in the post, actually the official one or your own convention?

Thanks!

Anil

 

Leave a Comment

(required )  
(optional )
(required )  
Add

About Steve

Steve Andrews is an independent consultant, INETA speaker, and Microsoft MVP for Visual Studio ALM. He has been working in technology for over ten years focusing on custom application development and Application Lifecycle Management. Steve is also Microsoft and IBM certified and a community fanatic having led sessions at nearly 100 events across North America. When he's not developing software solutions or engaging with the community about software technology, Steve is a closet singer and songwriter and plays the guitar and keys. Occasionally, Steve even gets to sleep. Occasionally.
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.