+ All Categories
Home > Documents > 程式碼鑑賞 師大資工鄭永斌副教授. 軟體的困難本質 Complexity Complexity...

程式碼鑑賞 師大資工鄭永斌副教授. 軟體的困難本質 Complexity Complexity...

Date post: 21-Dec-2015
Category:
View: 289 times
Download: 0 times
Share this document with a friend
Popular Tags:
35
程程程程程 程程程程程 師師師師 師師師師 師師師師師師 師師師師師師
Transcript
Page 1: 程式碼鑑賞 師大資工鄭永斌副教授. 軟體的困難本質 Complexity Complexity Conformity Conformity Changeability Changeability Invisibility (抽象化) Invisibility

程式碼鑑賞程式碼鑑賞師大資工 師大資工

鄭永斌副教授鄭永斌副教授

Page 2: 程式碼鑑賞 師大資工鄭永斌副教授. 軟體的困難本質 Complexity Complexity Conformity Conformity Changeability Changeability Invisibility (抽象化) Invisibility

軟體的困難本質軟體的困難本質 ComplexityComplexity ConformityConformity ChangeabilityChangeability Invisibility Invisibility (抽象化)(抽象化)

Page 3: 程式碼鑑賞 師大資工鄭永斌副教授. 軟體的困難本質 Complexity Complexity Conformity Conformity Changeability Changeability Invisibility (抽象化) Invisibility

ComplexityComplexity

非其他創作能比擬非其他創作能比擬 沒有兩個部分是一樣的沒有兩個部分是一樣的 However, in hardware, building, automobile conHowever, in hardware, building, automobile con

struction, they can struction, they can 大量的重複使用零件大量的重複使用零件 Digital computer Digital computer 比大部分人們建造的東西要更比大部分人們建造的東西要更

複雜複雜 有一大堆狀態有一大堆狀態 使得理解,描述,測試都非常困難使得理解,描述,測試都非常困難

軟體系統的狀態又比 軟體系統的狀態又比 digital computer digital computer 多出幾多出幾個數量級個數量級

Page 4: 程式碼鑑賞 師大資工鄭永斌副教授. 軟體的困難本質 Complexity Complexity Conformity Conformity Changeability Changeability Invisibility (抽象化) Invisibility

When scaling upWhen scaling up

雖然有少數同樣的元素可以重複使用雖然有少數同樣的元素可以重複使用 不同元素的數量也勢必增加不同元素的數量也勢必增加

彼此的交互作用成指數性成長彼此的交互作用成指數性成長

Page 5: 程式碼鑑賞 師大資工鄭永斌副教授. 軟體的困難本質 Complexity Complexity Conformity Conformity Changeability Changeability Invisibility (抽象化) Invisibility

其他科學的突飛猛進緣由其他科學的突飛猛進緣由 數學與物理學的三世紀的突飛猛進數學與物理學的三世紀的突飛猛進

由複雜現象─由複雜現象─ > model > model 簡單的模型簡單的模型 這種方式之所以行的通,是因為模型中所排除掉的複這種方式之所以行的通,是因為模型中所排除掉的複雜性非現象的本質,如果這些複雜性是屬與本質上的雜性非現象的本質,如果這些複雜性是屬與本質上的特性,就行不通了特性,就行不通了

軟體的老問題來自於複雜的本質以及複雜性隨著軟體的老問題來自於複雜的本質以及複雜性隨著軟體規模呈非線性成長的特性軟體規模呈非線性成長的特性 我們對付的問題是複雜性本身我們對付的問題是複雜性本身

功能上的複雜性功能上的複雜性 結構上的複雜性結構上的複雜性

使程式擴充新功能時,難保不會產生副作用使程式擴充新功能時,難保不會產生副作用

Page 6: 程式碼鑑賞 師大資工鄭永斌副教授. 軟體的困難本質 Complexity Complexity Conformity Conformity Changeability Changeability Invisibility (抽象化) Invisibility

