2018年7月29日 星期日

note for "The Art of Readable Code" - CH12 Turning Thoughts into Code


You do not really understand something unless you can explain it to your grandmother. 
— Albert Einstein

先用口語描述演算法後,再轉成程式碼,能讓programmer寫出更自然的code,也有助於找出可以分解的子問題。
 We are reading three row iterators in parallel.
 Whenever the rows' times don't line up, advance the rows so they do line up.
 Then print the aligned rows, and advance the rows again.
 Keep doing this until there are no more matching rows left.

上述口語轉成的code為
def PrintStockTransactions(): 
    stock_iter = ...
    price_iter = ... 
    num_shares_iter = ...

    while True:
        time = AdvanceToMatchingTime(stock_iter, price_iter, num_shares_iter) 
        if time is None:
            return
        # Print the aligned rows.
        print "@", time,
        print stock_iter.ticker_symbol,
        print price_iter.price,
        print num_shares_iter.number_of_shares
        stock_iter.NextRow()
        price_iter.NextRow()
        num_shares_iter.NextRow()


    參考資料:
  • The Art of Readable Code



沒有留言:

張貼留言

熱門文章