Thursday, March 07, 2013

那些年,我們一起追的創業。

對於想創業的人,我只想送他幾句話:

1.「千萬不要創業」
如果你沒有那個guts準備賠個幾百萬幾千萬,拜託你不要創業。創業那有不把手弄髒的,風險遠比你想像的高。你除了會賠錢,還會失去幾個朋友,創造一些敵人,很晚才結婚,別人開雙B你還在騎機車;然後,運氣好一點,你也許有機會改變一點點的世界。

2.「Don't follow your passion, follow your efforts!」
對於還沒找到自己的「熱情」的人,千萬不要去找你的熱情,因為熱情不是找來的,而是點點滴滴的努力累積而來的。唯有你專精於某個領域,某項技能,才能創造自信,也才會有熱情。

3. 「Only the craziest idea worths your lifetime journey」
的確,不是人人都是Steve Jobs,但每個人都有選擇自己體驗人生的權力,你可以想要成為如同Jobs這麼有感染力的人,也可以選擇成為Aaron Swartz改變世界。

4. 「時勢造英雄,還是英雄造時勢?」 
我認為是英雄造時勢,雖然我們都還不是英雄,但是如果不試著造勢,那怎麼還會有英雄呢?

5.「People who are crazy enough 
to think they can change the world 
are the ones who do」
把你的夢想訂的大一點,高一點,瘋狂一點,不是件壞事。因為唯有你把自己訂的跟天一樣高,但卻又腳踏實力的執行,你才能跟別人不同。

最後,Jeff是我2006 YEF一起出國的隊友,這雖然是黑莓機的廣告,但是我覺得很感人。

 6. 「台灣這個地方太容易告訴年輕人說,不可能」 

沒有什麼不可能的,你的生命也還沒有黃昏,不要等到時間用完了才開始悔恨。

翻滾吧! 你的第二人生。

Wednesday, March 07, 2012

程式設計師的自我修練

寫程式很簡單。

但寫好程式就沒那麼簡單了。

我覺得要學會寫好程式是一條漫長、需要耐心的路。
還記得我大二的時候去的那家遊戲公司,
裡頭有位寫3D引擎寫了好多年的前輩,我們都叫他葉老大。
他沒有顯赫的學歷,但就是一個很愛寫程式、玩遊戲、跟cosplay的傢伙。

他寫程式不算快,但是非常細心,而且絕大多數的程式碼都十分整齊,
歐對了,整齊不是說等號有對齊、變數有對齊就叫整齊,
而是整個程式的架構非常的乾淨、對稱、一致,
如果用我現在的角度來看,我會說這真的是充滿了美感,
但是小大二那裡懂得什麼叫美什麼叫不美,只知道這是一套完全沒有說明文件的3D引擎,只好硬看硬學。
但沒過多久,很快的,我就上手了。他的介面真的很簡單而且好用,
我覺得拿現在OGRE3D來比都不會遜色到那裡去。
當然很可惜的這年頭做3D引擎已經不是一般遊戲公司會嘗試的事情了,現成的東西太多了...

在那段時間,與其說我學會用這套3D引擎,我覺得我學的更多的是設計。
我了解到一個引擎在設計上的哲學,程式介面定義上怎樣叫做乾淨,要注意對稱性跟一致性等等。
這對我後來的工作,不管是寫程式技巧的提升,還是架構設計,都有很大的影響。

我前面提到寫程式是門哲學,或者我會稱他是門藝術;
當然有人說他是工程,是要用工程手法去規範、去設計;
但我認為其實任何一門工程,學到最後,其實不是什麼東西都有答案的,更多的是經驗法則、慣例。
而這些經驗法則或慣例,也都是前輩們嘔心瀝血、在錯誤中學習後才得來的。

所以其實很多時候老工程師有很多有的沒的堅持。不要小看這些堅持,大部份一定是有道理的。
(當然有些不學新東西的老工程師除外)

所以回到標題,程式設計師要怎麼自我修練?

第一,我覺得最重要的是心態。
盡量多從別人的程式碼去學習,原則上沒有所謂"最好"的寫法,但是會有一些common practice。
不要第一時間就抗拒這些東西,而自己閉門造車,很難進步。
另外,每個引擎、每個框架、每套語言,都有他的邏輯跟哲理(就像中文跟英文跟西班牙文)。
寫什麼語言就要用那個語言的邏輯來思考,用他的哲理來解決問題。

