Reduce repetitive code writing

Start using the built in code snippets included with VS2005/VS2008 (Lucky users with R# disregard!) I have found them invaluable for saving time writing boiler plate test fixtures and test cases, instead of having to write out:

 [TextFixture] public class When_() 
{ 
} 

I can simply type testfixture plus tab, below is the xml for the example above:

 
 
       
    <Header>
        testfixture
        testfixture
        Code snippet for a textfixture class
        Mike Cromwell
        
            Expansion
        
     </Header> 
    
        
             
                 name
                 Class name
                 foo
             
        
 
        <Code>
             
        </Code> 
       
     
 

Most of it is self explanatory, the SnippetType can be one of the following:

  • Expansion – The snippet can be added were the cursor is
  • WithSurrounds – The snippet will surround any selected code
  • Refactoring – The snippet can only be applied during a C# refactoring

To enable them in the intellisense for VS you can locate the My Snippets folder or by adding a new snippet location via the Snippets Manager under Tools. Once you have the location you simply need to create a new text file and give a .snippet extension, you can then add in the XML using the schema shown above, when you go back to VS you should see the snippet listed using the Shortcut value.

The msdn site has more details on the snippet schema reference

Advertisements