首頁 / CompScience / Programming / Languages / C_and_CPP / Brevity Can Be a Virtue

Brevity Can Be a Virtue

C++ Primer 4/e 在算式這裡還有一個忠告:『Programmers new to C++ who have not previously programmed in a C-based language often have trouble with the terseness of some expressions. In particular, expressions such as *iter++ can be bewilderingat first. Experienced C++ programmers value being concise. They are much more likely to write

     cout << *iter++ << endl;

 

than the more verbose equivalent

     cout << *iter << endl;
     ++iter;

 

For programmers new to C++, the second form is clearer because the action of incrementing the iterator and fetching the value to print are kept separate. However, the first version is much more natural to most C++ programmers.

It is worthwhile to study examples of such code until their meanings are immediately clear. Most C++ programs use succinct expressions rather than more verbose equivalents. Therefore, C++ programmers must be comfortable with such usages. Moreover, once these expressions are familiar, you will find them less error-prone.』

中文版的是這樣說:『C++新手如果先前不用過以C為基礎的語言,往往會被一些簡潔算式困擾,特別是可能被像*iter++這種算式給迷惑 - 在第一次看到的時候。C++老手喜歡簡潔,他們比較可能寫:

cout<< *iter++ << endl;

而不寫冗長的等價版本:

cout << *iter << endl;
     ++iter;

對於C++新手,第二種形式比較清楚,因為「遞增iterator」和「擷取其值並列印」被分開來描述。然而第一個版本對大部分C++程式員自然許多。

研讀這種程式碼範例,直到它們的意義能瞬間明朗化,這種努力式值得的。大部分C++程式使用簡潔算式,而非較囉唆的等價版本。因此C++程式員應該適應這種習慣。一旦熟悉這些算式,你會發現它們比較不容易出錯。』

恩,我看得懂,但是確也經過一段不短習慣期。

achis’ English Blog 

馬上成為工作達人的Fans

About ㄚ琪

工作達人Fun Taiwan的創辦者及總編,可以在這裡更認識他。

發表迴響

你的電子郵件位址並不會被公開。 Required fields are marked *

*

Scroll To Top
按讚 !
愛你喔!