第二,多寫多讀。
每一項工藝都是需要花時間練習的。這種東西沒什麼捷徑,只有多寫多錯多學習。
寫多了、看多了自然就會知道什麼叫好、什麼叫不好。

第三,寫程式速度的提升
寫程式的速度沒什麼好說的,就是打字要夠快、編輯器要好用。
什麼是好用的編輯器,這當然依語言而有差別啦,
當然有人會推用vim or emacs一招打天下,
我其實也不反對,畢竟真的蠻多優點的又是到處都可以跑(我最近也在學emacs...XD)
但我現在寫C++還是用Eclipse為主啦,畢竟Eclipse CDT真的做的很好,
比vim or emacs上的semantic completion好太多了,
加上refactor的功能也很好用,你也可以在eclipse掛vim or emacs的套件。
(雖然很吃記憶體,但反正記憶體很便宜...)

所以不管你是用什麼,如果你用了好一段時間,
但還是比不上跟你打字速度差不多的人用其它編輯器的速度,
那麼問題要嘛是你不夠認真學這個編輯器,要嘛就是這個編輯器沒辦法滿足你的需求。

第四,思考的速度
思考的速度我覺得是最難提升的。但跳躍性思考對於寫程式的人卻是最重要的技能之一。
這個需要日積月累、在平日就要練習的;
我覺得最快的方式是跟一群聰明的人在一起工作、一起討論。
當大家思考速度都很快的時候,你自然也會變快一些。

除了思考的速度之外,思考的密度也很重要;也就是說,除了求快之外,也要求面面俱到。
我覺得這個就跟右腦的跳躍性的思考模式有很大的關係。

這個練習的方式就是當大家在一起brainstorming的時候,某人提出你沒想到的點,
你不只能是說:「啊對」,你應該要想的是:「我怎麼沒想到」
要告訴自己的大腦說:「我應該要先想到這一點」才對。
久了他自然就會變聰明了。
相信我,這不是唬爛的,右腦是平行的,你要提升他的平行度,讓右腦主動"告訴"你問題在那裡。

這種能力也是除錯時常用到。像我大部份的時候還沒看GDB就知道問題在那,
其實很多時候都是被主動"告知"的。

第五,架構的哲學
其實你看很多design pattern,我覺得那只是一種程式設計師溝通的語言。
我自己覺得很多架構的設計,都是跟大自然或者這個社會同構的。
責任歸屬、權力分配,這些都是架構設計第一個要思考的問題,其次就是命名(古人說的嘛,名正才言順),
最後才是實作細節;但這不代表實作細節不重要,實作細節在架構設計的初期其實也是非常重要,
你可以思考的愈深入,這個架構愈不容易因為實作的變化而需修正。
所以你不能只看到表面的問題,只考慮表面的合諧。

第六,深入底層
程式設計師是個解決問題的人。
你愈了解底層的實作,就愈能用有效率的方法解決上層的問題。
所以作業系統、程式語言、組語、計算機架構這些科目我覺得太重要了。
當然這些課其實通常在學校有很大一部份是過時的,但他的基礎概念不能沒有;
其實最近真的去實作一個程式語言之才發現自己以前對程式語言或編譯器設計的無知,
才真的了解很多語言的設計的道理所在。

另外學習系統管理我覺得也是一個好的程式設計師可以挑戰的技能。
大多數人不喜歡學系統管理,不喜歡打指令,不喜歡處理這麼煩雜又不一致一東西,
但是我認為這就是一種訓練,訓練你如何在一片混亂中,找到規則。
很多的開放原始碼專案裡,連個文件都沒有,
如果你能快速的在一堆程式碼中找到他的邏輯、了解他的哲理,
那麼我想你的能力應該可以超越85%的程式設計師了。

