2021年3月12日 星期五

[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
    • Memory leak
      • 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

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
    • usage
      • run
      • cpplint [option] files
      • configuration
        • 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日 星期一

2020年12月27日 星期日

[Programming] Book list

  •  Competitive Programming
  • The Algorithm Design Manual
  • Cracking the Coding Interview

[Programming][C++] bits/stdc++.h

  • 比賽用的萬用 header 

[Management] 決策的開放性

  • Principles
    • 對於決策的方案, 應該全力投入.
      • 這是全公司同意後的策略, 應積極參與不扯後腿.
    • 對於決策方案的結果, 檢討應對事不對人.
      • 因為方案的決策是大家同意的結果, 實際上也是選擇而非對錯.
      • 對人檢討會造成各方勢力的形成, 也會拉長決策的時間, 以及降低參與決策的意願.
    • 對於決策的提案, 應該採取開放的態度, 但可以針對各提案的優缺點給予建議.
  • Practice
    • 小公司的決策模式會較遵循上述的方向, 因為沒有太多時間 argue 決策是正確或錯誤.
    • 極大公司具有研發團隊的公司也較能達到上述管理, 因為多角化的實驗, 成功與否如果不是基於產品落地來做評價的話, 也是能達到好的效果.
    • 一般大公司則容易陷入內部惡性競爭, 彼此爭取 KPI, 最簡單的方式就是讓人覺得別的 team 的決策是錯誤的. 但所謂的錯誤, 其實就是公司文化定義的產物.