The Hungarian Notation
Posted on January 22nd, 2006 at 3:46 by fr3@K

在 open source 的世界也好, 在公司內部也好, 任何像樣的 project, 大都免不了採用或自訂一套 coding convention. 不同 coding convention 所要達成的目標並不盡相同, 但都有個共同點 – 讓你寫的 code 看起來像是別人寫的. 主要原因是 – 這樣做應該可以讓他人容易閱讀/理解/修改你寫的 code.

Coding convention 主要分為兩個部份:

  • Naming notation (命名法)
  • Formatting (程式格式/排版)

以我個人經驗以及從朋友處得到的消息來看, 公司內部最常採用的 naming notation 是 Hungarian Notation (匈牙利命名法). 把 Hungarian notation 傳播到世界各地, 甚至錯誤地使用在例如 C++ 之類的 strong-typed (或是接近 strong-typed) 語言上, M$ 功不可沒, Win32 SDK 與 MFC 是其中經典之作. 時至今日, 就連 M$ 內部也已展開了反撲, 最好的例子就是 .NET 丟開 M$ 過去採用 hungarian notation 的包袱.

痛恨 hungarian notation, 但這通常不會造成我在工作上的困擾. 我總是能幸運地說服同事與上司放棄它, 或至少不強迫我採用它. 不過還是常會有朋友跟我抱怨公司強迫他們採用 hungarian notation, 他們多是無奈接受. 這帖 blog, 就是為了這些朋友寫的, 如果你剛好在這些公司工作, 不妨把這帖 blog 的 link 轉給你的同事或上司.

Annoyances of the Hungarian Notation:

  • 視你為弱智. 認為你替變數取的名字大都不好, 很難從名字辨認大約的型別
  • 視你為白痴. 認為你有可能會 – 把一個叫做 size 的變數 (不是 integral type 還能是什麼?) 誤判為一個 ifstream 的 instance
  • 視 strong-typed compiler (如 C++ 編譯器) 為低能. 認為 compiler 有可能會 – 在你把一個叫 name 的變數 (不是某種字串還能是什麼?) 當作 ComboBox 來操作時, 不產生 compile error

我常用的命名法則是盡量口語化, 並試著恰如其分地命名. 舉例說明較快:

  • Instance of integral type
    • unsigned long user_count;
      int offset;
      size_t index, received_bytes;
  • User-defined type
    • class Contact;
      class FastString;
      typedef list<Contact> Contacts; // or ContactList
  • Instance of user-defined type
    • string english_name;
      Contacts kens_contacts; // or contacts_of_ken
      Window main_window;