Conformity (Conformity ( 配合性)配合性) 面對複雜性 面對複雜性 we are not alone.we are not alone.

物理的粒子世界物理的粒子世界 但是背後總是能但是背後總是能化約化約成一種簡單都理論成一種簡單都理論

很不幸,軟體開發沒有這種信仰與化約很不幸,軟體開發沒有這種信仰與化約 軟體要配合的東西是由不同的人所設計,而非上軟體要配合的東西是由不同的人所設計,而非上

帝帝 更多的複雜性是來自於必須配合其他領域的介面更多的複雜性是來自於必須配合其他領域的介面

所致所致 這部分難以簡化這部分難以簡化

Page 7: 程式碼鑑賞 師大資工鄭永斌副教授. 軟體的困難本質 Complexity Complexity Conformity Conformity Changeability Changeability Invisibility (抽象化) Invisibility

changeabilitychangeability

其他製造業的產品雖然必須面對改變的壓其他製造業的產品雖然必須面對改變的壓力,不過量產之後很少更動,只有會背後力,不過量產之後很少更動,只有會背後來的新型產品所取代來的新型產品所取代 產品回收,現場變更產品回收,現場變更 (field change),(field change), 頻率都比頻率都比軟體來的非常非常低軟體來的非常非常低

Page 8: 程式碼鑑賞 師大資工鄭永斌副教授. 軟體的困難本質 Complexity Complexity Conformity Conformity Changeability Changeability Invisibility (抽象化) Invisibility

InvisibilityInvisibility

You cannot seeYou cannot see You cannot touchYou cannot touch

A building blue print A building blue print 可以幫助建築師與客可以幫助建築師與客戶一起評估空間動線與景觀戶一起評估空間動線與景觀

機械零件比例圖,化學粒子棒線圖模型都達到機械零件比例圖,化學粒子棒線圖模型都達到類似的效果類似的效果

軟體的本質與軟體的本質與空間空間沒有關係,所以沒有什沒有關係,所以沒有什麼幾何表示法可用麼幾何表示法可用

Page 9: 程式碼鑑賞 師大資工鄭永斌副教授. 軟體的困難本質 Complexity Complexity Conformity Conformity Changeability Changeability Invisibility (抽象化) Invisibility

Software Software 的表示圖的表示圖 可能是許多 可能是許多 directed graphsdirected graphs 這些圖可表現這些圖可表現 control flow, data flow, recontrol flow, data flow, re

lation, temporal relation, namespace lation, temporal relation, namespace … etc.… etc.

通常不是通常不是 2D2D ,也少有階層性,也少有階層性 由於軟體開發少了強大的概念性工具的意由於軟體開發少了強大的概念性工具的意圖,不僅阻礙了一個人腦袋所進行的設計圖,不僅阻礙了一個人腦袋所進行的設計過程,更會嚴重阻礙過程,更會嚴重阻礙不同大腦不同大腦之間的溝通之間的溝通

How about UML?How about UML?

Page 10: 程式碼鑑賞 師大資工鄭永斌副教授. 軟體的困難本質 Complexity Complexity Conformity Conformity Changeability Changeability Invisibility (抽象化) Invisibility

OKOK,, now what?now what? 軟體有抽象的本質軟體有抽象的本質 軟體有看不到的本質軟體有看不到的本質 軟體有難以描述的本質軟體有難以描述的本質 請問軟體開發有藝術的成分在嗎?請問軟體開發有藝術的成分在嗎?

Page 11: 程式碼鑑賞 師大資工鄭永斌副教授. 軟體的困難本質 Complexity Complexity Conformity Conformity Changeability Changeability Invisibility (抽象化) Invisibility

程式碼鑑賞程式碼鑑賞 程式設計有藝術的成分在嗎?程式設計有藝術的成分在嗎?

軟工的研究顯示軟工的研究顯示

另外一個研究顯示另外一個研究顯示 成為高手與庸手與年資經驗沒有絕對關係成為高手與庸手與年資經驗沒有絕對關係