所以我也很推薦大家去讀沒有文件的程式碼,練習trace code的能力。
像是如果是寫C++的話,我推薦大家去trace一些boost的原始碼;
像是mpl、fusion、tuple、function/bind、graph等等,其實只要花點耐心,我覺得都可以看懂的。
而且在你trace code的過程中,會學到很多別人的技巧。簡而言之,框架的底層都是很精彩的。
對了,除了學會技巧之外,最好同時也要學會他們的哲學;
千萬不要只會看sample code,學會怎麼用就了事了。

第七,相信自己、相信直覺
很多時候你要相信自己現在寫這段code是對的,
就像要相信自己現在下的這個command會有你預期的結果,
這樣你才能在解決現在的問題時,先去思考接下來的問題。

舉例來說,所以如果一個系統管理員打一個bash指令,
要去看他的結果你才能決定你的下一步要做什麼,那就太慢了;
應該要跟CPU一樣,要有speculative execution,
在打這個指令的同時你已經想好接下來要做那些事情了。

寫程式也一樣,你在寫這一行的時候,你打字速度如果跟不上你的思考速度,
你就要練習用50%的腦去完成現在要做的事,剩下50%去思考接下來要做的事。
(就像你邊開車也是可以同時跟旁邊的正妹聊天啊XD)

第八,程式碼的掌握度
很多人寫程式的習慣是每次寫一點,然後慢慢的測試再測試,最後達到原本的目標;
這也就是很多人說的小步開發。

我的習慣比較不一樣,我不喜歡每次寫一點寫一點,
我喜歡一次把所有東西寫完,然後跑幾次GDB除錯。
這可以稱的上是大步開發。

這點爭議性比較大,有人認為這樣寫有可能漏東漏西,少考慮一些條件等等的,

但我認為這個跟你一次寫多少無關,而是跟你對這份程式碼的掌握度有關;
簡單說,如果你對你的程式碼掌握度夠高,就跟成年人走路一樣,
那這小步開發裡的每一步難道還要學小baby似的又走又爬嗎?

而當你有能力勇敢的跨大步的時候,你邊寫也要邊在腦袋裡跑這段程式碼,
考慮各種可能的情況(同樣的,要讓右腦告訴你),
最後跑GDB測試就好了。

大步開發這只適用在對程式碼的掌握度夠高的時候。
如果你第一次用某個複雜的library像是boost graph,
或是寫了一些複雜的template metaprogramming的程式碼,
或是做嵌入式系統,或是寫些比較小但很複雜的程式,那還是小步小步的開發比較適合。

以上是我這麼多年來寫程式的心得。
希望大家還能多自我修練,互相學習,寫出好的程式。
這樣才不會白白浪費你程式設計師的人生啊~

Friday, October 07, 2011

Add All Subdirectories in CMake

有時候,某個目錄下有那些子目錄要用add_subdirectory加到build system裡是動態的(也就是不能寫死),像是我們用android的repo工具,projects目錄下要掛什麼sub-project完全就是看default.xml,那麼這個時候cmake的add_subdirectory就整個很沒用,所以就只好用regex硬幹...如果有人碰到一樣的問題可以用以下的方法:



file(GLOB all_valid_subdirs RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "*/CMakeLists.txt")

foreach(dir ${all_valid_subdirs})
message(STATUS "path = ${dir}")
if(${dir} MATCHES "^([^/]*)//CMakeLists.txt")
string(REGEX REPLACE "^([^/]*)//CMakeLists.txt" "\\1" dir_trimmed ${dir})
add_subdirectory(${dir_trimmed})
endif()
endforeach(dir)

Thursday, October 06, 2011

"You've got to find what you love" - Steve

In memorial of our great leader, Steve Jobs.

