CoderClark
    • 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 New
    • 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 Note Insights Versions and GitHub Sync 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
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # B0001 易讀程式碼的特性:白話 ## 講白話吧! 一般而言,程式碼是否易讀沒有客觀標準定義,好比人們在判斷一篇文章是平易近人還是艱澀難懂,也沒有統一的客觀方式去決定。即使如此,我們還是可以依據是否滿足某些**特性**來大致判斷程式碼是否相對易讀。這些特性中,**「白話」** 是一個很重要的指標。 下面例子可以說明**白話**的程式碼是什麼概念。 ## 極度難以理解的程式碼 請先嘗試閱讀並理解下面這段程式碼。 ```c++= int getLight_1() { static int a = 0; static int b = 0; static int c = 0; if(a < 4) { ++a; return 1; } else if (b < 1) { ++b; return 2; } else if (c < 5) { ++c; return 3; } else { a = 1; b = 0; c = 0; return 1; } } ``` 這些程式碼每一行單獨看都是不難懂的,但是全部兜再一起就不知道想要做什麼事情。理解不了請不需要覺得挫折,因為 `getLight_1` 是幾乎無法被直接理解的。就算給了一個提示:**這是一個紅綠燈控制程式碼**,之後再回頭閱讀一次程式碼,也不一定能讀懂具體行為。 ## 稍微可以理解的程式碼 如果不要偷懶,多敲幾個字,不要用難以解讀的`a, b, c`作為變數名稱的話,就可以得到 `getLight_2` 這樣的程式碼。 ```c++= int getLight_2() { static int counter_green = 0; static int counter_yellow = 0; static int counter_red = 0; if (counter_green < 4) { ++counter_green; return 1; } else if (counter_yellow < 1) { ++counter_yellow; return 2; } else if (counter_red < 5) { ++counter_red; return 3; } else { counter_green = 1; counter_yellow = 0; counter_red = 0; return 1; } } ``` `getLight_2` 算是稍微可以理解了。從這個程式碼可以看到對於綠燈、黃燈、紅燈各有一個 counter,並透過 counter 值決定返回值。不過`getLight_2` 的好理解也僅止於認知到 counter 本身,至於返回值的意義與 counter 的判斷規則,還無法輕易理解。 ## 更好理解的程式碼 為清楚明白返回值的意義與 counter 的判斷規則,只要將難以解釋的純數字用文字替換,就能更好理解。 `getLight_3` 提供了替換後的程式碼。 ```c++= enum LightId { light_green = 1, light_yellow = 2, light_red = 3 }; LightId getLight_3() { const int duration_green = 4; const int duration_yellow = 1; const int duration_red = 5; static int counter_green = 0; static int counter_yellow = 0; static int counter_red = 0; if (counter_green < duration_green) { ++counter_green; return light_green; } else if (counter_yellow < duration_yellow) { ++counter_yellow; return light_yellow; } else if (counter_red < duration_red) { ++counter_red; return light_red; } else { counter_green = 1; counter_yellow = 0; counter_red = 0; return light_green; } } ``` 這份程式碼首先將返回值用 `enum` 代替純數字,以清楚表達返回值的意義;接著將 counter 判斷式的對象以 duration 來命名,就能明白 counter 是用來計數該燈號的區間。 ## 翻譯機 其實3段程式碼差異沒有很大,基本架構都相同,只是單純換換名字而已,就能讓**易讀性**有顯著的提升。提升的關鍵,在於程式碼是否 **「白話」** 。所謂白話,就是閱讀起來要能直接得到字面的意思,而不是要帶著**翻譯機**才能讀懂。 這3段程式碼可以看到3個翻譯機: ### counter 翻譯機 `getLight_1` 到 `getLight_2` 需翻譯出 counter 。 * `a` 就是 `counter_green` * `b` 就是 `counter_yellow` * `c` 就是 `counter_red` ### 燈號翻譯機 `getLight_2` 到 `getLight_3` 需翻譯出返回值所代表的燈號 。 * 返回`1` 就是綠燈 * 返回`2` 就是黃燈 * 返回`3` 就是紅燈 ### 燈號時長翻譯機 `getLight_2` 到 `getLight_3` 需翻譯出每個燈號所維持的秒數。 * `counter_green` 判斷的 `4` 表示是綠燈維持4秒 * `counter_yellow` 判斷的 `1` 表示是黃燈維持1秒 * `counter_red` 判斷的 `5` 表示是紅燈維持5秒 ## 文言文當然難懂 當我們要嘗試理解 `getLight_1`,卻需要帶著3個翻譯機才能讀懂。有沒有覺得這種體驗似曾相識?對,就是閱讀文言文。當我們在閱讀文言文的時候,許多詞句都需要再翻譯過,才能符合平常習慣的用法、才能符合**白話文**的理解方式。需要帶著翻譯機閱讀的程式碼就像文言文,考驗著讀者的翻譯能力。 ## 挑戰記憶力當然不易讀 需要翻譯機的程式碼,不論是對程式碼的讀者或作者,都是在**挑戰記憶力**。怎麼說呢?因為每個翻譯機都是特定一段、或者數段的程式碼所專有的,不同程式碼之間翻譯機沒有共通性。 比方說燈號翻譯機,1、2、3分別是綠燈、黃燈、紅燈的這件事情,不是一個普遍常識,在其他跟紅綠燈有關的程式碼並不存在這個關聯,也不存在跟紅綠燈無關的程式碼中,它只發生在這個案例。因此我們不能累積這個經驗到其他的程式碼上,也不能將其他程式碼的翻譯機對照到這個紅綠燈案例上。 既然不能累積經驗、讓經驗變成常識,只有強記一途。熟記每一個翻譯機,在閱讀或撰寫程式碼的時候,隨時用翻譯機做對照。別看這個案例也才只有3個翻譯機,或許不難記憶;如果不好好控制,翻譯機的需求量可能要十幾個、甚至數十個,就演變成**挑戰記憶力**了。既然直接寫出翻譯後的文字(也就是白話)就不用記了,又何必對人腦如此不友善呢? ## 白話不會寫起來比較久 還記得 [A0003](/bZBvxIR_QW2BrLPcfWQO3Q) 提過**慢慢來比較快**的概念嗎? `getLight_3` 很明顯寫起來不會比 `getLight_1` 慢,而且可以想像的是,撰寫 `getLight_1` 這樣程式碼的過程必須不斷查找翻譯機,萬一查錯翻譯機又要花很多時間 Debug 檢查正確性;而 `getLight_3` 相對白話了許多,除了不小心打錯字以外,應該很難出錯要 Debug 了吧。說穿了,照顧到**白話**的程式碼通常能更快完成,而**不寫白話**只是**自以為程式碼會比較快完成**,給自己懶得思考的藉口罷了! --- ## 延伸閱讀 [C0001 不白話的程式碼:文字加密](/tdzMvnrYTrySrC5iokmOtQ)

    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