Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Clean Code
Search
Johnny Lin
September 05, 2022
Programming
150
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Clean Code
Johnny Lin
September 05, 2022
More Decks by Johnny Lin
See All by Johnny Lin
初探Rust.pdf
johnny178
0
140
Other Decks in Programming
See All in Programming
AWS CDKのカスタムリソースでContinuum(旧Security Agent)を実装した話 | Implementing Continuum (formerly Security Agent) via AWS CDK Custom Resource
akihisaikeda
2
150
JAWS-UG 東京支部が始める、JAWS-UG支部コラボ / JAWS-UG lunchtime LT Collaboration
y0hgi
0
270
3回つくってわかった、 定期実行ジョブ基盤の本質情報
takai
0
450
気持ちぃ〜角丸 Squircle
ryu1013
0
310
パスワード再設定メールのURLについて知っておこう
uyuki234
0
160
カツオ、ご期待ください
suneo3476
0
190
Agentic Software Factoryに、すごく賢いIF文を。 / super smart IF statement into the Agentic Software Factory.
rkaga
8
2.2k
Everything will be SERVERLESS — 信じて運用した10年の経験値 / Everything Will be Serverless — Lessons Learned from 10 Years of Operational Experience
seike460
PRO
1
1.1k
Starting & Sustaining Code-Based E2E Testing for Non-Coding QA Teams( #jasstniigata )
teyamagu
PRO
1
1k
UnityでSystem.Net.WebSocketsなWebSocketサーバが動かないのでUnity Monoのコードを覗いてみた / about implementing websocket server with unity mono
drumath2237
1
610
IBM Bob Dojo #1 仕様駆動開発入門
oniak3ibm
PRO
0
390
Atomic Design, Enforced: Scaling Mobile Design Systems (next.app devCon / droidCon Berlin 2026)
steliosf
PRO
0
170
Featured
See All Featured
Agile that works and the tools we love
rasmusluckow
331
22k
Ruling the World: When Life Gets Gamed
codingconduct
0
400
Taking LLMs out of the black box: A practical guide to human-in-the-loop distillation
inesmontani
PRO
3
2.5k
Side Projects
sachag
456
43k
Java REST API Framework Comparison - PWX 2021
mraible
34
9.8k
AI: The stuff that nobody shows you
jnunemaker
PRO
10
1.2k
Speed Design
sergeychernyshev
33
2.2k
Balancing Empowerment & Direction
lara
6
1.3k
Tips & Tricks on How to Get Your First Job In Tech
honzajavorek
1
800
End of SEO as We Know It (SMX Advanced Version)
ipullrank
3
4.4k
Collaborative Software Design: How to facilitate domain modelling decisions
baasie
1
340
Odyssey Design
rkendrick25
PRO
2
870
Transcript
讓你的Code 更乾淨🧹 Johnny Lin @Anue Tech Sharing, 2022/09/07
1. Clean Code 2. 命名 3. 函式 4. 註解 Outline
什麼是Clean Code? 優雅、簡單、明瞭 沒有重複的程式碼 能通過所有的測試 較少的相依性
Why Clean Code?
閱讀程式碼、寫程式時間比例 -> 10:1
閱讀程式碼、寫程式時間比例 -> 10:1 花時間寫更簡潔的Code >> 日後閱讀與維護
閱讀程式碼、寫程式時間比例 -> 10:1 花時間寫更簡潔的Code >> 日後閱讀與維護
閱讀程式碼、寫程式時間比例 -> 10:1 花時間寫更簡潔的Code >> 日後閱讀與維護 Clean Code is IMPORTANT
有意義的命名
微小差距的名稱: XYZControllerForEfficientHandlingOfString XYZControllerForEfficientStoreageOfString 避免誤導
同時使用小寫L 、大寫O 當變數名稱 避免誤導
IShapeFactory 與ShapeFactory: 多餘的I 字首 簡短且有力、避免多餘的單詞命名
IShapeFactory 與ShapeFactory: 多餘的I 字首 accountData 與account: 多餘的Data 單詞 簡短且有力、避免多餘的單詞命名
讓名稱代表意圖
函式
「關於函式的首要準則,就是要簡短。第二項準則,就是要比第一項的簡短函式還 要更簡短。」 簡短!
僅兩行、三行或四行 每個函式帶領你到下一個函式 「關於函式的首要準則,就是要簡短。第二項準則,就是要比第一項的簡短函式還 要更簡短。」 簡短!
Single Responsibility Principle(SRP) 「函式應該只做一件事。它們應該把這件事做好。而且他們只做這件事。」 只做一件事
Single Responsibility Principle(SRP) 「函式應該只做一件事。它們應該把這件事做好。而且他們只做這件事。」 如何判斷? 只做一件事
A module should have one, and only one reason to
change. Single Responsibility Principle(SRP) 「函式應該只做一件事。它們應該把這件事做好。而且他們只做這件事。」 如何判斷? 只做一件事
A module should have one, and only one reason to
change. 如果我們以文字的方式描述一個函式,看到其中包含了「不同層次」的抽象概念 步驟,就可以判斷這個程式包含了不只一件事。 Single Responsibility Principle(SRP) 「函式應該只做一件事。它們應該把這件事做好。而且他們只做這件事。」 如何判斷? 只做一件事
One thing
1. 判斷此頁面是否為測試頁面 2. 如果是測試頁面的話,則納入設定與拆解步驟 3. 將此頁面轉換為HTML 網頁
一件事還是三件事?
判斷此頁面是否為測試頁面 如果是測試頁面的話,則納入設定與拆解步驟 將此頁面轉換為HTML 網頁 TO RenderPageWithSetupsAndTeardowns 一件事還是三件事?
生活中的例子: 洗澡 只做一件事
「將flag 變數傳遞給函式,等於大聲宣布此函數做超過一件事。」 - Uncle Bob flag 參數
try 開頭,catch/finally 結尾,不含有其他程式碼 「函式只做一件事,而錯誤處理就是一件事」 錯誤處理就是一件事
程式變得擁擠 重複的改變,倍數增長的工時 避免重複的程式碼 不要重複自己 Don't Repeat Yourself(DRY)
註解
用程式碼表達你的本意 「寫註解的其中一個動機,是因為程式碼寫的太糟糕。」 - Uncle Bob 註解無法彌補糟糕的程式碼
在每個檔案的開頭都會寫入著作權聲名 法律型註解 有益的註解
沒辦法使用少量的單字為變數命名,或是用適當的動詞作為函式的命名 對意圖的解釋 有益的註解
docstring 有益的註解
喃喃自語 糟糕的註解
干擾型註解 糟糕的註解
交給Git 被註解的程式碼 糟糕的註解
“The only way to go fast is to go well.“
- Uncle Bob
https://medium.com/%E6%89%8B%E5%AF%AB%E7%AD%86%E8%A8%98/clean-code-index- 51e209cc47db Reference
Thank you for listening