跳到主要內容

[心得] 復古、老派的程式設計之路

最近看到 John Carmack 2018 年的貼文中的幾段話:

I’m not a Unix geek.  I get around ok, but I am most comfortable developing in Visual Studio on Windows.  I thought a week of full immersion work in the old school Unix style would be interesting, even if it meant working at a slower pace.  It was sort of an adventure in retro computing — this was fvwm and vi.  Not vim, actual BSD vi.

In the end, I didn’t really explore the system all that much, with 95% of my time in just the basic vi / make / gdb operations.  I appreciated the good man pages, as I tried to do everything within the self contained system, without resorting to internet searches.  Seeing references to 30+ year old things like Tektronix terminals was amusing.

In the spirit of my retro theme, I had printed out several of Yann LeCun’s old papers and was considering doing everything completely off line, as if I was actually in a mountain cabin somewhere, but I wound up watching a lot of the Stanford CS231N lectures on YouTube, and found them really valuable.  Watching lecture videos is something that I very rarely do — it is normally hard for me to feel the time is justified, but on retreat it was great!

I used a common pattern for me: get first results with hacky code, then write a brand new and clean implementation with the lessons learned, so they both exist and can be cross checked.

心得:
我覺得我這陣子想做的,跟 John Carmack 很像,在老舊的思維跟環境裡開發一些老舊或是被遺忘的演算法,並非捨棄新的技術,而是我想用以前那些優秀的程式設計師的想法,去重新思考一些專案。讓這些舊方法再次復興起來。
如同近代物理學中的弦論描述粒子行為的方程式,早在 18 世紀的歐拉就已經提出。
我想從舊的時代,去重新觀看新時代的面貌。因為許多舊的方法與系統哲學,似乎被遺忘的差不多了,但我相信這些傳統的東西有它的價值,而且我一直相信以前的人在某方面而言,比現在聰明。

當我謙虛地再次學習過這些復古的知識後,或許能彌補我的不足,也彌補我思考上的死角,也許能提昇我的思維速度。

我一直很認同貝爾實驗室發明 Unix 的工程師,他們的程式設計哲學。我將這些哲學再次引述於此:

  1. Make each program do one thing well. To do a new job, build afresh rather than complicate old programs by adding new "features".

  2. Expect the output of every program to become the input to another, as yet unknown, program. Don't clutter output with extraneous information. Avoid stringently columnar or binary input formats. Don't insist on interactive input.

  3. Design and build software, even operating systems, to be tried early, ideally within weeks. Don't hesitate to throw away the clumsy parts and rebuild them.

  4. Use tools in preference to unskilled help to lighten a programming task, even if you have to detour to build the tools and expect to throw some of them out after you've finished using them.

我一直想要在有限的時間,將能力再提昇一點點。
我想是時候複習老派跟復古的知識了!


留言

這個網誌中的熱門文章

[程式競賽] UVa 572, Oil Deposits,Flood Fill 演算法

原題目簡述如下: 以 m x n 大小的 grid 代表一張地圖,現今要在此地圖內探勘,找出油田。某一區塊如果標示 "@" 代表有油,"*" 代表沒有油。 "@" 相鄰的區域的聯集,可視為一個油田。(所謂相鄰,除了上下左右,斜向的相鄰也算進去) 求任意地圖中,油田的個數。 例如輸入的測資為: *    *   *    *  @ *   @  @  *  @ *   @   *   *  @ @ @  @   * @ @ @   *   *  @ 則油田個數為 2。 想法 採用典型的倒水演算法(Flood Fill),走訪 "@" 出現的區域,從此往下倒水,倒過水的區域標上 id,因此透過 id 的編號,可以得知油田的個數。 實作 先實作倒水演算法的子函式: void floodfill(vector<vector<char> >& map,                vector<vector<int>  >& id_table,                int row, int col, int id) {    if(row < 0 || (row >= map.size()) )   return;    if(col < 0 || (col >= map[0].size())) return;    if(map[row][col] != '@' || id_table[row][col] > 0) return;    id_table[row][col] = id;    floodfill(map, id_table, row-1, col-1, id);    floodfill(map, id_table, row-1, col,   id);    floodfill(map, id_table, row-1, col+1, id);    floodfill(map, id_table, row,   col-1, id);    floodfill(map, id_table, row,   col+1,

[Python] print 同時輸出到 file 和 console

在 Python 撰寫程式時,我們會希望螢幕 stdout 輸出可以同時記錄到 log 檔案裡。 但是螢幕輸出可能含有 ASCII escape codes 的顏色資訊,輸出的 log 檔案會有類似 ^[[01;32m 這種字樣出現。 我採用比較簡單的解法: 先將 print 函式輸出的訊息,同時導向到螢幕,同時儲存在指定的 log.txt 檔案中。 再用 sed 指令,將 log.txt 內的 ASCII escape code 清除。 方法如下: import sys class PrintLog(object): def __init__(self): self.console = sys.stdout self.log_file = open("log.txt", "w") def write(self, msg): self.console.write(msg) self.log_file.write(msg) def flush(self): pass def main(): original_stdout = sys.stdout sys.stdout = PrintLog() print " This is a testing message." sys.stdout = original_stdout if(__name__ == "__main__"): main() 也就是將 sys.stdout 指向自定義的 PrintLog class,讓 PrintLog 來處理輸出文字,用完 PrintLog 後再把 sys.stdout 導向回原本的 stdout。 接著使用 sed 指令刪除 log.txt 的 ASCII escape code: sed -i 's/\x1b[^m]*m//g' ./log.txt 上面的正規,\x 後面用來接一個 16 進位 ASCII 編碼,其中 1b 代表的是 ESC 退出鍵。 到此即可獲得沒有顏色編碼的 log.tx

[Python] 單引號,雙引號和三引號

在此解釋各種引號的意義。 雙引號跟單引號,在 Python 中的基本上沒差別,都可以代表字串: "This is a string" 'This is a string' 並且雙引號內可包含單引號,反之,如果用的是單引號,則單引號內可包含雙引號: "We call it 'Dog'...... " 'We call it "Dog"...... ' 三個雙引號,就可以直接輸入有換行的字串: """haha, this is a dog.""" 三個單引號要換行,就要輸入"\": '''haha, \ this is a dog.'''