"Somebody, somewhere, read Simonyi’s paper, where he used the word “type,” and thought he meant type, like class, like in a type system, like the type checking that the compiler does. He did not. He explained very carefully exactly what he meant by the word “type,” but it didn’t help. The damage was done."
2 - this is useless information as the type is either already in the declaration or you use a dynamic language where the actual type should not be so important in the first place.
The type information does not carry the right information, it is just paraphrasing the variable declaration, instead of outlining the 目的 of the variable (think 我的整数 vs. 页面大小).
For the compile-time type-checking languages it is mostly useless -- scrolling up a few lines should reveal the declaration and thus type. If you global variables or your code block spans for much more than one screen, you have grave design and reusability issues. Thus one of the criticisms is that Hungarian Notation allows developers to have bad design and easily get away with it. This is probably one of the reasons for hatered.
On the other hand, there can be cases where even compile-time type-checking languages would benefit from Hungarian Notation -- 无效 pointers or 把手's in win32 API. These obfuscates the actual data type, and there might be a merit to use Hungarian Notation there. Yet, if one can know the type of data at build time, why not to use the appropriate data type.
In general, there are no hard reasons not to use Hungarian Notation. It is a matter of likes, policies, and coding style.
In Joel Spolsky 让错误的代码看起来错误 he explains that what everybody thinks of as Hungarian Notation (which he calls Systems Hungarian) is not what was it was really intended to be (what he calls Apps Hungarian). Scroll down to the 我是匈牙利人 heading to see this discussion.
基本上,Systems匈牙利语是没有价值的,它只是告诉你你的编译器和/或 IDE 会告诉你同样的事情。
Apps Hungarian tells you what the variable is supposed to mean, and can actually be useful.
Hungarian notation only makes sense in languages 没有用户定义的类型. In a modern functional or OO-language, you would encode information about the "kind" of value into the datatype or class rather than into the variable name.
Second, if you believe in good modularized code and don't do too much work in any 1 function, you're variables are probable declared just above the code they are used in anyway (so you have the type right there).
Third, if you prefix every pointer with p and every class with C, your really screwing up your nice modern IDE's ability to do intellisense (you know that feature where it guesses as you type what class name your typing and as soon as it gets it right you can hit enter and it completes it for you? well, if you prefix every class with C, you always have at least 1 extra letter to type)...
你没有理由不使用匈牙利命名法。它之所以不受欢迎,是因为长期以来人们一直反对匈牙利命名法的 滥用,尤其是在 Windows API 中。
In the bad-old days, before anything resembling an IDE existed for DOS (odds are you didn't have enough free memory to run the compiler under Windows, so your development was done in DOS), you didn't get any help from hovering your mouse over a variable name. (Assuming you had a mouse.) What did you did have to deal with were event callback functions in which everything was passed to you as either a 16-bit int (WORD) or 32-bit int (LONG WORD). You then had to cast those parameter to the appropriate types for the given event type. In effect, much of the API was virtually type-less.
That "application" information he's talking about 应该在类型系统中进行编码. You should not depend on flipping variable names to make sure you don't pass unsafe data to functions requiring safe data. You should make it a type error, so that it is 不可能 to do so. Any unsafe data should have a type that is marked unsafe, so that it simply cannot be passed to a safe function. To convert from unsafe to safe should require processing with some kind of a sanitize function.
因此,匈牙利语的中心主张是对于非类型的事物是错误的。它被用于类型信息。当然,类型信息比传统的 C 类型信息更丰富; 类型信息对某种语义细节进行编码,以表明对象的用途。但它仍然是类型信息,正确的解决方案一直是将其编码到类型系统中。将其编码到类型系统中是获得适当验证和强制执行规则的最佳方法。变量名称根本无法切断芥末。
Of course when 99% of programmers agree on something, there is something wrong. The reason they agree here is because most of them have never used Hungarian notation correctly.
这对于代码维护来说是个大问题。
这意味着你可以使用好的 ol’单行文本搜索
(grep,findstr,‘ find in all files’)查找每一次提到这个“想法”的地方。
如果我们的 IDE 知道如何读取代码,为什么这一点如此重要呢?
因为他们还不是很擅长这个,在一个小的代码库中很难看到,
但是很明显,在一个大的-当“想法”可能被提到在评论中,
XML 文件、 Perl 脚本,以及源代码管理之外的地方(文档、 wiki、,
漏洞资料库)。
即使在这里,你也必须要小心一点——例如,在 C/C + + 宏中使用令牌粘贴
可以隐藏标识符的提及。这种情况可以使用
coding conventions, and anyway they tend to affect only a minority of the identifiers in the
代码库。
显然,强类型可以解决 Joel 文章中的安全与不安全字符串示例,但是你不会为位置和速度向量定义单独的类型; 它们都是大小为3的双数组,你可能对其中一个做的任何事情都可能适用于另一个。此外,将位置和速度连接起来(形成一个状态向量)或者取它们的点积,也是非常有意义的,但是可能不需要加上它们。输入如何允许前两种操作,禁止后两种操作,这样的系统如何扩展到您可能想要保护的每一种可能的操作?除非你愿意把所有的数学和物理加密到你的打字系统里。
On top of all that, lots of engineering is done in weakly typed high-level languages like Matlab, or old ones like Fortran 77 or Ada.
因此,如果你有一个奇特的语言,而 IDE 和应用程序匈牙利语并不能帮助你,那么忘记它吧——很多人显然已经忘记了。但是对于我来说,我比一个使用弱类型或动态类型语言的新手程序员更糟糕,我可以使用匈牙利应用程序比没有匈牙利应用程序更快地编写更好的代码。