简体   繁体   English

为什么是“使用系统”; 不被认为是不好的做法?

[英]Why is "using System;" not considered bad practice?

I have a C++ background and I do fully understand and agree with the answers to this question: Why is “using namespace std;”我有 C++ 背景,我完全理解并同意这个问题的答案: Why is “using namespace std;” considered bad practice?被认为是不好的做法?

So I'm astonished that, having some experience with C# now, I see the exact opposite there: using Some.Namespace;所以我很惊讶,现在有了 C# 的一些经验,我看到了完全相反的情况: using Some.Namespace; is literally used everywhere.字面上到处都在使用。 Whenever you start using a type, you add a using directive for its namespace first (if it isn't there already).每当您开始使用类型时,首先为其命名空间添加 using 指令(如果它还没有的话)。 I cannot recall having seen a .cs -file that didn't start with using System; using System.Collections.Generic; using XYZ; etc...我不记得曾经看过一个.cs文件,它不是从using System; using System.Collections.Generic; using XYZ; etc...开始的using System; using System.Collections.Generic; using XYZ; etc... using System; using System.Collections.Generic; using XYZ; etc... using System; using System.Collections.Generic; using XYZ; etc... . using System; using System.Collections.Generic; using XYZ; etc... In fact, if you add a new file via the Visual Studio wizard, it automatically adds some using directives there, even though you may not need them at all.事实上,如果您通过 Visual Studio 向导添加一个新文件,它会自动在那里添加一些 using 指令,即使您可能根本不需要它们。 So, while in the C++ community you get basically lynched, C# even encourages doing this.因此,虽然在 C++ 社区中您基本上会受到私刑,但 C# 甚至鼓励这样做。 At least this is how it appears to me.至少在我看来是这样。

Now, I do understand that using directives in C# and C++ are not exactly the same thing.现在,我明白在 C# 和 C++ 中使用指令并不完全相同。 Also, I do understand that one of the nastiest things you can do with using namespace in C++, namely putting it in a header file, has no equivalently nasty counterpart in C# due to the lack of a concept of header files and #include .另外,我确实理解在 C++ 中using namespace可以做的最讨厌的事情之一,即将它放在头文件中,由于缺乏头文件和#include的概念,因此在 C# 中没有等效的讨厌的对应物。

However, despite their differences, using directives in C# and C++ serve the same purpose, which is only having to type SomeType all the time, rather than the much longer Some.Namespace.SomeType (in C++ with :: instead of . ).然而,尽管存在差异,但在 C# 和 C++ 中使用指令的目的是相同的,它只需要一直输入SomeType ,而不是更长的Some.Namespace.SomeType (在 C++ 中使用::而不是. )。 And with this same purpose, also the danger appears to be the same to me: naming collisions.出于同样的目的,对我来说,危险似乎也是一样的:命名冲突。

In the best case this results in a compilation error, so you "only" have to fix it.在最好的情况下,这会导致编译错误,因此您“只需”修复它。 In the worst case, it still compiles and the code silently does different things than you intended it to do.在最坏的情况下,它仍然可以编译,并且代码会默默地做与您预期不同的事情。 So my question is: Why (apparently) are using directives considered so unequally bad in C# and C++?所以我的问题是:为什么(显然)使用在 C# 和 C++ 中被认为如此糟糕的指令?

