Posted on 2005-01-13 20:55
zhoulch's blog 閱讀(735)
評論(1) 編輯 收藏 所屬分類:
Soft Engineering
越復雜的東西越容易出錯,自動步槍的威力強于狼牙棒,但步槍會卡殼,狼牙棒卻不會。同樣對于軟件開發也一樣,一個進行FFT變換的程序肯定比一個計算10以內加法的程序要復雜,也更容易出錯。
軟件的復雜有兩種,一種是由于問題域自身的復雜所造成的,比如FFT肯定比10以內加法要復雜,這個是我們無法避免的。我所關心的是第二種復雜,就是人為造成的復雜。
一個計算FFT的程序,我可以把它化為若干的函數來執行,每個函數只做一件很簡單的事,這樣每個函數單獨都可以由于自身的簡單,而充分保證自己的可靠和正確。那么我只要能夠讓各個函數正確的合作就可以寫出一個可靠的FFT程序。也就是說把復雜的問題變成很多個簡單的問題。
在平時的程序設計中,很難對復雜有個明確的定義和劃分界限。是以代碼長度?還是以算法的復雜?而且如果由人工來完成,那么由于人的惰性,寫完程序就已經可以手工了,很少有人會去檢查程序中各個模塊的復雜性,就更不會去主動重構代碼,來降低他的復雜度了。
今天看到一個工具devMetrics,是在VS.NET下對代碼復雜性進行分析的工具。我用它對才完成的GisMediator進行分析。結果如下
Member Locations | Member Measures |
Project | Class | Member | Statements | Comments | Comment Ratio | Complexity |
GisMediator | GisXmlNode | GisXmlNode | 11 | 0 | 0.0 | 2 |
GisMediator | GisXmlNode | GisXmlNode | 24 | 6 | 0.3 | 7 |
GisMediator | GisXmlNode | fGisXmlNodeUnLoad | 7 | 1 | 0.1 | 4 |
GisMediator | GisXmlTree | GisXmlTree | 5 | 0 | 0.0 | 1 |
GisMediator | GisSchemaElement | GisSchemaElement | 10 | 1 | 0.1 | 3 |
GisMediator | GisSchemaSimpleType | GisSchemaSimpleType | 20 | 2 | 0.1 | 5 |
GisMediator | GisSchemaComplexType | GisSchemaComplexType | 15 | 1 | 0.1 | 5 |
GisMediator | GisSchemaComplexType | GisSchemaComplexTypeCompare... | 14 | 0 | 0.0 | 8 |
GisMediator | GisSchemaTree | GisSchemaTree | 13 | 1 | 0.1 | 2 |
GisMediator | GisSchemaTree | fDecodeSchema | 53 | 14 | 0.3 | 24 |
GisMediator | GisSchemaTree | fCompileSchema | 4 | 0 | 0.0 | 1 |
GisMediator | GisSchemaTree | SchemaValidationHandler | 1 | 1 | 1.0 | 1 |
GisMediator | GisSchemaTree | fSchemaTreeSearch | 14 | 0 | 0.0 | 11 |
GisMediator | GisMainForm | GisMainForm | 2 | 3 | 1.5 | 1 |
GisMediator | GisMainForm | Dispose | 7 | 3 | 0.4 | 3 |
GisMediator | GisMainForm | InitializeComponent | 80 | 46 | 0.6 | 1 |
GisMediator | GisMainForm | Main | 2 | 3 | 1.5 | 1 |
GisMediator | GisMainForm | GisMainForm_Load | 4 | 0 | 0.0 | 1 |
GisMediator | GisMainForm | GisMainForm_Closing | 10 | 0 | 0.0 | 10 |
GisMediator | GisMainForm | GisMainMenu_New_Click | 9 | 2 | 0.2 | 1 |
GisMediator | GisMainForm | GisMainMenu_Open_Click | 13 | 3 | 0.2 | 3 |
GisMediator | GisMainForm | GisMainMenu_Save_Click | 2 | 0 | 0.0 | 1 |
GisMediator | GisMainForm | GisMainMenu_SaveAs_Click | 7 | 0 | 0.0 | 3 |
GisMediator | GisMainForm | GisMainMenu_Print_Click | 3 | 0 | 0.0 | 1 |
GisMediator | GisMainForm | GisMainMenu_Exit_Click | 2 | 0 | 0.0 | 1 |
GisMediator | GisMainForm | GisFlexGrid_Initialize | 18 | 1 | 0.1 | 1 |
GisMediator | GisMainForm | fGisFlexGridSetCellStyle | 26 | 0 | 0.0 | 13 |
GisMediator | GisMainForm | fGisFlexGridSetContextMenu | 80 | 6 | 0.1 | 31 |
GisMediator | GisMainForm | GisFlexGrid_DoubleClick | 11 | 0 | 0.0 | 5 |
GisMediator | GisMainForm | GisFlexGrid_BeforeMouseDown | 20 | 0 | 0.0 | 7 |
GisMediator | GisMainForm | GisFlexGrid_AfterEdit | 2 | 0 | 0.0 | 1 |
GisMediator | GisMainForm | fGisFlexGrid_ContextMenu_In... | 32 | 6 | 0.2 | 10 |
GisMediator | GisMainForm | fGisFlexGrid_ContextMenu_De... | 12 | 2 | 0.2 | 2 |
GisMediator | GisMainForm | fGisReadXmlFile | 8 | 2 | 0.3 | 2 |
GisMediator | GisMainForm | fGisXmlToFlexGrid | 22 | 7 | 0.3 | 8 |
GisMediator | GisMainForm | fGisWriteXmlFile | 11 | 3 | 0.3 | 2 |
GisMediator | GisMainForm | fGisFlexGridToXml | 23 | 3 | 0.1 | 7 |
GisMediator | GisMainForm | fGisElementNameMapping | 24 | 2 | 0.1 | 5 |
確實,fDecodeSchema、fSchemaTreeSearch、fGisFlexGridSetContextMenu、fGisFlexGridSetCellStyle都是幾個很重要,而且很復雜的函數模塊。尤其是fGisFlexGridSetContextMenu,復雜度已經到了31,可見其已經很復雜且臃腫了。看來有必要對這幾個函數進行重構了。
簡單就是美,這點在計算機軟件設計上也是不破的真理。過于復雜的程序,一則難以復用,二則難以維護。
以后對于自己的程序,可以經常利用devMetrics,務必降低代碼模塊的復雜性。