The art of readable code ( pdf )

37
易讀程式 之美學 痞客邦前端組分享 - 徐如林 2015 / 11 / 20

Transcript of The art of readable code ( pdf )

Page 1: The art of readable code ( pdf )

易讀程式 之美學

痞客邦前端組分享 - 徐如林 2015 / 11 / 20

Page 2: The art of readable code ( pdf )

Code should be Easy to Understand

- 降低後⼈人的怒氣值 - 最⼩小化後⼈人理解的時間 ( "後⼈人" 包括隔天 / 三天後的⾃自⼰己 )

1

Page 3: The art of readable code ( pdf )

- Appropriate Naming - Aesthetics - Comments - Simplify Loops and Logic - Simplify Expressions - Variables and Readability - Reorganizing - Summary

2

Page 4: The art of readable code ( pdf )

- Appropriate Naming - Aesthetics - Comments - Simplify Loops and Logic - Simplify Expressions - Variables and Readability - Reorganizing - Summary

3

Page 5: The art of readable code ( pdf )

4Appropriate Naming

X - thisPlant O - manEaterPlant

pack information into the names

Page 6: The art of readable code ( pdf )

5Appropriate Naming

Be Specific

size() width()、height()、memory()

stop() pause()、kill()

Avoid Generic Names

tmp、result、retval

i、j、k

Prefer Concrete Names ( over Abstract Names )

severCanStart canListenOnPort

Page 7: The art of readable code ( pdf )

6Appropriate Naming

Values with Units

duration durationMs

Attach Details

password plaintextPassword

comment commentUnescaped

html htmlUtf8

Shorter Names for Shorter Scope PLEASE

Page 8: The art of readable code ( pdf )

7Appropriate Naming

Page 9: The art of readable code ( pdf )

8Appropriate Naming

filter() select()、exclude()

var cartTooBigLimit = 10; if (cart.num > cartTooBigLimit) { var maxItemsInCart = 10; if (cart.num > maxItemsInCart) { … }

[ min, max ] [ first, …last ] [ begin, …end )

Name of Boolean

readPw needPw、isAuth

disableClick = true; ableClick = false;

Match Expectations

getMean() computeMean()

Page 10: The art of readable code ( pdf )

- Appropriate Naming - Aesthetics - Comments - Simplify Loops and Logic - Simplify Expressions - Variables and Readability - Reorganizing - Summary

9

Page 11: The art of readable code ( pdf )

10Aesthetics

Page 12: The art of readable code ( pdf )

10Aesthetics

Make similar code look similar

Page 13: The art of readable code ( pdf )

- Appropriate Naming - Aesthetics - Comments - Simplify Loops and Logic - Simplify Expressions - Variables and Readability - Reorganizing - Summary

11

Page 14: The art of readable code ( pdf )

12Comments

Page 15: The art of readable code ( pdf )

12Comments

Page 16: The art of readable code ( pdf )

12Comments

Page 17: The art of readable code ( pdf )

13Comments

- director commentary - 這段程式法的 summary - 為什麼以這個⽅方法處理? - 有代表性的範例

- 註解程式碼的問題 - TODO、FIXME、HACK、XXX

- magic number

- 註解可能的陷阱 - eg. // 逾時時間 30 sec

Page 18: The art of readable code ( pdf )

- Appropriate Naming - Aesthetics - Comments - Simplify Loops and Logic - Simplify Expressions - Variables and Readability - Reorganizing - Summary

14

Page 19: The art of readable code ( pdf )

Simplify Loops and Logic15

Page 20: The art of readable code ( pdf )

Simplify Loops and Logic

- 尤達表⽰示法

- if / else - 先處理肯定的情況 - 先處理較簡單的情形

- eg. Guard Clause

- 減少使⽤用 ternary operator

- avoid do/while loop

16

Page 21: The art of readable code ( pdf )

Simplify Loops and Logic

- 尤達表⽰示法

- if / else - 先處理肯定的情況 - 先處理較簡單的情形

- eg. Guard Clause

- 減少使⽤用 ternary operator

- avoid do/while loop

16

Page 22: The art of readable code ( pdf )

- Appropriate Naming - Aesthetics - Comments - Simplify Loops and Logic - Simplify Expressions - Variables and Readability - Reorganizing - Summary

17

Page 23: The art of readable code ( pdf )

Simplify Expressions- use explaining variables

- if (line.split(':')[0].strip() === 'root') { … } - var username = line.split(':')[0].strip();

if (username === 'root') { … }

- use De Morgan’s Law - !(a || b) ⇔ !a && !b

- !(a && b) ⇔ !a || !b

- DRY ( don’t repeat yourself )

18

Page 24: The art of readable code ( pdf )

Simplify Expressions- use explaining variables

- if (line.split(':')[0].strip() === 'root') { … } - var username = line.split(':')[0].strip();

if (username === 'root') { … }

- use De Morgan’s Law - !(a || b) ⇔ !a && !b

- !(a && b) ⇔ !a || !b

- DRY ( don’t repeat yourself )

18

Page 25: The art of readable code ( pdf )

Simplify Expressions- use explaining variables

- if (line.split(':')[0].strip() === 'root') { … } - var username = line.split(':')[0].strip();

if (username === 'root') { … }

- use De Morgan’s Law - !(a || b) ⇔ !a && !b

- !(a && b) ⇔ !a || !b

- DRY ( don’t repeat yourself )

18

Page 26: The art of readable code ( pdf )

- Appropriate Naming - Aesthetics - Comments - Simplify Loops and Logic - Simplify Expressions - Variables and Readability - Reorganizing - Summary

19

Page 27: The art of readable code ( pdf )

Variables and Readability- shrink the scope of variables - use less global variables - prefer write-once variables

20

Page 28: The art of readable code ( pdf )

- Appropriate Naming - Aesthetics - Comments - Simplify Loops and Logic - Simplify Expressions - Variables and Readability - Reorganizing - Summary

21

Page 29: The art of readable code ( pdf )

Reorganizing

- 專案專屬的功能

22

Page 30: The art of readable code ( pdf )

Reorganizing

- 專案專屬的功能

22

Page 31: The art of readable code ( pdf )

Reorganizing

- 專案專屬的功能

- 抽離不相關的⼦子問題 - pure utility - general-purpose - simplify interfaces

22

Page 32: The art of readable code ( pdf )

Reorganizing23

Page 33: The art of readable code ( pdf )

Reorganizing24

Page 34: The art of readable code ( pdf )

- Appropriate Naming - Aesthetics - Comments - Simplify Loops and Logic - Simplify Expressions - Variables and Readability - Reorganizing - Summary

25

Page 35: The art of readable code ( pdf )

Summary26

Page 36: The art of readable code ( pdf )

Summary26

Page 37: The art of readable code ( pdf )

Thanks for your listening

end