Some ideas of an answer that I have (none of these really satisfy me, though):我对答案的一些想法(不过,这些都没有真正让我满意):

  • Namespaces tend to be much longer and much more nested in C# than in C++ ( std vs. System.Collection.Generic ).与 C++ 相比,C# 中的命名空间往往更长且嵌套更多( stdSystem.Collection.Generic )。 So, there is more desire and more gain in de-noising the code this way.因此,以这种方式对代码去噪有更多的愿望和更多的收获。 But even if this is true, this argument only applies when we look at the standard namespaces.但即使这是真的,这个论点也只适用于我们查看标准命名空间时。 Custom ones can have any short name you like, in both C# and C++.在 C# 和 C++ 中,自定义名称可以有您喜欢的任何短名称。

  • Namespaces appear to be much more "fine granular" in C# than in C++.命名空间在 C# 中似乎比在 C++ 中更“细粒度”。 As an example, in C++ the entire standard library is contained in std (plus some tiny nested namespaces like chrono ) while in C# you have System.IO , System.Threading , System.Text etc. So, the risk of having naming collisions is smaller.例如,在 C++ 中,整个标准库都包含在std (加上一些小的嵌套命名空间,如chrono ),而在 C# 中,您有System.IOSystem.ThreadingSystem.Text等。因此,发生命名冲突的风险是较小。 However, this is only a gut feeling.然而,这只是一种直觉。 I didn't actually count how many names you "import" with using namespace std and using System .我实际上并没有计算using namespace stdusing System “导入”了多少个名称。 And again, even if this is true, this argument applies only when looking at the standard namespaces.再说一次,即使这是真的,这个论点也只适用于查看标准命名空间。 Your own ones can be designed as fine granular as you wish, in both C# and C++.您可以按照自己的意愿在 C# 和 C++ 中将自己的内容设计为细粒度的。

Are there more arguments?还有更多的争论吗? I'm especially interested in actual hard facts (if there are any) and not so much in opinions.我对实际的硬事实(如果有的话)特别感兴趣,而不是对意见感兴趣。

Why is “using System;”为什么是“使用系统”; not considered bad practice?不被认为是不好的做法?

"using System;" “使用系统;” is not universally not considered a bad practice.并非普遍认为是不好的做法。 See for example: Why would you not use the 'using' directive in C#?参见示例: 为什么不在 C# 中使用“using”指令?

But it may be true that it is not considered quite as bad as using namespace std .但它确实没有被认为像using namespace std那样糟糕 Probably because:大概是因为:

  1. C# does not have header files. C# 没有头文件。 It is uncommon to "include" one C# source file into another using a pre-processor.使用预处理器将一个 C# 源文件“包含”到另一个文件中是不常见的。

  2. std namespace is nearly flat ie almost all standard library functions, types and variables are in it (there are few exceptions such as the filesystem sub-namespace). std命名空间几乎是扁平的,即几乎所有标准库函数、类型和变量都在其中(很少有例外,例如文件系统子命名空间)。 It contains very, very high number of identifiers.它包含非常非常多的标识符。 To my understanding, System contains much fewer names, and instead has more sub-namespaces.据我了解, System包含的名称要少得多,而是具有更多的子命名空间。

  3. In C#, there are no global functions or variables.在 C# 中,没有全局函数或变量。 As such, the number of global identifiers is typically quite small in contrast to C++ which does have those: Furthermore, it is typical to use C libraries (often indirectly) which doesn't have namespaces, and therefore place all their names into the global namespace.因此,与具有这些标识符的 C++ 相比,全局标识符的数量通常非常少:此外,通常使用没有命名空间的 C 库(通常是间接的),因此将它们的所有名称都放在全局标识符中命名空间。

  4. As far as I know, C# has no argument dependent lookup.据我所知,C# 没有依赖参数的查找。 ADL in conjunction with name hiding, overloading etc. can produce cases where some programs are not affected by a name conflict, while others are subtly affected, and catching all corner cases is not feasible with testing. ADL 与名称隐藏、重载等相结合,可能会产生一些程序不受名称冲突影响的情况,而其他程序则受到微妙的影响,并且通过测试捕获所有极端情况是不可行的。

Because of these differences, “using System;”由于这些差异,“使用系统;” has lower chance of name conflict than using namespace std .using namespace std相比,名称冲突的可能性更低。


Also, namespace "importing" is in a way, a self-perpetuating convention: If it is conventional to import a standard namespace, then programmers will conventionally try to avoid choosing names from that namespace for their own identifiers, which helps to reduce problems with such convention.此外,命名空间“导入”在某种程度上是一种自我延续的约定:如果导入标准命名空间是约定俗成的,那么程序员通常会尽量避免从该命名空间中为自己的标识符选择名称,这有助于减少这样的约定。

