Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeMaid vs Stylecop usings organisation

Tags:

c#

stylecop

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?

like image 769
Kajiyama Avatar asked Sep 06 '25 17:09

Kajiyama


2 Answers

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

like image 180
Steve Cadwallader Avatar answered Sep 09 '25 08:09

Steve Cadwallader


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.

like image 34
Patrick Quirk Avatar answered Sep 09 '25 08:09

Patrick Quirk