研究顯示,高手與庸手的表現有極大的差異,研究顯示,高手與庸手的表現有極大的差異,而且往往是一個數量級的差異 (而且往往是一個數量級的差異 ( an order an order of magnitude, 10^1)of magnitude, 10^1)

- SACKMAN, EROKSO- SACKMAN, EROKSON, and GRANTN, and GRANT

Page 12: 程式碼鑑賞 師大資工鄭永斌副教授. 軟體的困難本質 Complexity Complexity Conformity Conformity Changeability Changeability Invisibility (抽象化) Invisibility

YesYes

我們認為寫軟體有藝術的成分在我們認為寫軟體有藝術的成分在 藝術的東西藝術的東西

有時只能意會不能言傳有時只能意會不能言傳 有學過畫畫等藝術的東西嗎?請問你怎麼有學過畫畫等藝術的東西嗎?請問你怎麼學的?學的?

上過藝術鑑賞這種課程嗎?上過藝術鑑賞這種課程嗎? 通常藝術的傳授一定會配合藝術的鑑賞通常藝術的傳授一定會配合藝術的鑑賞

Page 13: 程式碼鑑賞 師大資工鄭永斌副教授. 軟體的困難本質 Complexity Complexity Conformity Conformity Changeability Changeability Invisibility (抽象化) Invisibility

ANDAND ?? 你看過多少別人的程式碼?(教科書的片你看過多少別人的程式碼?(教科書的片段程式碼不算)段程式碼不算)

你知道什麼樣的程式碼是好的?你知道什麼樣的程式碼是好的? 你知道什麼樣的程式碼是壞的?你知道什麼樣的程式碼是壞的? 我們先來看看壞的我們先來看看壞的

Page 14: 程式碼鑑賞 師大資工鄭永斌副教授. 軟體的困難本質 Complexity Complexity Conformity Conformity Changeability Changeability Invisibility (抽象化) Invisibility

Spaghetti CodeSpaghetti Code

Page 15: 程式碼鑑賞 師大資工鄭永斌副教授. 軟體的困難本質 Complexity Complexity Conformity Conformity Changeability Changeability Invisibility (抽象化) Invisibility
Page 16: 程式碼鑑賞 師大資工鄭永斌副教授. 軟體的困難本質 Complexity Complexity Conformity Conformity Changeability Changeability Invisibility (抽象化) Invisibility
Page 17: 程式碼鑑賞 師大資工鄭永斌副教授. 軟體的困難本質 Complexity Complexity Conformity Conformity Changeability Changeability Invisibility (抽象化) Invisibility
Page 18: 程式碼鑑賞 師大資工鄭永斌副教授. 軟體的困難本質 Complexity Complexity Conformity Conformity Changeability Changeability Invisibility (抽象化) Invisibility

Lasagna codeLasagna code

Lasagna code is used to describe Lasagna code is used to describe software that has a simple, software that has a simple, understandable, and layered structure. understandable, and layered structure. Lasagna code, although structured, is Lasagna code, although structured, is unfortunately monolithic and not easy unfortunately monolithic and not easy to modify. An attempt to change one to modify. An attempt to change one layer conceptually simple, is often very layer conceptually simple, is often very difficult in actual practice. difficult in actual practice.

Page 19: 程式碼鑑賞 師大資工鄭永斌副教授. 軟體的困難本質 Complexity Complexity Conformity Conformity Changeability Changeability Invisibility (抽象化) Invisibility
Page 20: 程式碼鑑賞 師大資工鄭永斌副教授. 軟體的困難本質 Complexity Complexity Conformity Conformity Changeability Changeability Invisibility (抽象化) Invisibility

Ravioli codeRavioli code

In ravioli code, each of the components, In ravioli code, each of the components, or objects, is a package containing some or objects, is a package containing some meat or other nourishment for the meat or other nourishment for the system; any component can be modified system; any component can be modified or replaced without significantly or replaced without significantly affecting other components. affecting other components.

