C 的回归
周末出差,去另一个城市给公司的一个项目解决点问题。回程去机场的路上,我用手机上 google reader 打发时间。第一眼就看到孟岩大大新的一篇:Linux之父话糙理不糙 。主题是 C 与 C++ 的语言之争。转到刘江的 blog 下读完了 Linux之父炮轰C++:糟糕程序员的垃圾语言 大呼过瘾。立刻把链接短信发给了几个朋友。
语言之争永远是火药味十足的话题。尤其是 C 和 C++ 的目标市场又有很高的重合性,C++ 程序员往往对C++ 其有着宗教般的虔诚。我想,今天我在自己的 blog 上继续这个战争,一定会换来更多的骂名。只不过这次 Linus 几句话真是说到我心坎里去了,不喊出来会憋坏的 :D
首先,给不熟悉我的朋友做一个技术背景的自我介绍:
我不是一个 Linux 的 fans ,虽然我今天对 Windows 也没有什么好感,但我的大部分工作还是在 WIndows 上做应用软件开发的,对 Windows 还算熟悉。现在我也用非 Windows 的系统,但那是一台 FreeBSD 的机器,不是 Linux 。
我自认为对 C++ 相当熟悉,精读过市面上能买到的关于 C++ 的大部分书籍,像 D&E of C++ 这样的经典还读了不只一遍。用 C++ 写过至少数十万行代码,阅读过 STL 的大部分源码,和 ACE / Boost 的一小部分。
曾经我是 C++ 的忠实粉丝,如果谁说 C++ 的不是,要么会选择跟他辩论到底,要么会对此人不屑一顾。
还有一点我认为非常重要:我第一次爱上 C++ 是 15 年前(1992 年),然后对其慢慢冷淡,回归 C 的怀抱。而到了 2000 年,我又一次爱上 C++ 。也就是说,从热爱 C++ 到否定它,在我的个人经历中,有过两次。不排除未来有第三次的可能,但这一点足可说明,否定 C++ 是出于一种理性的判断,而不是一种冲动。
写上这些,并非是想倚老卖老。我知道,想骂我的 C++ 程序员,更讨厌有人倚老卖老的数落 C++ 的不是。而且论资格,我顶多及的上 Linus 大大的一个零头,既然有老人在前撑腰,下面说话的底气就可以足一些了 :)
C 是 C++ 的一个子集(从 C99 开始已经不是了),用 C 能写出来的代码,C++ 一样可以写出来,然后可以完成的更好。
这是新手们自以为是的攻击武器。Linus 用了一个很恰当的理由做出反击:“你当然可以用任何语言编写糟糕的代码。但是,有些语言,尤其是带有一些心智(mental)包袱的语言本身就非常糟糕。”
没错,我最想说的就是这个。C++ 就是一个“带有一些心智(mental)包袱的语言”。这对软件设计的影响非常之大,没有经年的软件开发实践很难理解这一点。
从这一点上展开,把 ASM 和 C 比较的问题和 C 与 C++ 的比较相提并论就没有意义了。
接下来要找到的问题要点就是,C++ 比 C 多出来那些东西后,真的会带来心智包袱吗?这个问题不好回答。单纯从 C++ 语言特性的繁杂导致的不易掌握和误用这些角度是很难说服我自己的,更别说去说服那些比我聪明的多,刻苦的多的 C++ 程序员们。我自认为对所谓 C++ 的高级特性掌握的还是不错的,并运用在诸多实际项目中。他们相当有趣,在某种程度上也非常的有效。代码可以获得相当高的执行效率,并可以缩短编码的时间(更少的键击数),完成他们也有很大的成就感。
好了,让我再引用 Linus 的一句说到我心坎里的话。“字符串/内存管理根本无关紧要。这不是重要的部分,而且也不复杂。唯一真正重要的部分是设计。”
设计!这才是重中之重。
如果要说,这最近 10 年的程序员生涯我学会了什么?我认为,我比以前能设计出更好的代码了。能更准确的把握设计的坏味道。而对编程语言的掌握,对操作系统的熟悉,工作相关知识的了解等等。那些只是自然而然发生的事,那些是知识的积累,而非能力的提高。
“抽象”,“面向对象”,“设计模式”,这些重要吗?重要。对软件开发相当重要。但重要不是必要,执迷于“抽象”会使你离目标越来越远。当我们一次又一次的提取出事物的共性,建立起抽象层的时候,我们可能丢弃了真实。C++ 继承了 C 语言中“信任程序员”这一设计哲学,致力于让程序员在建立抽象层时,可以不做出额外的消耗。他的解决方式是提供尽可能多的语言工具和设计选择,任何一个都允许你在不用的时候不带来额外的性能损失。
这是一个美好的愿景:C++ 程序员指望可以建立强大的可复用的抽象层,面对世界上一切的具体应用。同时 CPU 执行序列在穿越这个坚厚的抽象层的过程中,居然可以以光速通过(通过抽象层没有额外的执行效率付出)。为此:C++ 社区创造了 STL ,创造了 Boost 。它们共同的关键词是:效率、复用。
再往上呢?另一个问题产生了:“——低效的抽象编程模型,可能在两年之后你会注意到有些抽象效果不怎么样,但是所有代码已经依赖于围绕它设计的‘漂亮’对象模型了,如果不重写应用程序,就无法改正。”这一段依旧是 Linus 语,我不停的引用,是因为我明白这一点,但是不能表达的更清楚。
使用 C++ 的程序员不断的强调复用性,却不断的需要重写代码。如果一段代码可以不被重写,那多半是因为对重写工程量的妥协。是的,其实我们可以用 C++ 的各种特性写出更好,更漂亮,更高效的代码。两年前的框架不那么完美,不是 C++ 语言的错,是两年前的我能力有限的缘故。但是因为需要改写的是设计框架,这意味着我们必须跟着变更已经完成的功能模块,或是加上桥接层。
的确,STL 和 Boost 都是世界顶尖程序员完成的。代码质量非常的高(当然,我对 Boost 的一部分持保留意见)。我不拿编译器兼容性和可移植性或是编译速度说事,虽然这些的确是现实问题,但不足以成为反对 C++ 基础类库的理由。
好好的用好 C++ 当然得用好 STL ,Boost 也应该认真考察一下。能够仔细读一下源码更好。合格的 C++ 程序员应该做这个。否则作为 C++ 程序员你就违背了 C++ 语言的设计哲学:C++ 信任了你,你就该对的起这种信任,搞清楚你写的每一行代码背后,机器都去干了什么。
但是,STL 过于庞大了,Boost 更加是。我不是抱怨阅读和学习它们的源码的难度和需要的时间和精力。正相反,我在学习它们的过程中充满了乐趣和感激之情。高手前辈透过这些高质量的代码教会了我很多东西。我隐隐担心的是,这么庞大的代码,它的设计不可能是永远正确的。两年之后,他们的设计肯定依旧正确,再两年还是的。但是我几乎敢肯定,放之更长远的时间来看,绝对会在某些设计领域发现其不是最佳的选择。到那一天,我们会选择修改吗?我想 C++ 社区会被迫选择妥协。但是,C++ 程序员心中会充满痛苦。
C 在这个问题上的抉择是不一样的。在效率问题上,C 程序里最令人担心的是函数调用的消耗。C++ 程序员最津津乐道的案例就是 std::sort 全面击败了 C 库中的 qsort 。C 语言的失败正在于多余的函数调用消耗。
但是,从一开始 C 就选择了承认函数调用的消耗,而这一点几乎是唯一。付出了这个代价后,设计失误导致的效率下降问题几乎总可以避免。C 和 C++ 都可以选择重写设计失败的部分,但不一样的是, C 程序员几乎可以不考虑妥协的问题。同样的是考虑极端效率的语言,C 语言坦然面对缺陷,才是真正的符合了 KISS 原则。
我对这个问题的见解,可以再引用 Linus 的一段话作为收场。“如果你想用更花哨的语言,C++绝对是最糟糕的选择。如果想要真正的高级特性,那就选择有垃圾回收或者好的系统集成的,而不是既缺乏C的简约(sparseness)又缺乏C的直接而且没有重要概念的高层绑定(high-level bindings to important concepts)的东西。”。这是我最近几年来一直坚持的观点:C++ 的发展,一定要补充对 GC 支持所需要的特性。
强调一下,我并不讨厌 C++ :) 。 C++ 的粉丝们可以随便骂我,但是不要带上阶级仇恨。
ps. 最近两年多,我在做一个游戏引擎的项目。这个项目现在是第三个版本了。第一个版本是用 C++ 实现的,但是没有用任何已存在的类库(包括 STL)。在第二个版本中,我去掉了所有使用 C++ 高级特性实现的部分,只使用了 C++ 基本特性实现所有。今年重写的第三个版本,全部换成 C 代码了。这个项目的发展,可以反应出我个人对 C/C++ 理解的心路过程。
Comments
Posted by: metalmoon | (192) August 17, 2023 04:51 PM
Posted by: WYW | (191) November 7, 2017 09:50 PM
Posted by: QQ群发器 | (190) August 23, 2016 12:06 AM
Posted by: 吴道 | (189) June 20, 2016 04:36 PM
Posted by: 某人 | (188) May 12, 2016 07:50 PM
Posted by: Anonymous | (187) February 4, 2016 03:07 AM
Posted by: 杨晓 | (186) February 9, 2015 03:01 PM
Posted by: lichking | (185) September 14, 2014 04:48 PM
Posted by: 语音群呼 | (184) September 12, 2014 05:34 PM
Posted by: papivan | (183) October 4, 2012 08:51 PM
Posted by: 2222222222 | (182) December 16, 2011 04:42 PM
Posted by: 小良 | (181) August 3, 2010 11:58 AM
Posted by: adc | (180) July 11, 2009 07:59 AM
Posted by: 赵中 | (179) December 26, 2008 11:53 AM
Posted by: Cloud | (178) October 13, 2008 12:49 PM
Posted by: freebsd | (177) October 11, 2008 04:09 AM
Posted by: sdk | (176) June 5, 2008 07:43 AM
Posted by: pass86 | (175) April 24, 2008 10:50 PM
Posted by: cat | (174) April 2, 2008 11:19 PM
Posted by: rodder | (173) February 16, 2008 11:33 AM
Posted by: hm | (172) February 14, 2008 02:34 PM
Posted by: hm | (171) February 14, 2008 02:10 PM
Posted by: hm | (170) February 14, 2008 01:42 PM
Posted by: hm | (169) February 14, 2008 01:04 PM
Posted by: hm | (168) February 14, 2008 12:29 PM
Posted by: Anonymous | (167) February 13, 2008 04:43 PM
Posted by: Anonymous | (166) February 13, 2008 12:12 PM
Posted by: hm | (165) February 13, 2008 10:11 AM
Posted by: hm | (164) February 13, 2008 08:07 AM
Posted by: Cloud | (163) February 12, 2008 04:32 PM
Posted by: hm | (162) February 12, 2008 11:33 AM
Posted by: hm | (161) February 12, 2008 11:11 AM
Posted by: hm | (160) February 12, 2008 10:51 AM
Posted by: hm | (159) February 12, 2008 10:39 AM
Posted by: Anonymous | (158) February 11, 2008 09:21 PM
Posted by: Anonymous | (157) February 11, 2008 09:13 PM
Posted by: Anonymous | (156) February 11, 2008 08:24 PM
Posted by: Anonymous | (155) February 11, 2008 07:45 PM
Posted by: ioriliao | (154) January 1, 2008 12:27 PM
Posted by: Atry | (153) December 12, 2007 03:27 AM
Posted by: Brin | (152) December 5, 2007 08:06 AM
Posted by: Atry | (151) October 18, 2007 03:49 PM
Posted by: Cloud | (150) October 14, 2007 10:35 PM
Posted by: 迷茫 | (149) October 14, 2007 12:27 AM
Posted by: 愚人 | (148) October 5, 2007 10:09 PM
Posted by: Anonymous | (147) October 3, 2007 05:23 PM
Posted by: hi | (146) September 27, 2007 02:14 PM
Posted by: hi | (145) September 27, 2007 02:13 PM
Posted by: hi | (144) September 27, 2007 02:11 PM
Posted by: hi | (143) September 27, 2007 02:10 PM
Posted by: hi | (142) September 27, 2007 02:08 PM
Posted by: hi | (141) September 27, 2007 02:05 PM
Posted by: vczh | (140) September 21, 2007 10:27 PM
Posted by: Anonymous | (139) September 21, 2007 05:59 PM
Posted by: 小文 | (138) September 20, 2007 08:36 AM
Posted by: dkf | (137) September 19, 2007 05:39 PM
Posted by: dfk | (136) September 19, 2007 05:30 PM
Posted by: flying | (135) September 19, 2007 11:01 AM
Posted by: flying | (134) September 19, 2007 10:45 AM
Posted by: nothanks | (133) September 19, 2007 09:03 AM
Posted by: Anonymous | (132) September 18, 2007 07:16 PM
Posted by: Cloud | (131) September 18, 2007 06:09 PM
Posted by: david | (130) September 18, 2007 05:14 PM
Posted by: david | (129) September 18, 2007 05:12 PM
Posted by: Cloud | (128) September 18, 2007 04:54 PM
Posted by: nothanks | (127) September 18, 2007 02:16 PM
Posted by: Anonymous | (126) September 18, 2007 01:11 PM
Posted by: Wesley | (125) September 17, 2007 02:49 PM
Posted by: 11 | (124) September 17, 2007 02:41 PM
Posted by: 11 | (123) September 17, 2007 02:39 PM
Posted by: Wesley | (122) September 17, 2007 02:30 PM
Posted by: 路人甲 | (121) September 16, 2007 08:57 PM
Posted by: 11 | (120) September 16, 2007 11:35 AM
Posted by: 11 | (119) September 16, 2007 11:32 AM
Posted by: Anonymous | (118) September 16, 2007 08:29 AM
Posted by: wodiw | (117) September 15, 2007 11:22 PM
Posted by: wingfiring | (116) September 15, 2007 02:43 PM
Posted by: churunmin@gmail.com | (115) September 15, 2007 01:30 AM
Posted by: 混口饭吃 | (114) September 15, 2007 12:49 AM
Posted by: Initial_X | (113) September 14, 2007 03:35 PM
Posted by: PX | (112) September 14, 2007 03:31 PM
Posted by: Cloud | (111) September 14, 2007 03:15 PM
Posted by: rockcarry | (110) September 14, 2007 02:36 PM
Posted by: wingfiring | (109) September 14, 2007 02:25 PM
Posted by: Wesley | (108) September 14, 2007 02:10 PM
Posted by: nette | (107) September 14, 2007 12:54 PM
Posted by: mike | (106) September 14, 2007 12:07 PM
Posted by: Anonymous | (105) September 14, 2007 11:00 AM
Posted by: Atry | (104) September 14, 2007 10:07 AM
Posted by: Atry | (103) September 14, 2007 10:04 AM
Posted by: Wesley | (102) September 14, 2007 06:10 AM
Posted by: phoolimin | (101) September 13, 2007 10:39 PM
Posted by: 果子 | (100) September 13, 2007 10:20 PM
Posted by: Cloud | (99) September 13, 2007 02:45 PM
Posted by: fastzhao | (98) September 13, 2007 02:09 PM
Posted by: lk | (97) September 13, 2007 10:17 AM
Posted by: rainwolf911 | (96) September 13, 2007 10:05 AM
Posted by: 安德尔斯 | (95) September 13, 2007 07:10 AM
Posted by: 安德尔斯 | (94) September 13, 2007 07:09 AM
Posted by: x | (93) September 12, 2007 11:57 PM
Posted by: simgenes | (92) September 12, 2007 10:27 PM
Posted by: yinRed | (91) September 12, 2007 07:53 PM
Posted by: C.K | (90) September 12, 2007 06:10 PM
Posted by: Rome | (89) September 12, 2007 05:19 PM
Posted by: Anonymous | (88) September 12, 2007 05:19 PM
Posted by: lujunneng | (87) September 12, 2007 04:00 PM
Posted by: wuhanhoutao | (86) September 12, 2007 03:04 PM
Posted by: wuhanhoutao | (85) September 12, 2007 03:03 PM
Posted by: xrainfir | (84) September 12, 2007 03:02 PM
Posted by: Anonymous | (83) September 12, 2007 02:51 PM
Posted by: skyworld | (82) September 12, 2007 01:13 PM
Posted by: larrin | (81) September 12, 2007 01:09 PM
Posted by: Anonymous | (80) September 12, 2007 01:04 PM
Posted by: rockcarry | (79) September 12, 2007 12:49 PM
Posted by: ken | (78) September 12, 2007 12:39 PM
Posted by: htqx | (77) September 12, 2007 10:32 AM
Posted by: 只相信資料 | (76) September 12, 2007 10:13 AM
Posted by: Naga | (75) September 12, 2007 10:03 AM
Posted by: zl | (74) September 12, 2007 09:14 AM
Posted by: chai | (73) September 12, 2007 09:09 AM
Posted by: nothanks | (72) September 12, 2007 09:08 AM
Posted by: Linker M Lin | (71) September 11, 2007 11:26 PM
Posted by: baojian | (70) September 11, 2007 09:52 PM
Posted by: jsxxxly | (69) September 11, 2007 09:21 PM
Posted by: Naga | (68) September 11, 2007 08:39 PM
Posted by: luo | (67) September 11, 2007 08:22 PM
Posted by: Diviner | (66) September 11, 2007 06:52 PM
Posted by: 摩天 | (65) September 11, 2007 06:04 PM
Posted by: rockcarry | (64) September 11, 2007 06:03 PM
Posted by: ljianl | (63) September 11, 2007 05:18 PM
Posted by: 韩锋 | (62) September 11, 2007 04:40 PM
Posted by: ywthegod | (61) September 11, 2007 04:31 PM
Posted by: nothanks | (60) September 11, 2007 04:26 PM
Posted by: rockcarry | (59) September 11, 2007 04:21 PM
Posted by: zzzzz | (58) September 11, 2007 03:57 PM
Posted by: rockcarry | (57) September 11, 2007 03:52 PM
Posted by: rockcarry | (56) September 11, 2007 03:44 PM
Posted by: jiantang | (55) September 11, 2007 03:26 PM
Posted by: rockcarry | (54) September 11, 2007 03:16 PM
Posted by: david | (53) September 11, 2007 03:12 PM
Posted by: Anonymous | (52) September 11, 2007 02:19 PM
Posted by: pi1ot | (51) September 11, 2007 02:01 PM
Posted by: wuqingman | (50) September 11, 2007 01:57 PM
Posted by: 天堂的隔壁 | (49) September 11, 2007 01:52 PM
Posted by: 我想看資料 | (48) September 11, 2007 01:42 PM
Posted by: cnm | (47) September 11, 2007 01:23 PM
Posted by: lost | (46) September 11, 2007 12:51 PM
Posted by: Zwinger | (45) September 11, 2007 12:47 PM
Posted by: realzjie | (44) September 11, 2007 12:36 PM
Posted by: windlucid | (43) September 11, 2007 12:34 PM
Posted by: 张杰 | (42) September 11, 2007 12:22 PM
Posted by: Linux鼻祖不读书不学习不进步 | (41) September 11, 2007 12:00 PM
Posted by: cjamie | (40) September 11, 2007 11:59 AM
Posted by: david | (39) September 11, 2007 11:28 AM
Posted by: Rome | (38) September 11, 2007 09:59 AM
Posted by: nothanks | (37) September 11, 2007 09:25 AM
Posted by: k120 | (36) September 11, 2007 09:06 AM
Posted by: lxlxdw | (35) September 11, 2007 09:05 AM
Posted by: nR | (34) September 11, 2007 05:02 AM
Posted by: Cloud | (33) September 11, 2007 01:04 AM
Posted by: Atry | (32) September 10, 2007 11:27 PM
Posted by: 极光炫影 | (31) September 10, 2007 10:56 PM
Posted by: efdev | (30) September 10, 2007 10:44 PM
Posted by: foucaoult | (29) September 10, 2007 10:29 PM
Posted by: Cloud | (28) September 10, 2007 10:17 PM
Posted by: windlucid | (27) September 10, 2007 09:59 PM
Posted by: windlucid | (26) September 10, 2007 09:56 PM
Posted by: missdeer | (25) September 10, 2007 08:24 PM
Posted by: pencilend | (24) September 10, 2007 06:03 PM
Posted by: david | (23) September 10, 2007 05:28 PM
Posted by: Anonymous | (22) September 10, 2007 04:56 PM
Posted by: Atry | (21) September 10, 2007 03:03 PM
Posted by: wangor | (20) September 10, 2007 03:00 PM
Posted by: 和谐C | (19) September 10, 2007 01:47 PM
Posted by: Rome | (18) September 10, 2007 01:39 PM
Posted by: analyst | (17) September 10, 2007 01:08 PM
Posted by: nothanks | (16) September 10, 2007 11:17 AM
Posted by: chai | (15) September 10, 2007 11:11 AM
Posted by: Cofyc | (14) September 10, 2007 11:08 AM
Posted by: zcpro | (13) September 10, 2007 10:54 AM
Posted by: zcpro | (12) September 10, 2007 10:36 AM
Posted by: Zenberg | (11) September 10, 2007 10:05 AM
Posted by: neutralevil | (10) September 10, 2007 09:38 AM
Posted by: Linker M Lin | (9) September 10, 2007 09:28 AM
Posted by: redsea | (8) September 10, 2007 09:22 AM
Posted by: pi1ot | (7) September 10, 2007 09:15 AM
Posted by: Atry | (6) September 10, 2007 08:32 AM
Posted by: Atry | (5) September 10, 2007 08:30 AM
Posted by: Atry | (4) September 10, 2007 08:25 AM
Posted by: Atry | (3) September 10, 2007 08:21 AM
Posted by: 安德尔斯 | (2) September 10, 2007 07:40 AM
Posted by: roy_hu | (1) September 10, 2007 04:24 AM