这绝对是主观的,但我想尽量避免它变成争论。我认为如果人们恰当地对待它,这将是一个有趣的问题。

这个问题的想法来自于我对“你最讨厌的语言的哪五件事?”问题的回答。我认为c#中的类在默认情况下应该是密封的——我不会把我的理由放在这个问题上,但我可能会写一个更完整的解释来回答这个问题。我对评论中的讨论热度感到惊讶(目前有25条评论)。

那么,你有什么有争议的观点?我宁愿避免那些基于相对较少的基础而导致相当宗教的事情(例如,大括号放置),但例如可能包括“单元测试实际上并没有多大帮助”或“公共字段确实是可以的”之类的事情。重要的是(至少对我来说)你的观点背后是有理由的。

请提出你的观点和理由——我鼓励人们投票给那些有充分论证和有趣的观点,不管你是否恰好同意这些观点。


当前回答

将XML存储在关系数据库中的CLOB中通常是一种可怕的逃避。它不仅在性能方面很糟糕,还将正确管理数据结构的责任从数据库架构师转移到应用程序程序员身上。

其他回答

“不要从构造函数调用虚方法”。这只是有时PITA,但只是因为在c#中,我不能决定在构造函数中的哪个点调用基类的构造函数。为什么不呢?.NET框架允许它,那么c#有什么好的理由不允许它呢?

该死的!

规模很重要!修饰你的代码,让它看起来更大。

代码生成很糟糕

我讨厌那些要求你使用代码生成(或复制&粘贴)来处理简单事情的语言,比如JavaBeans的getter和setter。

c#的AutoProperties是朝着正确方向迈出的一步,但是对于具有字段、属性和构造函数参数的dto来说,你仍然需要大量的冗余。

对象不应该处于无效状态

不幸的是,许多ORM框架要求所有实体类都使用零参数构造函数,使用setter填充成员变量。在这些情况下,很难知道为了构造一个有效的对象必须调用哪些setter。

MyClass c = new MyClass(); // Object in invalid state. Doesn't have an ID.
c.setId(12345); // Now object is valid.

在我看来,一个对象不可能发现自己处于无效状态,类的API应该在每次方法调用后主动强制它的类不变量。

构造函数和突变方法应该原子地将对象从一种有效状态转换为另一种有效状态。这个好多了:

MyClass c = new MyClass(12345); // Object starts out valid. Stays valid.

作为一些库的使用者,在尝试使用一个对象之前跟踪是否调用了所有正确的setter是一件非常痛苦的事情,因为文档通常没有提供关于类契约的线索。

源文件太20世纪了。

在函数/方法的主体中,将过程逻辑表示为线性文本是有意义的。即使逻辑不是严格的线性,我们也有良好的编程结构(循环、if语句等),允许我们使用线性文本清晰地表示非线性操作。

But there is no reason that I should be required to divide my classes among distinct files or sort my functions/methods/fields/properties/etc in a particular order within those files. Why can't we just throw all those things within a big database file and let the IDE take care of sorting everything dynamically? If I want to sort my members by name then I'll click the member header on the members table. If I want to sort them by accessibility then I'll click the accessibility header. If I want to view my classes as an inheritence tree, then I'll click the button to do that.

Perhaps classes and members could be viewed spatially, as if they were some sort of entities within a virtual world. If the programmer desired, the IDE could automatically position classes & members that use each other near each other so that they're easy to find. Imaging being able to zoom in and out of this virtual world. Zoom all the way out and you can namespace galaxies with little class planets in them. Zoom in to a namespace and you can see class planets with method continents and islands and inner classes as orbitting moons. Zoom in to a method, and you see... the source code for that method.

基本上,我的观点是,在现代语言中,不管你把你的类放在什么文件中,或者你定义一个类的成员的顺序是什么,那么为什么我们仍然被迫使用这些古老的实践呢?还记得Gmail出来谷歌说的是"搜索,不排序"吗?那么,为什么同样的哲学不能应用于编程语言呢?