The ideal software structure is one The ideal software structure is one having components that are small and having components that are small and loosely coupled; this ideal structure is loosely coupled; this ideal structure is called ravioli code. called ravioli code.

Page 21: 程式碼鑑賞 師大資工鄭永斌副教授. 軟體的困難本質 Complexity Complexity Conformity Conformity Changeability Changeability Invisibility (抽象化) Invisibility
Page 22: 程式碼鑑賞 師大資工鄭永斌副教授. 軟體的困難本質 Complexity Complexity Conformity Conformity Changeability Changeability Invisibility (抽象化) Invisibility

Now, let’s look at some spaghetti code Now, let’s look at some spaghetti code and give a critics and give a critics

Page 23: 程式碼鑑賞 師大資工鄭永斌副教授. 軟體的困難本質 Complexity Complexity Conformity Conformity Changeability Changeability Invisibility (抽象化) Invisibility

Top 100 signs that I may be Top 100 signs that I may be writingwriting spaghetti codespaghetti code

1.1. I have no idea where this constant is defined I have no idea where this constant is defined 2.2. I have printf stmts littered throughout my code. I have printf stmts littered throughout my code. 3.3. There is an error but it isn't handled, and I can't find it to figThere is an error but it isn't handled, and I can't find it to fig

ure out what's wrong. ure out what's wrong. 4.4. Passing variables directly to 635 functions and using 'global' Passing variables directly to 635 functions and using 'global'

on 453 functions on 453 functions 5.5. I have to modify the same function 10 times because I have I have to modify the same function 10 times because I have

not moved it into a function...yet. not moved it into a function...yet. 6.6. I find that rewriting is quicker and easier than modifying thI find that rewriting is quicker and easier than modifying th

e original design. e original design. 7.7. You have a library of small functions that you wrote yourselYou have a library of small functions that you wrote yoursel

f, and will cut'n'paste them into almost every project you wof, and will cut'n'paste them into almost every project you work on... Whether they're needed or not... rk on... Whether they're needed or not...

Now let’s continue and win your creditsNow let’s continue and win your credits

Page 24: 程式碼鑑賞 師大資工鄭永斌副教授. 軟體的困難本質 Complexity Complexity Conformity Conformity Changeability Changeability Invisibility (抽象化) Invisibility

8.8. Every time you want to change a variable you have to use grEvery time you want to change a variable you have to use grep to find it ;) ep to find it ;)

9.9. you have so many "for" loops you've declared a variable for eyou have so many "for" loops you've declared a variable for each letter in the alphabet. ach letter in the alphabet.

10.10. there are enough "if" statements to make you scroll to the rithere are enough "if" statements to make you scroll to the right. ght.

11.11. You see comments in the code that read something like: You see comments in the code that read something like: [Your Name: Don't ask me how or why this works, I know it [Your Name: Don't ask me how or why this works, I know it looks overly complex and it appears there IS a simpler solutilooks overly complex and it appears there IS a simpler solution, but this works, it fixed a bug from the 'simpler code'...juon, but this works, it fixed a bug from the 'simpler code'...just leave it be...don't touch it, I've hired a voodoo witch doctost leave it be...don't touch it, I've hired a voodoo witch doctor to place a curse on anybody who alters this code...trust me,r to place a curse on anybody who alters this code...trust me, just leave it alone! ] just leave it alone! ]

12.12. you know you're digging through a bowl of the stuff when, ayou know you're digging through a bowl of the stuff when, as a regular tool, you're using the windoz's search feature to fs a regular tool, you're using the windoz's search feature to find functions, variables and other bits of code. ind functions, variables and other bits of code.

Top 100 signs that I may be Top 100 signs that I may be writingwriting spaghetti code- spaghetti code-

(CONT)(CONT)

Page 25: 程式碼鑑賞 師大資工鄭永斌副教授. 軟體的困難本質 Complexity Complexity Conformity Conformity Changeability Changeability Invisibility (抽象化) Invisibility

