views:

303

answers:

3

The related default StyleCop rules are:

  1. Place using statements inside namespace.
  2. Sort using statements alphabetically.
  3. But ... System using come first (still trying to figure out if that means just using System; or using System[.*];).

So, my use case:

  • I find a bug and decide that I need to at least add an intelligible Assert to make debugging less painful for the next guy. So I start typing Debug.Assert( and intellisense marks it in Red. I hover mouse over Debug and between using System.Diagnostics; and System.Diagnostics.Debug I choose the former. This inserts using System.Diagnostics; after all other using statements. It would be nice if VS2010 did not assist me in writing code that won't build due to warnings as errors.

How can I make VS2010 smarter? Is there some sort of setting, or does this require a full-fledged add-in of some sort?

+2  A: 

You can make VS2010 smarter by using Resharper (www.jetbrains.com), a full-fledged add-in. It can do all of these things for you (and very much more), and is well worth the price. The Resharper add-in "StyleCop for Resharper" can even check StyleCop violations on-the-fly and underline your code the same way Visual Studio does for errors.

Matthew Ferreira
+3  A: 

For 2008, I use the Power Commands add-in. It includes a command to sort and remove unused using statements. I map that to Ctrl-O, Ctrl-R. It's not automatic, but it's very quick.

2010 has a Power Commands too, but I think the sort and order using statements stuff is now built in. You just need to set up a shortcut for it.

PS. I do not use Resharper because of the resource overhead. Every time I tell people that it thrashes my hard drive and drives memory usage through the roof, they tell me to "try the latest version - it's much better now". Suffice to say, it never has been... I do use CodeRush Xpress though.

HTH,
Kent

Kent Boogaart
Remove And Sort was introduced in VS2008
Stuart Dunkeld
+3  A: 

With regards to your #1, you can edit the project template items by using the instructions here or here. I've done this for VS 2K8 to make StyleCop and FxCop happy by default, but I haven't gotten around to doing it for 2010 as I find the procedure a bit tedious and there's always a likelihood that a VS service pack could overwrite them.

For instance, I edited the program.cs in the ConsoleApplication template to look like this:

// <copyright file="Program.cs" company="$registeredorganization$">
// Copyright (c) $year$ All Rights Reserved
// </copyright>
// <author></author>
// <email></email>
// <date>$time$</date>
// <summary></summary>

namespace $safeprojectname$
{
    using System;
    using System.Collections.Generic;
    $if$ ($targetframeworkversion$ == 3.5)using System.Linq;
    $endif$using System.Text;

    /// <summary>
    /// Contains the program's entry point.
    /// </summary>
    internal static class Program
    {
        /// <summary>
        /// The program's entry point.
        /// </summary>
        /// <param name="args">The command-line arguments.</param>
        private static void Main(string[] args)
        {
        }
    }
}

and the assemblyinfo.cs to look like this:

// <copyright file="AssemblyInfo.cs" company="$registeredorganization$">
// Copyright (c) $year$ All Rights Reserved
// </copyright>
// <author></author>
// <email></email>
// <date>$time$</date>
// <summary></summary>

using System;
using System.Reflection;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following 
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("$projectname$")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("$registeredorganization$")]
[assembly: AssemblyProduct("$projectname$")]
[assembly: AssemblyCopyright("Copyright © $registeredorganization$ $year$")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible 
// to COM components.  If you need to access a type in this assembly from 
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

[assembly: CLSCompliant(true)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("$guid1$")]

// Version information for an assembly consists of the following four values:
//
//      Major Version
//      Minor Version 
//      Build Number
//      Revision
//
// You can specify all the values or you can default the Build and Revision Numbers 
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

I've submitted an incident at Microsoft Connect to the effect that their tools' auto-generated code should satisfy StyleCop/FxCop and their coding guidelines documents.

Jesse C. Slicer
+1 for pointing to beautify the templates.
Filburt