Monday, November 22, 2010

why C++?


因為某些說不清楚的原因,我又開始 study 討厭的 C++

shares 为什么C++?如果效率是人们使用C++的唯一理由,那么为啥不直接用C呢?C被认为比C++效率更高~有很多人更加倾向于用C,最大的理由就是C++实在是太难用对了。甚至Linus也这么想:C++ is a horrible language.

Linus Torvalds:
In other words, the only way to do good, efficient, and system-level and
portable C++ ends up to limit yourself to all the things that are
basically available in C. And limiting your project to C means that people
don't screw that up, and also means that you get a lot of programmers that
do actually understand low-level issues and don't screw things up with any
idiotic "object model" crap.

feels Linus 的說法非常毒辣!刀刀見骨! XDD

shares BS: KISS并不等同于“原始”;KISS意味着用最适合的工具来做事情,这里“最合适”的意思是~工具能够帮你以尽量直接简洁的方式来表达思想,同时又不降低代码的可读性,另外还保持代码容易理解。

shares BS: 有时KISS也是建立在抽象上的。我觉得Matthew Wilson在他新书《Extended STL,卷1》的序言中对此做了很好的阐释。他写了2段代码,一段用C,另一段用C++:

// in C
DIR*  dir = opendir(".");
if (NULL != dir) {
struct dirent*  de;
for(; NULL != (de = readdir(dir)); ) {
struct stat st;
if ( 0 == stat(de->d_name, &st) &&
S_IFREG == (st.st_mode & S_IFMT)) {
remove(de->d_name);
}
}
closedir(dir);
}


// in C++
readdir_sequence entries(".", readdir_sequence::files);
std::for_each(entries.begin(), entries.end(), ::remove);

// In C++09 更简单:
std::for_each(readdir_sequence(".", readdir_sequence::files), ::remove);


says 仔細比對了兩段代碼,C++ 使用了比較少的字數,不像原味的 C 那麼冗長,但也更 cryptic 難以理解。

thinks 如果一段敘述是隱晦的、難以解析的,除了像符咒一樣照著套用之外,該如何重新組合創造出不一樣的用法? 

shares BS: 因为有如此众多方便的C++库,我觉得有充分的理由在这些领域中使用C++——你可以在编码时仅使用C++中的C核心(以任何你喜欢的方式来KISS),同时还能用强大的C++库(比如STL容器、算法和即将到来的tr1/tr2组件)。

feels BS 跳過大家對 C++語法矯枉過正的批評,引導到 C++ 的 class/template library 很強大,可以簡化程式?

feels 我不知道 tr1/tr2,也不清楚 C++0x 的具體內容。不過可以感覺到 BS 把希望壓寶在即將到來的新玩意。。。

shares 電影矽谷海盜,有段名場面:



**Jobs**: We're better than you are! We have better stuff.
**Gates**: You don't get it, Steve. That doesn't matter!

says C++ 在地球上出現超過三十年了,不停修補也就罷了,把重點從 language 本身移轉到 library,光環褪盡也是剛好! XD





No comments:

Post a Comment