Top 100 signs that I may be Top 100 signs that I may be writingwriting spaghetti code- spaghetti code-

(CONT)(CONT)13.13. having to "peel" your way through while/for loops and a chunks of Ihaving to "peel" your way through while/for loops and a chunks of I

F and ELSE statements because the original author (probably in all F and ELSE statements because the original author (probably in all his haste) couldn't be bothered to use brackets. The code works but his haste) couldn't be bothered to use brackets. The code works but not always for the right reasons. not always for the right reasons.

14.14. use every imaginable variation of the word "foobar" in variable namuse every imaginable variation of the word "foobar" in variable names es

15.15. You sit down, open your program, and wonder where to begin with You sit down, open your program, and wonder where to begin with debugging... (ooh boy, I know the feeling of that!) debugging... (ooh boy, I know the feeling of that!)

16.16. You wrap it up and realize that there are 20 funtions in the include You wrap it up and realize that there are 20 funtions in the include files which are never called, although at some point, they were all rfiles which are never called, although at some point, they were all required. equired.

17.17. Changing database is (e.g MS SQL to MySQL) requires the rewrite of Changing database is (e.g MS SQL to MySQL) requires the rewrite of SQL statments. SQL statments.

18.18. You realize that you are changing global variables with functions inYou realize that you are changing global variables with functions inside include files side include files

19.19. You create a separate class for every single function. You create a separate class for every single function. 20.20. You have no idea what a CRC Card is ... You have no idea what a CRC Card is ...

Page 26: 程式碼鑑賞 師大資工鄭永斌副教授. 軟體的困難本質 Complexity Complexity Conformity Conformity Changeability Changeability Invisibility (抽象化) Invisibility

To be continued!!To be continued!!

Page 27: 程式碼鑑賞 師大資工鄭永斌副教授. 軟體的困難本質 Complexity Complexity Conformity Conformity Changeability Changeability Invisibility (抽象化) Invisibility

Coding StyleCoding Style

Anything wrong?Anything wrong?

Page 28: 程式碼鑑賞 師大資工鄭永斌副教授. 軟體的困難本質 Complexity Complexity Conformity Conformity Changeability Changeability Invisibility (抽象化) Invisibility

Be consistentBe consistent

Page 29: 程式碼鑑賞 師大資工鄭永斌副教授. 軟體的困難本質 Complexity Complexity Conformity Conformity Changeability Changeability Invisibility (抽象化) Invisibility

Coding horror!Coding horror!

Page 30: 程式碼鑑賞 師大資工鄭永斌副教授. 軟體的困難本質 Complexity Complexity Conformity Conformity Changeability Changeability Invisibility (抽象化) Invisibility

What’s wrong with this What’s wrong with this piece of code?piece of code?

Page 31: 程式碼鑑賞 師大資工鄭永斌副教授. 軟體的困難本質 Complexity Complexity Conformity Conformity Changeability Changeability Invisibility (抽象化) Invisibility
Page 32: 程式碼鑑賞 師大資工鄭永斌副教授. 軟體的困難本質 Complexity Complexity Conformity Conformity Changeability Changeability Invisibility (抽象化) Invisibility
Page 33: 程式碼鑑賞 師大資工鄭永斌副教授. 軟體的困難本質 Complexity Complexity Conformity Conformity Changeability Changeability Invisibility (抽象化) Invisibility
Page 34: 程式碼鑑賞 師大資工鄭永斌副教授. 軟體的困難本質 Complexity Complexity Conformity Conformity Changeability Changeability Invisibility (抽象化) Invisibility

This class is representing two ADTs: an This class is representing two ADTs: an employee and a ListContainer.employee and a ListContainer.

Page 35: 程式碼鑑賞 師大資工鄭永斌副教授. 軟體的困難本質 Complexity Complexity Conformity Conformity Changeability Changeability Invisibility (抽象化) Invisibility

Recommended