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

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

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

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


当前回答

这里有一个多年来对我来说似乎是显而易见的,但对其他人来说却是一个诅咒:在“发布”版本中使用NDEBUG关闭C(或c++)断言几乎总是一个错误。(唯一的例外是时间或空间惩罚是不可接受的)。

原理:如果断言失败,则程序进入了一种状态

从未被检验过 开发人员无法为恢复策略编写代码 开发人员有效地将其记录为不可思议的。

然而,在某种程度上,“行业最佳实践”是,当涉及到客户数据的实时运行时,事情应该只是敷衍了事,期待最好的结果。

其他回答

代码中的大多数注释实际上是一种有害的代码复制形式。

我们花了大部分时间维护别人(或者我们自己)写的代码,糟糕的、不正确的、过时的、误导性的注释肯定是代码中最令人讨厌的工件列表的顶部。

我想最终很多人会忘记它们,尤其是那些花盒子里的怪物。

最好集中精力使代码可读,必要时进行重构,并尽量减少习惯用法和奇怪之处。

另一方面,许多课程告诉我们注释几乎比代码本身更重要,这就导致了下面这一行添加了一个注释到invoiceTotal风格。

I'd say that my most controversial opinion on programming is that I honestly believe you shouldn't worry so much about throw-away code and rewriting code. Too many times people feel that if you write something down, then changing it means you did something wrong. But the way my brain works is to get something very simple working, and update the code slowly, while ensuring that the code and the test continue to function together. It may end up actually creating classes, methods, additional parameters, etc., I fully well know will go away in a few hours. But I do it because i want to take only small steps toward my goal. In the end, I don't think I spend any more time using this technique than the programmers that stare at the screen trying to figure out the best design up front before writing a line of code.

我得到的好处是,我不必不断地处理那些因为我碰巧以某种方式破坏了它而不再工作的软件,并试图找出停止工作的原因和原因。

要成为一名程序员,你必须会打字。

这在那些不知道如何打字,但坚持认为他们可以像任何打字员一样用两根手指搜索和啄的人之间存在争议,或者他们真的不需要花那么多时间打字,或者智能感知减轻了打字的需要……

我从来没有遇到过会打字的人,但坚持认为打字没什么区别。

参见:编程中最肮脏的小秘密

前期设计——不要因为兴奋而开始写代码

我见过很多设计糟糕的应用程序,因为开发人员太兴奋了,以至于他们直接打开白页开始写代码。我知道在开发生命周期中事情会发生变化。但是,如果应用程序具有多种不同的布局和开发方法(从一种形式到另一种形式,从一种方法到另一种方法),则很难处理这些应用程序。

如果没有明确定义任务以及计划如何编写任务,就很难达到应用程序要处理的目标。花点时间(而不是仅仅5分钟),确保在开始编码之前,你已经尽可能多地布局了它。这样你就可以避免顶替你的人不得不承担的意大利面条般的混乱。

您不应该停留在您发现的编写“有效”代码的第一种方法上。

I really don't think this should be controversial, but it is. People see an example from elsewhere in the code, from online, or from some old "Teach yourself Advanced Power SQLJava#BeansServer in 3.14159 minutes" book dated 1999, and they think they know something and they copy it into their code. They don't walk through the example to find out what each line does. They don't think about the design of their program and see if there might be a more organized or more natural way to do the same thing. They don't make any attempt at keeping their skill sets up to date to learn that they are using ideas and methods deprecated in the last year of the previous millenium. They don't seem to have the experience to learn that what they're copying has created specific horrific maintenance burdens for programmers for years and that they can be avoided with a little more thought.

事实上,他们似乎甚至没有意识到做一件事可能有不止一种方法。

I come from the Perl world, where one of the slogans is "There's More Than One Way To Do It." (TMTOWTDI) People who've taken a cursory look at Perl have written it off as "write-only" or "unreadable," largely because they've looked at crappy code written by people with the mindset I described above. Those people have given zero thought to design, maintainability, organization, reduction of duplication in code, coupling, cohesion, encapsulation, etc. They write crap. Those people exist programming in every language, and easy to learn languages with many ways to do things give them plenty of rope and guns to shoot and hang themselves with. Simultaneously.

But if you hang around the Perl world for longer than a cursory look, and watch what the long-timers in the community are doing, you see a remarkable thing: the good Perl programmers spend some time seeking to find the best way to do something. When they're naming a new module, they ask around for suggestions and bounce their ideas off of people. They hand their code out to get looked at, critiqued, and modified. If they have to do something nasty, they encapsulate it in the smallest way possible in a module for use in a more organized way. Several implementations of the same idea might hang around for awhile, but they compete for mindshare and marketshare, and they compete by trying to do the best job, and a big part of that is by making themselves easily maintainable. Really good Perl programmers seem to think hard about what they are doing and looking for the best way to do things, rather than just grabbing the first idea that flits through their brain.

如今,我主要在Java世界中编程。我见过一些非常好的Java代码,但我也见过很多垃圾代码,而且我还看到了更多我在开头描述的心态:人们选择了第一个看起来可以工作的丑陋代码块,而不理解它,也不考虑是否有更好的方法。

You will see both mindsets in every language. I'm not trying to impugn Java specifically. (Actually I really like it in some ways ... maybe that should be my real controversial opinion!) But I'm coming to believe that every programmer needs to spend a good couple of years with a TMTOWTDI-style language, because even though conventional wisdom has it that this leads to chaos and crappy code, it actually seems to produce people who understand that you need to think about the repercussions of what you are doing instead of trusting your language to have been designed to make you do the right thing with no effort.

我确实认为你可能会在另一个方向上走得太远:例如,完美主义完全忽略了你的真正需求和目标(通常是你的业务的真正需求和目标,通常是盈利能力)。但我不认为任何人都能成为一个真正伟大的程序员,除非学会投入一些高于平均水平的努力来思考寻找最好的(或至少是最好的一种)方法来编码他们正在做的事情。