- 目標: 給未來的自己看懂
- 方法:
- First level: 外在 - naming, alignment
- Naming
- 使用精確的詞: e.g. search / load / …
- get/set 帶有 lightweight 的概念
- first/last 代表範圍內頭與尾的指標, begin/end 來表示範圍外的指標
- 避免 tmp, retval 等沒有解釋性的參數名詞
- Alignment
- 用 function / macro 等來對齊程式
- Group 不同目的的 function lists
- Constructors / deconstructors
- Getter / setters
- helper functions
- 使用段落來註解程式
- // 1. register database
- // 2. init
- // 3. CRUD operations
- // 4. release resources
- Comments
- 移除沒有意義的註解, e.g. variable name has explained
- 留下設計理念與意圖的註解 (全局觀)
- 留下 limitation / FIXME / TODO
- 提供 example (UT)
- Second level: control flow
- loops
- 避免 do-while (except marco using do-while(0))
- 大多時候避免 goto function
- 最小化 embedded loops
- Expressions / variables
- 利用暫存的解釋變量減少長表達式, 但有時反過來要移除沒必要的中間變量
- 盡可能縮小 variable scope (避免 global variables)
- 善用 marcos 縮短表達式長度
- Third level: 模組化
- 善用 utility functions 來做到模組化
- 利用 wrapper function 簡化 interface
- 各 function 一次只做一件事
- 熟悉既有 libraries
- 善用 scripts 處理簡單工作, e.g. awk parse logs
- Others:
- Test-driven
- Sample refactoring flow: from naïve solution to complete solution
- 延伸閱讀
- "Code Complete: A Practical Handbook of Software Construction, 2Ed", Steve McConnell
- "Refactoring: Improving the Design of Existing Code", Martin Fowler
- "The Practice of Programming", Brian Kernighan and Rob Pike
- "Clean Code: A Handbook of Agile Software Craftsmanship", RobertC. Martin
- "Design Patterns: Elements of Reusable Object-Oriented Software", Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides
- "Programming Pearls, 2Ed", Jon Bentley
2021年3月21日 星期日
[Refactoring] 編寫可讀代碼 筆記
2021年3月12日 星期五
[Kid][Health] 禾馨眼科
- 費用: 掛號費(300)+部份負擔(50)+0-7歲自費視力檢查(300)
- 醫師: 王馨儀院長
- 詳細且認真解說
- 目前小孩視力會介於 0.6-1.0之間
- 天生散光
- 小孩量測時, 因為兒童自己視力的調節、專注力、表達方式都會影響視力報告結果
[Code Review] Check pointer not null
- C 語言有非常強大的 pointer, 但也因此惡名昭彰
- 兩個常用的情況是
- Null pointer check
if (ptr != NULL) {
// do something ...
} else {
// error handling
}
- 要用 if (ptr) 還是 if (ptr != NULL) 還是 if (NULL != ptr)
- 建議使用 if (ptr) 就好, 幾個理由如下
- If (ptr ==) 類型的 check, 可能會少寫一個 =, 變成 assignment, 造成難追的 bug
- NULL 的 implementation 未明確定義, 依不同 compiler 可能實作成 ((char *)0), ((void *) 0), 0, 0L 等
- 相容於 smart pointer return bool 的 check
- 降低不必要的 compare
- Reference: Checking for null pointer in c
- C++ 引入了 unique_ptr, shared_ptr, weak_ptr 等smart pointers
- Auto release resource at desstructor
2021年1月4日 星期一
[Programming] 重要的環境變數
- PATH
- 這應該不用多說, 執行時期搜尋的 directory path
- LD_LIBRARY_PATH
- dynamically load library search path
- RPATH
- run-time search path
- ORIGIN
- executable name
2021年1月3日 星期日
2020年12月30日 星期三
[cpplint] cpplint 設定
- What
- cpplint 是 Google 開發的一套 open source static code checker.
- 是一套 python tool.
- 主要 check 原則是依據 Google C++ Style Guide
- source repository: cpplint
- How
- install
pip install cpplint
- run
cpplint [option] files
- config file: CPPLINT.CFG, 可以在資料夾內新增此檔做設定, 裡面由 set noparent 這設定來指定是否獨立於其他設定檔
- options
- set noparent
- extensions=c, cpp, h
- 會被偵測到的 file extension
- exclude_files=regex
- root
- 設定 root directory, 這會影響 header file include的變數名稱
- e.g: #ifndef _PATH_FROM_ROOT_FILE_NAME_H_
- filter
- 可以透過--filter= (參數為空)來查看所有filters, 透過 filter=+filter1,-filter2 來 enable filter1, disable filter2
- build/c++11
- build/include
- 滿常 argue include header 須加入路徑, 個人是都用這把此 filter 關閉
- linelength=80
2020年12月28日 星期一
[Json] Quick reference
- Reader
- Writer
Json::Value vec(Json::arrayValue); vec.append(Json::Value(1)); vec.append(Json::Value(2)); vec.append(Json::Value(3)); root["sample"] = vec; std::cout << root; - library
2020年12月27日 星期日
[Programming] Book list
- Competitive Programming
- The Algorithm Design Manual
- Cracking the Coding Interview
訂閱:
文章 (Atom)