5

I am a big fan of StyleCop, it makes my life easier. A bunch of other people have thought of good rules, and I gladly follow them by enabling StyleCop. Recently I have been messing with Coded Ui extensibility, and came across this article:

http://blogs.msdn.com/b/gautamg/archive/2010/01/05/2-hello-world-extension-for-coded-ui-test.aspx

The sample code below makes StyleCop unhappy because the using statements are outside of the namespace. However, I can move only the two System* packages in - the third is needed to define an assembly attribute, and I cannot throw assembly: inside of a namespace.

Is there a clean way to re-organize this code?

using System;
using System.Diagnostics;
using Microsoft.VisualStudio.TestTools.UITest.Common;
using Microsoft.VisualStudio.TestTools.UITest.Extension;

// Attribute to denote that this assembly has UITest extensions.
[assembly: UITestExtensionPackageAttribute("HelloWorldPackage",
           typeof(UITestHelloWorldPackage.HelloWorldPackage))]

namespace UITestHelloWorldPackage
{
    internal class HelloWorldPackage : UITestExtensionPackage
    {
        public override object GetService(Type serviceType)
        {
            Trace.WriteLine("Hello, World");
            return null;
        }
....
Hamish Grubijan
  • 10,562
  • 23
  • 99
  • 147

2 Answers2

5

IMHO, the rule to put usings inside the namespace is useless and makes the code hard to read.

Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
  • 1
    Definitely. I always remove this rule from the StyleCop settings. – Graham Clark Feb 22 '11 at 15:33
  • Hm ... I do see your point, but the must have had some sort of a reason for that rule. – Hamish Grubijan Feb 22 '11 at 15:33
  • 1
    If you can't think of that reason, the rule doesn't apply to you. And really: Not all rules make sense and you need to check for yourself which make sense and which not. – Daniel Hilgarth Feb 22 '11 at 15:36
  • It doesn't quite answer the question - he was asking how to get round it, there is a valid but relatively rare reason for that rule I remember reading somewhere about it. – Xander Mar 03 '11 at 13:32
  • @Daniel: http://stackoverflow.com/questions/125319/should-usings-be-inside-or-outside-the-namespace – Wim Coenen Mar 03 '11 at 21:14
5

Can't you move your [assembly: UITestExtensionPackageAttribute()] attribute to your Properties\AssemblyInfo.cs file? I'm guessing the article you referenced had the [assembly:] attribute there just to keep the example in a single block of code.

I agree with Daniel - having using statements inside namespaces makes your code harder to read.

I'd recommend moving the attribute to your AssemblyInfo.cs file and keep your usings at the top of your class file. That seems pretty standard.

Hope this helps!

David Hoerster
  • 28,421
  • 8
  • 67
  • 102