誤判以上所舉例子型別的人, 或許該考慮換個工作 :( .

當然, 上例都是淺顯的例子, 從我個人經驗來說, 應用這原則倒也沒遇過特別困難的狀況. 我想說的並不是我用的方法多好, 而是希望別再有更多人強迫他人或被他人強迫用什麼彆腳的 dwData, wndMain, ctrlBrowser, nCount, szName 來寫 C++.

[Update] Sutter on Hungarian Notation.

del.icio.us:The Hungarian Notation digg:The Hungarian Notation spurl:The Hungarian Notation newsvine:The Hungarian Notation furl:The Hungarian Notation Y!:The Hungarian Notation 黑米共享書籤:The Hungarian Notation 推推王:The Hungarian Notation
Previous Post
« C++ Terminology – 0. 前序 «
Next Post
» C++ Terminology – 1. Variety of Types »

7 Comments »

Comment #39

size 可以是float, double, DWORD, WORD, size_t
某些時候我希望是 D3DVECTOR3 ….(3度空間下)

所以我不認為Hungarian Notation就是很笨的方法.
雖然我也不是完全遵守…

Comment by 也用C++的程式設計師 — July 12, 2006 @ 15:36


Comment #40

我能想像到型別為 float, double 等的 variable/argument 都跟 size 無關呢. 我想到的是 ratio, rate 等等. 不過這也有可能是因為我工作的項目都與浮點數無關 (或許跟我缺乏想像力比較有關系), 導致我想像不到.

DWORD, WORD, size_t and etc. 不都是整數 (integral) 型別嗎? 你大概沒注意看吧 :P

或許是我表達得不夠好, 我想說的重點是與其提倡或盲從 hungarian notation, 不如鼓勵好的 variable/argument naming. 而這會導致一個好的副作用, 就是由 variable/argument name 即為 variable/argument 型別的提示.

在我個人的經驗, 這已經足以應付絕大部分情況. 又, 運用 hungarian notation 該如何準確命名一個 tree view control (假設這個 control 的型別名稱為 CTreeView)? 或許可叫 tvCtrl, ctrlTreeView 等等. 可是這樣的 variable/argument name 雖然或多或少能一目了然它的型別, 卻是一個非常不好的命名, 因為這只能讓人知道型別卻不了解用途. 命名為 folderView (假設用途是顯示某個目錄以下的結構) 或是 familyTree (假設用途是顯示某人的族譜) 等等, 既能清楚表達用途, 也多少能讓人看出大致型別, 這不是更好嗎?

Comment by fr3@K — July 12, 2006 @ 18:01


Comment #13056

最近讀了《软件随想录》(More Joel on Software)
其中有一篇講到Hungarian notation的故事
扭轉了我的印象, 也可能扭轉你的…
在”Apps Hungarian”和”Systems Hungarian”的差別.
原始blog連結:(請直接跳到後1/3 “I’m Hungary”)
http://www.joelonsoftware.com/articles/Wrong.html
最後還有reference可以看呢

Comment by Chang — March 31, 2010 @ 0:19


Comment #13112

Thanks, Chang. I will check it out.

Comment by fr3@K — April 2, 2010 @ 15:13


Comment #13125

Hi Chang,

你提到 Making Wrong Code Look Wrong 可能會扭轉我對 Hungarian notation 的印象. 我假設你指的是扭轉我對把 Hungarian notation 用在 C/C++ 中的壞印象.

拜訪了之後才想起, 在一年多前一位朋友也曾把這篇文章的 link 留在我的 另一篇文字的討論 中:

同意您的看法。討論Hungarian Notation的資料還蠻多的:
Joel Spolsky 也有類似的看法。(http://www.joelonsoftware.com/articles/Wrong.html)

我的理解也是該文與我的看法相近, quote:

Apps Hungarian was extremely valuable, especially in the days of C programming where the compiler didn’t provide a very useful type system.

我同意並完全理解在 pre-standard C 的時代中 Apps Hungarian 能帶來的好處. 但在今天的 C/C++ 環境中 – 特別是 C++ – 除非 programmer 刻意干擾 type system, 否則 compiler 可是比你我的眼睛還要挑剔呢.

Another quote:

So in Systems Hungarian you got a lot of dwFoo meaning “double word foo,” and doggone it, the fact that a variable is a double word tells you darn near nothing useful at all.

是我會錯了你的意思還是沒看懂 Joel 的文章?

Comment by fr3@K — April 4, 2010 @ 22:50


Comment #13126

嗯… 其實我也沒有想很多, 只是上個月看了書之後才很震驚的發現還有分App & Sys兩種. 您這邊沒特別提這兩種, 我就手癢comment了一下:Q…

Sutter的文章也提到, 現在講HN幾乎都是講SysHN,並提到在c++ codeing standard一開始就寫, 不要在小東西上面花太多功夫. 我也是超痛恨要精神分裂去時時注意變數的型別並加上(sys/type)HN, 而且一點好處都沒有享受到.

看來AppHN跟變數型別是沒有直接關係的,如果AppHN讓我們注意的不是小東西而且有好處的話, 我倒是會很感謝的. 那AppHN現在用在C/C++上好不好呢? 也許需要思考加上使用才會有感覺吧…

quote Joel’s last section on it:

But there’s still a tremendous amount of value to Apps Hungarian, in that it increases collocation in code, which makes the code easier to read, write, debug, and maintain, and, most importantly, it makes wrong code look wrong.

可惜他沒多給一些例子, 不過我想他應該還是覺得AppHN現在還是好的吧.(that post is 2005, Hmmm….)

Comment by Chang — April 5, 2010 @ 0:27


Comment #13135

Hi Chang,

根據我粗潛的了解, Apps Hungarian 是一種幫助人眼確認 type correctness 的方法, 我戲稱它為 人肉 type safety 機制.

關於你在上一個 comment 留的問題:

那AppHN現在用在C/C++上好不好呢? 也許需要思考加上使用才會有感覺吧…

關於這個問題, 如果有人對我的想法有興趣的話, 我還是那句 老話:

在今天的 C/C++ 環境中 – 特別是 C++ – 除非 programmer 刻意干擾 type system, 否則 compiler 可是比你我的眼睛還要挑剔呢.

不論是 Apps Hungarian 還是 Systems Hungarian.

Comment by fr3@K — April 6, 2010 @ 17:58


Comments RSS TrackBack URI

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>