BK_0710
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    --- title: Clean code tags: Study group description: View the slide with "Slide Mode". slideOptions: # 簡報相關的設定 theme: league # 顏色主題 transition: 'fade' # 換頁動畫 --- ## CH9 Unit Test --- ### 測試程式跟產品程式一樣重要 test code 亂 → 越難修改 → 花更多時間在寫新 test code → 舊測試碼開始沒辦法過 → 放棄測試 → 容易出錯 --- TDD(Test Driven Developement) 的三大法則 👉 步驟 1:在撰寫一個單元測試時,不可撰寫任何產品程式 👉 步驟 2:只撰寫剛好無法通過的單元測試,不能編譯也算無法通過 👉 步驟 3:只撰寫剛好能通過當前測試失敗的程式 ```c int Add(int firstNumber, int secondNumber){ return firstNumber + secondNumber; } void AddTest(){ int firstNumber = 1; int secondNumber = 2; int expected = 3; int actual = Add(firstNumber, secondNumber); Assert.AreEqual(expected, actual); } ``` --- - 一個函式只能做一件事 - Unit Test 也是一種函式 - 讓單元測試只包含一個概念 → 減少 assert ---- ```cpp void CalTest(){ int firstNumber = 1; int secondNumber = 2; int expected = 3; int actual = Add(firstNumber, secondNumber); Assert.AreEqual(expected, actual); firstNumber = 3; secondNumber = 2; expected = 1; int actual = Sub(firstNumber, secondNumber); Assert.AreEqual(expected, actual); } ``` ---- ```cpp void AddTest(){ int firstNumber = 1; int secondNumber = 2; int expected = 3; int actual = Add(firstNumber, secondNumber); Assert.AreEqual(expected, actual); } void SubTest(){ int firstNumber = 3; int secondNumber = 2; int expected = 1; int actual = Sub(firstNumber, secondNumber); Assert.AreEqual(expected, actual); } ``` --- ## F.I.R.S.T Fast(快速): Test 的執行速度越快越好,但可讀性最重要! Independent(獨立): 測試程式不應該互相依賴,這樣子一個 unit test fail 了才不用還要去檢查其他的 unit test Repeatable(可重複性): 測試程式應該可以在任何環境中執行 ---- Self-Validating(自我驗證): 測試程式應該輸出**Boolean**值,不管測試成功或失敗,都不應該還要自己在那邊看 log Timely(及時地): 撰寫測試程式要及時,單元測試要恰好在使其通過的產品程式之前撰寫(TDD概念)。 --- ## CH10 Class ---- class 的結構 → `stepdown rule` ```java public class dog(){ public static variable private static variable public variable private variable public function private function } ``` --- ## 簡短有力 像是函式一樣 用 25 個內的單字描述他,不用 if else or but 等詞彙 - 每個 class 有單一功能,且該功能應該由這個 class 完全封裝起來。 - 與其他少數幾個 class 合作來完成系統要求的行為 --- ## 單一職責原則 ### (Single Responsibility Principle, SRP) Responsibility is *one reason to change* > A class should have only one reason to change. ---- ```clike // Modem interface Modem { // 撥號 public function dial($pno); // 掛斷 public function hangup(); // 發送資料 public function send($c); // 接收資料 public function recv(); } ``` ---- ```clike interface Connection { // 撥號 public function dial($pno); // 掛斷 public function hangup(); } interface DataChannel { // 發送資料 public function send($c); // 接收資料 public function recv(); } ``` ---- ## 凝聚性 (cohesion) class 只有少量的實體變數,盡量讓所有 function 都用到這些變數,如果發現凝聚性低,就要考慮拆解 class ---- 將 class 大函式分解成小函式 → 靠實體變數避免參數傳遞 → 越來越多實體變數 →少數函式用到一個實體變數 → 他們應該自成一個 class ---- ### 優點 - 可讀性與可維護性提升 單一類別的複雜度降低,因為要實現的職責都很清晰明確的定義,這將大幅提升可讀性與可維護性 - More rubust 如果有做好 SRP ,那修改只會對同一個介面或類別有影響,這對擴展性和維護性都有很大的幫助。 --- ## 開放封閉原則 ### (The Open/Closed Principle, OCP) >Software entities (class, modules, functions, etc.) should be open for extension, but closed for modification. 一個軟體製品在面對擴展時是開放的,且擴充時不應修改到原有的程式。 Ex: Linux Kernel, Chrome extension ---- 怎麼不改 code ,又改變行為? 善用 abstract interface! ---- ```clike public function getData() { // 下載資料 // 載入 XML // 解析 XML return data; } ``` ---- ```clike public function getData() { // 下載資料 if(XML){ // 載入 XML // 解析 XML } else if(JSON){ // 載入 JSON // 解析 JSON } return data; } ``` ---- ```cpp abstract class DataResource { public function getData() { // 下載資料 // ... $content = curl_exec($ch); $data = $this->parse($content); return $data; } abstract protected function parse($content); } class XmlResource extends DataResource { protected function parse($content) { // 載入 XML // 解析 XML return $data; } } class JsonResource extends DataResource { protected function parse($content) { // 解析 JSON $data = json_decode($content); // ... return $data; } } ``` ```cpp ``` --- ref https://ithelp.ithome.com.tw/articles/10192105 https://ithelp.ithome.com.tw/articles/10191955

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully