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

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

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

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


当前回答

记录器配置是浪费时间。如果这意味着学习一种新的语法,尤其是一种无声地失败的语法,为什么要有它们呢?不要误解我,我喜欢好的伐木。我喜欢日志记录器的继承和向日志记录器的处理程序添加格式化程序。但是为什么要在配置文件中进行呢?

您希望在不重新编译的情况下对日志代码进行更改吗?为什么?如果您将日志代码放在单独的类、文件中,会有什么不同呢?

您是否希望将可配置的日志与您的产品一起分发给客户端?这不是提供了太多信息吗?

最令人沮丧的是,用流行语言编写的流行实用程序往往会按照该语言指定的格式编写良好的api。编写一个Java日志实用程序,我知道您已经生成了javadocs,我知道如何导航。为您的日志记录器配置编写一种特定于域的语言,我们有什么?也许有文件,但到底哪里去了?你来决定怎么组织,我对你的思路不感兴趣。

其他回答

程序员应该不惜一切代价避免通过继承隐藏方法。

In my experience, virtually every place I have ever seen inherited method hiding used it has caused problems. Method hiding results in objects behaving differently when accessed through a base type reference vs. a derived type reference - this is generally a Bad Thing. While many programmers are not formally aware of it, most intuitively expect that objects will adhere to the Liskov Substitution Principle. When objects violate this expectation, many of the assumptions inherent to object-oriented systems can begin to fray. The most egregious cases I've seen is when the hidden method alters the state of the object instance. In these cases, the behavior of the object can change in subtle ways that are difficult to debug and diagnose.

Ok, so there may be some infrequent cases where method hiding is actually useful and beneficial - like emulating return type covariance of methods in languages that don't support it. But the vast majority of time, when developers use method hiding it is either out of ignorance (or accident) or as a way to hack around some problem that probably deserves better design treatment. In general, the beneficial cases I've seen of method hiding (not to say there aren't others) is when a side-effect free method that returns some information is hidden by one that computes something more applicable to the calling context.

像c#这样的语言通过要求在隐藏基类方法的方法上使用new关键字进行了一些改进——至少有助于避免非自愿地使用方法隐藏。但我发现许多人仍然混淆了new和override的含义——特别是在简单的场景中,它们的行为看起来是相同的。如果像FxCop这样的工具实际上有内置的规则来识别方法隐藏的潜在不良使用,那就太好了。

顺便说一下,通过继承隐藏方法不应该与其他类型的隐藏(例如通过嵌套)相混淆,我认为嵌套是一种有效且有用的构造,潜在问题较少。

初级程序员在被允许实际编写或修改代码之前,应该被分配做几个月的对象/模块设计和设计维护。

太多的程序员/开发人员在工作了5年或10年之后,还没有理解好的设计的要素。当他们想要超越编写和维护代码时,这可能会让他们陷入瘫痪。

不要注释你的代码

注释不是代码,因此当事情发生变化时,不改变解释代码的注释是很容易的。相反,我更喜欢将代码中的垃圾重构到不需要注释的程度。一个例子:

if(data == null)  // First time on the page

to:

bool firstTimeOnPage = data == null;
if(firstTimeOnPage)

我唯一一次真正的评论是当它是一个TODO或解释为什么

Widget.GetData(); // only way to grab data, TODO: extract interface or wrapper

复制/粘贴不是反模式,事实上它有助于避免产生更多的错误

我的经验法则——只输入不能复制/粘贴的东西。如果创建类似的方法,类或文件-复制现有的并更改所需的内容。(我不是在谈论复制应该放在单个方法中的代码)。

我通常从不输入变量名——要么复制粘贴,要么使用IDE自动补全。如果需要一些DAO方法-复制类似的方法并更改所需的内容(即使将更改90%)。对某些人来说,这可能看起来是极度的懒惰或缺乏知识,但我几乎从来没有处理过由拼写错误引起的问题,而且它们通常很难捕捉(如果没有在编译级别检测到)。

每当我偏离我的复制粘贴规则,开始输入东西时,我总是拼写错误(这只是一个统计数据,没有人能立即写出完美的文本),然后花更多的时间试图找出哪里。

Xah Lee: actually has some pretty noteworthy and legitimate viewpoints if you can filter out all the invective, and rationally evaluate statements without agreeing (or disagreeing) based solely on the personality behind the statements. A lot of my "controversial" viewpoints have been echoed by him, and other notorious "trolls" who have criticized languages or tools I use(d) on a regular basis. [Documentation Generators](http://en.wikipedia.or /wiki/Comparison_of_documentation_generators): ... the kind where the creator invented some custom-made especially-for-documenting-sourcecode roll-your-own syntax (including, but not limited to JavaDoc) are totally superfluous and a waste of time because: 1) They are underused by the people who should be using them the most; and 2) All of these mini-documentation-languages all of them could easily be replaced with YAML