我正在对初级(也许是高级)软件工程师所犯的常见错误和错误假设进行一些研究。

你坚持时间最长、最终被纠正的假设是什么?

例如,我误解了整数的大小不是标准的,而是取决于语言和目标。说起来有点尴尬,但事实就是这样。

坦率地说;你有什么坚定的信念?你大概坚持了多长时间?它可以是关于一种算法、一种语言、一个编程概念、测试,或者任何关于编程、编程语言或计算机科学的东西。


当前回答

在我刚开始学习c++的时候(很多时候),我周围都是Java学者。当被问及c++相对于Java的优势时(我通常会认为这是一个做作的问题,但就是这样),我会在我的回答中包括c++提供了引用和指针。Java的家伙会看起来难以置信,并建议引用是指针,并把我笑出了房间。我坚持在c++中引用和指针是不同的。

说句公道话,我是对的。引用和指针在语义和语法上是不同的。不幸的是,我用一个谬论来支持我的说法:底层实现是不同的。

我坚信,通过标准化,引用是语法中的名称别名,就像typedef是没有存储空间的类型别名一样。

我确信引用不是对象,也没有存储空间,它们只是提供了从“名称”到“对象”的多个顶级映射。在这方面,我认为它们就像文件系统中的软链接:

Code: int a = 3; int& b = a;

 Names          Objects           Memory

+-----+     +-------------+     +-------+
|  a  |---->|             |     |       |
+-----+     |             |     |       |
            |     int     |---->|   3   |
+-----+     |             |     |       |
|  b  |---->|             |     |       |
+-----+     +-------------+     +-------+

当然,尽管优化可能会导致这种情况,但引用确实有存储空间。它们是不同的对象,即使语法尽力将其从程序员那里抽象出来。

可以这么说,我很失望地了解到,关闭优化的编译器可能会将引用作为指针实现,需要一个解引用操作:我实际上是在文件系统中创建一个硬链接的类比:

Code: int a = 3; int& b = a;

 Names          Objects           Memory

+-----+     +-------------+     +-------+
|  a  |---->|     int     |---->|       |
+-----+     +-------------+     |       |
                                |   3   |
+-----+     +-------------+     |       |
|  b  |---->|     int&    |---->|       |
+-----+     +-------------+     +-------+

标准c++实际上并没有指定引用应该如何实现,所以我的理论可能适用于一些工具链,但在任何主流编译器中都不适用……当然标准中也没有说明。

其他回答

代码越少越好。现在我知道,有时候如果代码行数更多,就更容易阅读/理解,这是值得的

不要使用高级的特定于实现的特性,因为你可能“有时”想要切换实现。我这样做了一次又一次,几乎无一例外地,这种转换从未发生过。

面向对象始终是设计源代码的最佳方式,而且永远都是。

营销人员关心你做什么。

我仍然对以下的一些误解感到困扰——尽管我知道这些误解是正确的,但我仍然试图抓住它们不放:

All stakeholders will make decisions about software design objectively. Those that aren't embroiled in writing the code make all sorts of decisions based entirely on emotion that don't always make sense to us developers. Project budgets always make sense - I've seen companies that are quite happy to drop [just for example] $50,000 a month for years rather than pay $250,000 to have a project completed in 6 months. The government for one loses their annual budget if they don't spend it - so spend it they will, come hell or high water. It astounds me at how many project dollars are wasted on things like this. You should always use the right tools for the right job - sometimes this decision is not in your hands. Sometimes it comes down from on high that "thou shalt use X technology" for this project, leaving you thinking "WTF! Who came up with that ridiculous idea?"... the guy paying your paycheque, that's who, now get it done. Programming ideology comes first and foremost, everything else is secondary. In reality, deadlines and business objectives need to be met in order to get your paycheque. Sometimes you make the worst decisions because you just don't have time to do it the right way... just as sometimes that word is on the tip of your tongue but the minute it takes to recall it makes you choose a different and less ideal word. There isn't always time to do it right, sometimes there is only time to do it - however that may be. Hence oft' seen anti-patterns used by so called experienced developers who have to knock out a solution to a problem 10 minutes before the presentation deadline for the software being delivered to your best client tomorrow.