Full transcript of the Stanford Commencement 2005
(from: http://news.stanford.edu/news/2005/june15/jobs-061505.html

I am honored to be with you today at your commencement from one of the finest universities in the world. I never graduated from college. Truth be told, this is the closest I've ever gotten to a college graduation. Today I want to tell you three stories from my life. That's it. No big deal. Just three stories.

The first story is about connecting the dots.

I dropped out of Reed College after the first 6 months, but then stayed around as a drop-in for another 18 months or so before I really quit. So why did I drop out?

It started before I was born. My biological mother was a young, unwed college graduate student, and she decided to put me up for adoption. She felt very strongly that I should be adopted by college graduates, so everything was all set for me to be adopted at birth by a lawyer and his wife. Except that when I popped out they decided at the last minute that they really wanted a girl. So my parents, who were on a waiting list, got a call in the middle of the night asking: "We have an unexpected baby boy; do you want him?" They said: "Of course." My biological mother later found out that my mother had never graduated from college and that my father had never graduated from high school. She refused to sign the final adoption papers. She only relented a few months later when my parents promised that I would someday go to college.

And 17 years later I did go to college. But I naively chose a college that was almost as expensive as Stanford, and all of my working-class parents' savings were being spent on my college tuition. After six months, I couldn't see the value in it. I had no idea what I wanted to do with my life and no idea how college was going to help me figure it out. And here I was spending all of the money my parents had saved their entire life. So I decided to drop out and trust that it would all work out OK. It was pretty scary at the time, but looking back it was one of the best decisions I ever made. The minute I dropped out I could stop taking the required classes that didn't interest me, and begin dropping in on the ones that looked interesting.

It wasn't all romantic. I didn't have a dorm room, so I slept on the floor in friends' rooms, I returned coke bottles for the 5¢ deposits to buy food with, and I would walk the 7 miles across town every Sunday night to get one good meal a week at the Hare Krishna temple. I loved it. And much of what I stumbled into by following my curiosity and intuition turned out to be priceless later on. Let me give you one example:

Reed College at that time offered perhaps the best calligraphy instruction in the country. Throughout the campus every poster, every label on every drawer, was beautifully hand calligraphed. Because I had dropped out and didn't have to take the normal classes, I decided to take a calligraphy class to learn how to do this. I learned about serif and san serif typefaces, about varying the amount of space between different letter combinations, about what makes great typography great. It was beautiful, historical, artistically subtle in a way that science can't capture, and I found it fascinating.

None of this had even a hope of any practical application in my life. But ten years later, when we were designing the first Macintosh computer, it all came back to me. And we designed it all into the Mac. It was the first computer with beautiful typography. If I had never dropped in on that single course in college, the Mac would have never had multiple typefaces or proportionally spaced fonts. And since Windows just copied the Mac, it's likely that no personal computer would have them. If I had never dropped out, I would have never dropped in on this calligraphy class, and personal computers might not have the wonderful typography that they do. Of course it was impossible to connect the dots looking forward when I was in college. But it was very, very clear looking backwards ten years later.

Again, you can't connect the dots looking forward; you can only connect them looking backwards. So you have to trust that the dots will somehow connect in your future. You have to trust in something — your gut, destiny, life, karma, whatever. This approach has never let me down, and it has made all the difference in my life.

My second story is about love and loss.

I was lucky — I found what I loved to do early in life. Woz and I started Apple in my parents garage when I was 20. We worked hard, and in 10 years Apple had grown from just the two of us in a garage into a $2 billion company with over 4000 employees. We had just released our finest creation — the Macintosh — a year earlier, and I had just turned 30. And then I got fired. How can you get fired from a company you started? Well, as Apple grew we hired someone who I thought was very talented to run the company with me, and for the first year or so things went well. But then our visions of the future began to diverge and eventually we had a falling out. When we did, our Board of Directors sided with him. So at 30 I was out. And very publicly out. What had been the focus of my entire adult life was gone, and it was devastating.

I really didn't know what to do for a few months. I felt that I had let the previous generation of entrepreneurs down - that I had dropped the baton as it was being passed to me. I met with David Packard and Bob Noyce and tried to apologize for screwing up so badly. I was a very public failure, and I even thought about running away from the valley. But something slowly began to dawn on me — I still loved what I did. The turn of events at Apple had not changed that one bit. I had been rejected, but I was still in love. And so I decided to start over.

I didn't see it then, but it turned out that getting fired from Apple was the best thing that could have ever happened to me. The heaviness of being successful was replaced by the lightness of being a beginner again, less sure about everything. It freed me to enter one of the most creative periods of my life.

During the next five years, I started a company named NeXT, another company named Pixar, and fell in love with an amazing woman who would become my wife. Pixar went on to create the worlds first computer animated feature film, Toy Story, and is now the most successful animation studio in the world. In a remarkable turn of events, Apple bought NeXT, I returned to Apple, and the technology we developed at NeXT is at the heart of Apple's current renaissance. And Laurene and I have a wonderful family together.

I'm pretty sure none of this would have happened if I hadn't been fired from Apple. It was awful tasting medicine, but I guess the patient needed it. Sometimes life hits you in the head with a brick. Don't lose faith. I'm convinced that the only thing that kept me going was that I loved what I did. You've got to find what you love. And that is as true for your work as it is for your lovers. Your work is going to fill a large part of your life, and the only way to be truly satisfied is to do what you believe is great work. And the only way to do great work is to love what you do. If you haven't found it yet, keep looking. Don't settle. As with all matters of the heart, you'll know when you find it. And, like any great relationship, it just gets better and better as the years roll on. So keep looking until you find it. Don't settle.

My third story is about death.

When I was 17, I read a quote that went something like: "If you live each day as if it was your last, someday you'll most certainly be right." It made an impression on me, and since then, for the past 33 years, I have looked in the mirror every morning and asked myself: "If today were the last day of my life, would I want to do what I am about to do today?" And whenever the answer has been "No" for too many days in a row, I know I need to change something.

Remembering that I'll be dead soon is the most important tool I've ever encountered to help me make the big choices in life. Because almost everything — all external expectations, all pride, all fear of embarrassment or failure - these things just fall away in the face of death, leaving only what is truly important. Remembering that you are going to die is the best way I know to avoid the trap of thinking you have something to lose. You are already naked. There is no reason not to follow your heart.

About a year ago I was diagnosed with cancer. I had a scan at 7:30 in the morning, and it clearly showed a tumor on my pancreas. I didn't even know what a pancreas was. The doctors told me this was almost certainly a type of cancer that is incurable, and that I should expect to live no longer than three to six months. My doctor advised me to go home and get my affairs in order, which is doctor's code for prepare to die. It means to try to tell your kids everything you thought you'd have the next 10 years to tell them in just a few months. It means to make sure everything is buttoned up so that it will be as easy as possible for your family. It means to say your goodbyes.

I lived with that diagnosis all day. Later that evening I had a biopsy, where they stuck an endoscope down my throat, through my stomach and into my intestines, put a needle into my pancreas and got a few cells from the tumor. I was sedated, but my wife, who was there, told me that when they viewed the cells under a microscope the doctors started crying because it turned out to be a very rare form of pancreatic cancer that is curable with surgery. I had the surgery and I'm fine now.

This was the closest I've been to facing death, and I hope it's the closest I get for a few more decades. Having lived through it, I can now say this to you with a bit more certainty than when death was a useful but purely intellectual concept:

No one wants to die. Even people who want to go to heaven don't want to die to get there. And yet death is the destination we all share. No one has ever escaped it. And that is as it should be, because Death is very likely the single best invention of Life. It is Life's change agent. It clears out the old to make way for the new. Right now the new is you, but someday not too long from now, you will gradually become the old and be cleared away. Sorry to be so dramatic, but it is quite true.

Your time is limited, so don't waste it living someone else's life. Don't be trapped by dogma — which is living with the results of other people's thinking. Don't let the noise of others' opinions drown out your own inner voice. And most important, have the courage to follow your heart and intuition. They somehow already know what you truly want to become. Everything else is secondary.

When I was young, there was an amazing publication called The Whole Earth Catalog, which was one of the bibles of my generation. It was created by a fellow named Stewart Brand not far from here in Menlo Park, and he brought it to life with his poetic touch. This was in the late 1960's, before personal computers and desktop publishing, so it was all made with typewriters, scissors, and polaroid cameras. It was sort of like Google in paperback form, 35 years before Google came along: it was idealistic, and overflowing with neat tools and great notions.

Stewart and his team put out several issues of The Whole Earth Catalog, and then when it had run its course, they put out a final issue. It was the mid-1970s, and I was your age. On the back cover of their final issue was a photograph of an early morning country road, the kind you might find yourself hitchhiking on if you were so adventurous. Beneath it were the words: "Stay Hungry. Stay Foolish." It was their farewell message as they signed off. Stay Hungry. Stay Foolish. And I have always wished that for myself. And now, as you graduate to begin anew, I wish that for you.

Stay Hungry. Stay Foolish.

Thank you all very much.