很難想象鋼琴家不用聆聽大師的作品;詩人不用揣摩傳世的經典;畫家不用體會前輩的佳作;拳手不用參詳高人的示範。那我們怎麼能想象程式員不用仔細學習性感的代碼?可惜的是,美妙的代碼往往有如像Shrek,乍一看也就是面目醜陋的龐然大物。沒有Fionna的聰慧,我們也難欣賞Shrek洋蔥一般層次豐富的心靈。再說,代碼一旦寫成,我們看到的也就是一段神來之筆。再難體會到作者在難題前內心有如困獸般地衝撞,面臨多種選擇時精神的激蕩。我們也再難追溯每個資料結構背後的理念,每段演算法成型過程中每一步的由來(順便說一句。這也是為什麼Knuth的書引人入勝的原因。每段演算法怎麼從無到有,自粗而細,由慢轉快,通通脈絡清晰)。就算是理解代碼本身,想來每人的體會也有深有淺。不知道多少老大因為這些困難沒能體會到閱讀代碼時心頭腫脹(亂用馮唐語)的快感?除非,除非有高手引領我們入門,給我們細述經典代碼如何玲瓏浮屠,如何眼波婉轉。
IBM的Grady Booch也強力推薦程式員大量閱讀代碼,認為這是從新手到高手的必要手段。如果喜歡軟體開發老大還沒有訂閱Grady Booch的部落格的話,現在是時候了。G老大的私人項目Architecture Handbook想必更是每位對軟體架構有興趣的老大的必讀材料吧?他在這本公開的手冊將歸類整理曆史上各式架構。雖然這些工作開始還不到四年,但上面已經有不少高品質的資料。比如以前提到過的Eclipse架構考古。也許用G老大自己的話最能雄辯地道出軟體考古的意義:經典科學通過在定量觀察和理論構建間曼舞取得進展。前者細緻而刻意,後者富於創新且能經受檢驗。電腦科學充滿了經驗的觀察和理論的構造,但軟體世界裡,我們往往把所有時間用於搭建實物,卻疏於科學研究。我們有自己關於流程和工具的理論,但它們大部分都基於坊間傳聞和個人經驗,而不是基於反映了可靠經驗研究的確鑿且中立的資料(classical science advances via the dance between quantitative observation and theoretical construction." The former is deliberate and intentional; the latter is creative and testable. Computer science is full of empirical observation and the construction of theories, but in the world of software we often spend all of time building artifacts and not enough time doing science. We have our share of theories, about process and tools, but much of that work is based on anecdote and personal experience, not the hard, dispassionate data that reflects good empirical work) G老大的架構手冊有一欄read list,目前推薦的兩個條目都是代碼閱讀。一是C++的STL(設計,原始碼)。二是qmail(設計,原始碼)。今年的SIGCSE年會上,G老大做了主題演講。不,我沒去。我也是看別人的部落格寫的,現在就等Podcast出來了。裡面提到電腦系不僅要交給學生知識,也要讓學生領會“激情,美麗,快樂,和敬畏”,真是深得我心啊。在演講裡,G老大頻繁用“正確和高尚”來描述電腦業界眾人的努力,說從事軟硬體研發的工作既是特權也是義務。它是特權,因為我們從根本上多方面深刻地改變這個世界。基於同樣的原因,這也是我們的義務。我們應當牢記這點,並讓我們的學生同樣明白。不知道G老大和蜘蛛人有什麼瓜葛。
又跑題了。還是說回來。大家都熟悉的Code Reading: The Open Source Perspective是本不錯的入門書。不過作者著眼於零散的代碼,注重局部細節的實現(比如第三章第四章),很少分析一段完整程式:這段程式的動機是什嗎?解決了什麼好玩兒的問題?哪些地方體現了作者的天才?代碼的設計理念是什嗎?面臨選擇,怎麼做出取捨。。。
令人欣慰的是,人見人耐的老牌geek,資深出版人,Tim O’Reilly終於按耐不住出手了。今年6月,O’Reilly將推出新書Beautiful Code: Leading Programmers Explain How They Think。瞧這書名取的,多誘人啊。“Explain How They Think”,嘖嘖,這不引誘俺體驗一下當Craig Schwartz的經驗嗎?
再看看目錄,禁不住口水滴滴答答地流哈。Enough showed. Pre-ordered.
- Greg Wilson: Foreword
- Brian Kernighan: Beautiful Brevity: Rob Pike’s Regular Expression Matcher
- Karl Fogel: Subversion’s Delta Editor: Interface as Ontology
- Jon Bentley: The Most Beautiful Code I Never Wrote
- Tim Bray: Finding Things
- Elliotte Rusty Harold: Correct, Beautiful, Fast (In That Order): Lessons From Designing XML Validators
- Michael Feathers: The Framework for Integrated Test: Beauty through Fragility
- Alberto Savoia: Beautiful Tests
- Charles Petzold: On-the-Fly Code Generation for Image Processing
- Douglas Crockford: Top Down Operator Precedence
- Henry Warren: Accelerating Population Count
- Ashish Gulhati: Secure Communication: The Technology of Freedom
- Lincoln Stein: Growing Beautiful Code in BioPerl
- Jim Kent: The Design of the Gene Sorter
- Jack Dongarra and Piotr Luszczek: How Elegant Code Evolves With Hardware: The Case Of Gaussian Elimination
- Adam Kolawa: Beautiful Numerics
- Greg Kroah-Hartman: The Linux Kernel Driver Model
- Diomidis Spinellis: Another Level of Indirection
- Andrew Kuchling: An Examination of Python’s Dictionary Implementation
- Travis Oliphant: Multi-Dimensional Iterators in NumPy
- Ronald Mak: A Highly Reliable Enterprise System for NASA’s Mars Rover Mission
- Rogerio Atem de Carvalho and Rafael Monnerat: ERP5: Designing for Maximum Adaptability
- Bryan Cantrill: A Spoonful of Sewage
- Jeff Dean and Sanjay Ghemawat: Distributed Programming with MapReduce
- Simon Peyton Jones: Beautiful Concurrency
- Kent Dybvig: Syntactic Abstraction: The syntax-case expander
- William Otte and Doug Schmidt: Object-Oriented Patterns and a Framework for Networked Software
- Andrew Patzer: Integrating Business Partners the RESTful Way
- Andreas Zeller: Beautiful Debugging
- Yukihiro Matsumoto: Code That’s Like an Essay
- Arun Mehta: Designing Interfaces Under Extreme Constraints: the Stephen Hawking Editor
- TV Raman: Emacspeak: The Complete Audio Desktop
- Christopher Seiwald and Laura Wingerd: Code in Motion
- Brian Hayes: Writing Programs for “The Book”
- Andy Oram: Afterword