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



note for "The Art of Readable Code" - CH11 One Task at a Time


Code should be organized so that it’s doing only one task at a time.舉個投票例子,投UP則+1,Down則-1,結果為所有投票總和,如果依據該rule,則code應該是
var vote_changed = function (vote) {
    var score = get_score();
    score += vote_value(vote);
    set_score(score);
};

基本上這個章節的概念跟前一章節差不多"將子問題抽離,讓function專注在處理問題本身上面"。其餘細節就不贅述了。

    參考資料:
  • The Art of Readable Code



2018年7月28日 星期六

note for "The Art of Readable Code" - CH10 Extracting Unrelated Subproblems


將子問題抽離,讓function專注在處理問題本身上面,首先要先了解這個function的"目的",接著抽離與"目的"無關的子問題,將這些子問題一一寫成獨立的function,如:(簡單說就主程式專注在邏輯上,細節部分就一一寫成sub function處理)
// Return which element of 'array' is closest to the given latitude/longitude.
// Models the Earth as a perfect sphere.
var findClosestLocation = function (lat, lng, array) {
    var closest;
    var closest_dist = Number.MAX_VALUE;
    for (var i = 0; i < array.length; i += 1) {
        // Convert both points to radians.
        var lat_rad = radians(lat);
        var lng_rad = radians(lng);
        var lat2_rad = radians(array[i].latitude);
        var lng2_rad = radians(array[i].longitude);
        // Use the "Spherical Law of Cosines" formula.
        var dist = Math.acos(Math.sin(lat_rad) * Math.sin(lat2_rad) +
                             Math.cos(lat_rad) * Math.cos(lat2_rad) *
                             Math.cos(lng2_rad - lng_rad));
        if (dist < closest_dist) {
             closest = array[i];
             closest_dist = dist;
        }
    }
    return closest;
};

抽離後的code
var spherical_distance = function (lat1, lng1, lat2, lng2) {
    var lat1_rad = radians(lat1);
    var lng1_rad = radians(lng1);
    var lat2_rad = radians(lat2);
    var lng2_rad = radians(lng2);
    // Use the "Spherical Law of Cosines" formula.
    return Math.acos(Math.sin(lat1_rad) * Math.sin(lat2_rad) +
                     Math.cos(lat1_rad) * Math.cos(lat2_rad) *
};

var findClosestLocation = function (lat, lng, array) {
    var closest;
    var closest_dist = Number.MAX_VALUE;
    for (var i = 0; i < array.length; i += 1) {
        var dist = spherical_distance(lat, lng, array[i].latitude, array[i].longitude);
        if (dist < closest_dist) {
            closest = array[i];
            closest_dist = dist;
        }
    }
    return closest;
};
抽離這些子問題的另一個好處容易優化,比如有更好的方式去運算spherical_distance()或是變更findClosestLocation()的運算邏輯。建立通用的function也是一個很好也很重要的習慣,當你開發一個新的program時,就可以運用這些通用function,快速建立一個prototype。

Simplifying an Existing Interface & Reshaping an Interface to Your Needs

如果既有的API不好用,那就包裝他或改寫他吧,我在A Wrap for service/thread也包裝了一些API,讓自己易於開發,有必要也會進行Reshaping/Re-factor等等步驟。

    參考資料:
  • The Art of Readable Code



熱門文章