If such an import is considered a bad practice, then programmers will be less likely to even attempt such avoidance of conflicts with imported namespaces.如果这样的导入被认为是不好的做法,那么程序员甚至不太可能尝试避免与导入的命名空间发生冲突。 As such, conventions tend to get polarised either for or against the practice, even if weights of arguments between the choices were originally subtle.因此,即使选择之间的争论权重最初是微妙的,惯例往往会变得两极分化,无论是支持还是反对实践。

However, despite their differences, using directives in C# and C++ serve the same purpose, which is only having to type SomeType all the time, rather than the much longer Some.Namespace.SomeType (in C++ with :: instead of .).然而,尽管存在差异,但在 C# 和 C++ 中使用指令的目的是相同的,它只需要一直输入 SomeType,而不是更长的 Some.Namespace.SomeType(在 C++ 中使用 :: 而不是 .)。 And with this same purpose, also the danger appears to be theto me: naming collisions.出于同样的目的,对我而言,危险似乎也在于:命名冲突。

Yes, but you didn't export that danger (read: forcing others to deal with it), because of:是的,但你没有输出那个危险(阅读:强迫他人处理它),因为:

Now, I do understand that using directives in C# and C++ are not exactly the same thing.现在,我明白在 C# 和 C++ 中使用指令并不完全相同。 Also, I do understand that one of the most nasty things you can do with using namespace in C++, namely putting it in a header file, has no equivalent in C# due to the lack of a concept of header files and #include.此外,我确实理解在 C++ 中使用命名空间可以做的最讨厌的事情之一,即将它放在头文件中,由于缺乏头文件和#include 的概念,因此在 C# 中没有等价物。

So it's rather a different category of thing.所以它是一个不同的类别。

Also, C++ is not "designed" to be developed in an IDE the same way C# is.此外,C++ 并非“设计”为在 IDE 中以与 C# 相同的方式进行开发。 C# is basically always written in Visual Studio with its Intellisense and whatnot. C# 基本上总是在 Visual Studio 中编写,并带有 Intellisense 和诸如此类的东西。 It's designed to be used that way, by the people who created it.它旨在由创建它的人以这种方式使用。 Regardless of how many people use an IDE to develop in C++, it is not designed with that use case as an overwhelming concern.无论有多少人使用 IDE 来用 C++ 进行开发,它的设计都没有将这种用例作为压倒性的问题。

Namespaces appear to be much more "fine granular" in C# than in C++.命名空间在 C# 中似乎比在 C++ 中更“细粒度”。

Yes, that too.是的,也是。 using namespace std and using System.Collection.Generic are incomparable. using namespace stdusing System.Collection.Generic是不可比的。

So don't compare them!所以不要比较它们!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么使用System.Threading.Thread.Sleep()是一种不好的做法? - Why using System.Threading.Thread.Sleep() is a bad practice? 为什么枚举中的位置标记(如第一个或最后一个)被视为不良做法? - Why are position markers, like first or last, in an Enumeration considered bad practice? 为什么在C ++中将dynamic_cast视为不良做法? - Why is dynamic_cast considered bad practice in C++? 使用动态是否被认为是不好的做法? - Is the use of dynamic considered a bad practice? 在属性中声明变量被认为是不好的做法吗? - Is it considered bad practice to declare variables within a property? 在地图定义中进行排序是否被认为是不好的做法? - Is sorting within a Map definition considered a bad practice? 变量名不能以&#39;m_&#39;开头:StyleCop规则1308 - 为什么它被认为是坏(或不好/标准)的做法? - Variable names must not start with 'm_' : StyleCop Rule 1308 - Why is it considered a bad (or not good/standard) practice? 使用IOC容器时,在库中使用New关键字创建实例是否被认为是不好的做法? - Is it considered bad practice to create instances using the New keyword inside a library when using an IOC Container? 如果您使用 <input> 与之相比 <asp:Textbox> ? - Is it considered bad practice if you use an <input> for a textbox compared to <asp:Textbox>? 在多个部分视图中拆分单个表单是否被认为是不好的做法? - Is it considered bad practice to split a single form among multiple partial views?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM