27

I am using StyleCop for a quite a while (and I am used to it). Friend of mine recommended me to also try CodeMaid. First thing I've noticed is difference in usings organisation.

Stylecop orders usings in alphabetical order where System usings are listed first and non-system usings are listed below.

CodeMaid orders usings also in alphabetical order but it does not order System usings first. It only orders usings alphabetically

Another thing I came across is that CodeMaid allows you to have usings outside of namespace (and as far as I know it is better to place all the usings within the namespace)

I wanted to ask what is the proper ordering of usings and eventually why?

PhillyNJ
  • 3,859
  • 4
  • 38
  • 64
Kajiyama
  • 3,393
  • 8
  • 26
  • 38
  • 1
    To be frank, I would trust StyleCop - MS Brand/tool. It's been developed internally and then made public a few good years ago so the recommendations are naturally built into StyleCop. I guess it depends on your needs more than anything but personally, StyleCop. – Ahmed ilyas Mar 12 '15 at 15:18
  • 1
    possible duplicate of [c# using namespace statement ordering](http://stackoverflow.com/questions/6543107/c-sharp-using-namespace-statement-ordering) – Jeff Siver Mar 12 '15 at 15:20
  • The order of using directives does not matter; the position of using directives does not matter. In fact, if you research a bit in MSDN, they are called using directives for a reason; they provide information to the compiler but do not show up in the IL. – Jeff Siver Mar 12 '15 at 15:24

2 Answers2

64

Disclaimer: I wrote CodeMaid.

  1. For System using statements being first, Microsoft changed their default preferences in Visual Studio 2012 as they started introducing "Windows." assemblies. More details here: Using Directives Sorted in Wrong Order

You can easily change this back to the VS2010 default at Tools->Options->Text Editor->C#->Advanced->"Place 'System' directives first when sorting usings". CodeMaid respects the Visual Studio defined preference which defaults to not putting System directives first.

  1. For using statements being inside the namespace, it is on our backlog to support that https://trello.com/c/CLRxsIyc . StyleCop may state it as the standard, but if you look at MSDN examples, Visual Studio templates, etc. you'll find using statements outside the namespace is far more conventional. There is a lot of debate on that issue and our goal is to support both approaches.

Hope it helps. :)

Community
  • 1
  • 1
Steve Cadwallader
  • 2,656
  • 4
  • 28
  • 37
7

There is no "proper" ordering, just conventions. Stylecop's SA1210 rule explicitly states that System namespaces are placed first:

A violation of this rule occurs when the using directives are not sorted alphabetically by namespace. Sorting the using directives alphabetically makes the code cleaner and easier to read, and can help make it easier to identify the namespaces that are being used by the code. The System namespaces are an exception to this rule and will always precede all other namespaces. See SA1208 for more details.

Rule SA1208 states that the System namespaces must be before other namespaces, and the reasoning is:

Placing all System using directives at the top of the using directives can make the code cleaner and easier to read, and can help make it easier to identify the namespaces that are being used by the code.

Stylecop's rules are conventions, and Codemaid chose a slightly different convention. Pick the one you like and move on to more important decisions.

See this question regarding having using statements outside of the namespace.

Community
  • 1
  • 1
Patrick Quirk
  • 23,334
  • 2
  • 57
  • 88
  • Thanks for link, I know about it and i read it few year ago therefor i mentioned "as fas as i know" part – Kajiyama Mar 12 '15 at 15:20