Is Hungarian notation obsolete?

Is Hungarian notation obsolete?

书上说匈牙利命名法已经过时了,我不这样认为。

有人认为现在编译器已经可以很好的检测出类型的不匹配,或者IDE中可以很快的看到类型,所以在c中可能需要,在C++(强类型语言)中就不需要了。
C++ made it harder to do that without wicked casting and the compiler catches most of those kind of errors.  So, I agree with the previous poster that it's now redundant.
Also, modern IDEs allow you to hover the cursor over a variable and show you the variable's definition.


不过我觉得代码不是写给编译器看的,而是写给人看的,这里就有self-documenting和readability的问题。
很明显,如果你看到nIndex 或者strFile或者wndNext,就可以很快知道分别是int CString CWnd类型,而不用回头去看变量定义,这样,看代码时就会很快。
而且,对于MFC程序员来说,更重要一些,因为MFC里面的变量都是用匈牙利命名法的。
If you're programming C++/MFC you're better sticking to hungarian for consistency with the class library & Win32 API declarations.
微软的约定,就是标准了

不过,书上提到在泛型编程中不需要,现在体会还不深,可能是对的。

今天(2006 04 13碰巧看到codeproject的一个vote),结果如下

Option Votes %
Pascal Cased 171 10.6
camel Cased 702 43.4
Fixed letter prefix (eg lLocal) 81 5.0
Hungarian prefix (eg strLocal) 481 29.7
Scope prefix (eg l_Local) 36 2.2
Scope and Hungarian prefix (eg l_strLocal) 125 7.7
Responses 1618

Hungarian Notation排第二.
cp上面有两个链接
Conversations: Hungarian wartHogs ( http://www.cuj.com/documents/s=7989/cujcexp1911hyslop/hyslop.htm)
号称这篇文章就已经明白的说HN过时了(作者也是 c++ coding stardard的作者).
如果不用HN,那么应该用什么样的命名规则呢?
Naming Guidelines( http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconNamingGuidelines.asp)
.Net下的推荐,也许别的地方也可以用.

你可能感兴趣的:(Is Hungarian notation obsolete?)