YouMinTW
    • 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 No publishing access yet

      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.

      Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Explore these features while you wait
      Complete general settings
      Bookmark and like published notes
      Write a few more notes
      Complete general settings
      Write a few more notes
      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 New
    • Engagement control
    • Make a copy
    • 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 Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy 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 No publishing access yet

    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.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    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
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # 重構 - Chapter 10 簡化條件邏輯 [TOC] ## 1. 分解條件邏輯 (Decompose Conditional) **動機:** 凸顯意圖、降低閱讀複雜度 **作法:** 將分支中的條件邏輯抽成 函式 (Extract Function) From ```javascript! if (!aDate.isBefore(plan.summerStart) && !aDate.isAfter(plan.summerEnd)) charge = quantity * plan.summerRate; else charge = quantity * plan.regularRate + plan.regularServiceCharge; ``` To ```javascript! if (summer()) charge = summerCharge(); else charge = regularCharge(); ``` To ```javascript! const charge = summer() ? summerCharge() : regularCharge() ``` ## 2. 合併條件式(Consolidate Conditional Expression) **動機:** 當檢查不同、底下卻做相同的事時,代表 1. 檢查可結合、 2. 可抽出函式 (Extract Function)凸顯意圖 (封裝、隱藏細節,從說 how 變成說 why) **作法:** 1. 確保條件式中沒 Side Effect (否則請先執行 Separate Query from Modifier) 2. 將兩個條件式中,用邏輯運算子結合 (or -> 依序、 and -> 巢狀) 3. 測試 4. 將結合完的這個條件式與下個條件結合,直到只剩一個條件式 5. 考慮將此條件式抽成 Function From ```javascript! function disabilityAmount (anEmployee) { if (anEmployee.seniority < 2) return 0; if (anEmployee .monthsDisabled > 12) return 0; if (anEmployee.isPartTime) return 0; // 計算 disability amount ... } ``` To ```javascript! function disabilityAmount (anEmployee) { if (isNotEligableForDisability()) return 0; //計算 disability anount ... function isNotEligableForDisability() { return ((anEmployee.seniority < 2) || (anEmployee.monthsDisabled > 12) || (anEmployee.isPartTime)); } } ``` From ```javascript! if(anEmployee.onVacation) if (anEmployee.seniority > 10) return 1; return 0.5; ``` To ```javascript! if ((anEmployee.onVacation) && (anEmployee.seniority > 10)) return 1; return 0.5; ``` ## 3. 將巢狀條件式換成防衛敘句(Replace Nested Conditional with Guard Clauses) **動機:** 使入口和出口更“清晰”! 防衛敘句能指出某情況非功能“核心”,發生了就採取行動離開。有意識到思考你的條件分支是屬於正常 or 異常,(2組都正常,用 if/else <= 權重相同;有一組為異常 <= 提早離開) **作法:** 1. 先將最外層需要替換的條件轉為 Guard Clauses 2. 測試 3. 重複該行為 4. 最後如果 Guard Clauses 都回傳相同結果,可使用二:合併條件式 From ```javascript! function getPayAmount () { let result; if (isDead) result = deadAmount(); else { if (isSeparated) result = separatedAmount(); else { if (isRetired) result = retiredAmount(); else result = normalayAmount(); } } return result; } ``` To ```javascript! function getPayAmount () { if (isDead) return deadAmount() ; if (isSeparated) return separatedAmount(); if (isRetired) return retiredAmount(); return normalPayAmount (); } ``` From ```javascript!= function adjustedCapital (anInstrument) { let result = 0; if (anInstrument.capital > 0) { if (anInstrument.interestRate > 0 && anInstrument.duration > 0){ result = (anInstrument.income / anInstrument.duration) * anInstrument.adjustmentFactor; } } return result; } // 條件反轉、簡化過程 + if (anInstrument.capital <= 0) return result + if (!(anInstrument.interestRate > 0 && anInstrument.duration > 0)) return result + if (anInstrument.interestRate <= 0 || anInstrument.duration <= 0) return result + 合併 12,14 行 + 移除多餘的變數 result ``` 註解:狄摩根定律 `(!a) && (!b) === !(a || b)` 和 `(!a) || (!b) === !(a && b)` ![](https://pic2.zhimg.com/50/v2-88bfea40e6a7dbc88b52dfb13d097062_720w.jpg?source=1940ef5c) To ```javascript! function adjustedCapital (anInstrument) { if ( anInstrument.capital <= 0 || anInstrument.interestRate <= 0 || anInstrument.duration<= 0 ) return 0; return (anInstrument.income / anInstrument .duration) * anInstrument .adjustmentFactor ``` ## 4. ~~將條件式換成多型(Replace Conditional with Polymorphism)~~ ## 5. ~~加入特例(Introduce Special Case)~~ ## 6. 加入斷言(Introduce Assertion) **動機:** 凸顯意圖、寶貴的溝通工具、除錯的好幫手 **作法:** 當你見到一個條件為真的狀況時,加入斷言來說明此事 From ```javascript! if(this.discountRate) base = base - (this.discountRate * base) ``` To ```javascript! assert(this.discountRate >= 0) // 斷言失敗代表程式碼有錯 if(this.discountRate) base = base - (this.discountRate * base) // assert 大致會長得像下面這樣,nodejs 有提供 API function assert(condition, message) { if (!condition) { throw new Error(message || "Assertion failed"); } } ``` ## Discucsion 1. 盡量使用正向表述,人腦比較好理解 ```javascript! disabled = !isEligible || !isActive disabled = !(isEligible && isActive) enabled = isEligible && isActive ``` 2. 在單元測試、TypeScript 型別斷言都還滿常見 assert 的概念 ```javascript! expect(add(5, 5)).toBe(10); ``` ```typescript let str: unknown = "geeksforgeeks"; let len: number = (str as string).length; ``` 3. IIFE 變數,來替代巢狀三元運算式 ```javascript const content = (() => { switch (status) { case 'loading': return <LoadingBar />; case 'loaded': return <Table />; default: return null; } })(); return ( <Container> {content} </Container> ``` 4. 用 switch case true 來做多重判斷 https://seanbarry.dev/posts/switch-true-pattern ```javascript const user = { firstName: "Seán", lastName: "Barry", email: "my.address@email.com", number: "00447123456789", }; switch (true) { case !user: throw new Error("User must be defined."); case !user.firstName: throw new Error("User's first name must be defined"); case typeof user.firstName !== "string": throw new Error("User's first name must be a string"); // ...lots more validation here default: return user; } ``` 5. 用 object literal 取代 switch case https://ultimatecourses.com/blog/deprecating-the-switch-statement-for-object-literals ```javascript! // from var drinks; switch(type) { case 'coke': drink = 'Coke'; break; case 'pepsi': drink = 'Pepsi'; break; default: drink = 'Unknown drink!'; } // to function getDrink (type) { var drinks = { 'coke': 'Coke', 'pepsi': 'Pepsi', 'lemonade': 'Lemonade', 'default': 'Default item' }; return 'The drink I chose was ' + (drinks[type] || drinks['default']); } ``` ## 參考資料 1. 重構:改善既有程式的設計 1. [Code の 斷捨離 — 重構 (Refactoring)-ch9](https://medium.com/@duidae/code-%E3%81%AE-%E6%96%B7%E6%8D%A8%E9%9B%A2-%E9%87%8D%E6%A7%8B-refactoring-ch9-6768dae8a6ba) 2. [What is “assert” in JavaScript?](https://stackoverflow.com/questions/15313418/what-is-assert-in-javascript#:~:text=There%20is%20no%20assert%20in%20JavaScript.%20However%2C%20there,line%20argument%20-enableassertions%20%28or%20its%20shorthand%20-ea%20%29%2C)

    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
    Sign in via Google Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    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