CodeMaid vs Stylecop 使用组织

CodeMaid vs Stylecop usings organisation

我使用 StyleCop 已经有一段时间了(我已经习惯了)。我的朋友推荐我也试试 CodeMaid。我注意到的第一件事是使用组织的不同。

Stylecop 按字母顺序排列使用顺序,系统使用排在最前面,非系统使用在下面列出。

CodeMaid 也按字母顺序对使用进行排序,但它不会首先对系统使用进行排序。它只按字母顺序排列 usings

我遇到的另一件事是,CodeMaid 允许您在命名空间之外使用(据我所知,最好将所有使用放在命名空间内)

我想问一下 usings 的正确顺序是什么,最后是为什么?

没有 "proper" 排序,只有约定。 Stylecop 的 SA1210 rule 明确指出 System 命名空间放在第一位:

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 声明 System 命名空间必须在其他命名空间之前,推理是:

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 的规则是惯例,Codemaid 选择了一个稍微不同的惯例。选择您喜欢的那个,然后继续做出更重要的决定。

参见 this question 关于在命名空间外有 using 语句。

免责声明:我写了 CodeMaid。

  1. 对于 System using 语句是第一个,Microsoft 在 Visual Studio 2012 年开始引入 "Windows." 程序集时更改了他们的默认首选项。此处有更多详细信息:Using Directives Sorted in Wrong Order

您可以在工具->选项->文本编辑器->C#->高级->"Place 'System' directives first when sorting usings" 轻松将其更改回 VS2010 默认值。 CodeMaid 尊重 Visual Studio 定义的首选项,默认情况下不将系统指令放在首位。

  1. 对于命名空间内的 using 语句,我们的积压工作中需要支持 https://trello.com/c/CLRxsIyc。 StyleCop 可能将其声明为标准,但如果您查看 MSDN 示例、Visual Studio 模板等,您会发现在命名空间外使用语句更为传统。关于这个问题有很多争论,我们的目标是支持这两种方法。

希望对您有所帮助。 :)