SlideShare uma empresa Scribd logo
1 de 60
Classification 5/26/2011 1 Refactoring10 – Making Method Calls Simpler Allen Chien 2011-05-15
Agenda Function Name Function Parameter Private Function Constructor Exception private example(intparam) Classification 5/26/2011 2
Function Name Rename Method Classification 5/26/2011 3
Function Parameters Add Parameters Remove Parameters Separate Query From Modifier Parameterize Method Replace Parameter with Explicit Method Preserve Whole Object Replace Parameter with Method Introduce Parameter Object Classification 5/26/2011 4
Private Function Hide Method Classification 5/26/2011 5
Constructor Remove Setting Method Replace Constructor with Factory Method Encapsulate Downcast Classification 5/26/2011 6
Exception Replace Error Code with Exception Replace Exception with Test Classification 5/26/2011 7
Function Name Rename Method (忽略  “做法”) Classification 5/26/2011 8
Rename Method Motivation 函式名稱應該準確表達它的用途 為函式寫上一句註釋, 然後為該註釋給予一個名稱 Example Classification 5/26/2011 9
Function Parameters Add Parameters Remove Parameters Separate Query From Modifier Parameterize Method Replace Parameter with Explicit Method Preserve Whole Object Replace Parameter with Method Introduce Parameter Object Classification 5/26/2011 10
Add Parameter Motivation 修改函式後, 需要增加資訊 壞味道 :Data Clumps (P81) 建議:Introduce Parameter Object (295) Classification 5/26/2011 11
Function Parameters Add Parameters Remove Parameters Separate Query From Modifier Parameterize Method Replace Parameter with Explicit Method Preserve Whole Object Replace Parameter with Method Introduce Parameter Object Classification 5/26/2011 12
Remove Parameter Motivation 修改函式後, 去除參數的重構 在多型的情況下, 需要檢查該函式是否已被其他程式實做 Classification 5/26/2011 13
Function Parameters Add Parameters Remove Parameters Separate Query From Modifier Parameterize Method Replace Parameter with Explicit Method Preserve Whole Object Replace Parameter with Method Introduce Parameter Object Classification 5/26/2011 14
Separate Query from Modifier Motivation 某個函式既回傳物件狀態值, 又修改物件狀態 (getXXX + setXXX) 既有返回值又有副作用就應該分離 (getXXX only) Meyer’s Rule: 任何有返回值的函式皆不應有副作用 優點: 增加重複查詢的效能 總是獲得相同的結果 [Allen]  函式名稱與期望結果一致 Example Classification 5/26/2011 15
Classification 5/26/2011 16
Classification 5/26/2011 17 Substitute Algorithm (139)
Function Parameters Add Parameters Remove Parameters Separate Query From Modifier Parameterize Method Replace Parameter with Explicit Method Preserve Whole Object Replace Parameter with Method Introduce Parameter Object Classification 5/26/2011 18
Parameterize Method Motivation 問題:  某個函式做類似的工作, 但是函式本體卻包含不同的值 方式 使用單一函式, 並以參數來表達不同的值 將少量數值視為參數, 找出重覆的程式碼 優點:  減少重複的程式碼 [Allen] 與 Replace Parameter with Explicit Method 相反 Example Classification 5/26/2011 19
Classification 5/26/2011 20
Classification 5/26/2011 21
Function Parameters Add Parameters Remove Parameters Separate Query From Modifier Parameterize Method Replace Parameter with Explicit Method Preserve Whole Object Replace Parameter with Method Introduce Parameter Object Classification 5/26/2011 22
Replace Parameter with Explicit Methods Motivation 與Parameterize Method 相反 條件: 離散取值 函式中以條件事檢查參數 [Allen] 將 IF-ELSE 或 SWITCH 移除 [Allen] Code 不一樣 優點: 避免出現條件式 利用編譯器檢查程式 介面清楚 使用參數時, 則須判斷參數合法性 [Allen] 如 switch之default Example Classification 5/26/2011 23
Classification 5/26/2011 24
Function Parameters Add Parameters Remove Parameters Separate Query From Modifier Parameterize Method Replace Parameter with Explicit Method Preserve Whole Object Replace Parameter with Method Introduce Parameter Object Classification 5/26/2011 25
Preserve Whole Object Motivation 參數為某一物件中取出, 則將該物件當作輸入參數 優點: 避免新增修改參數項 減少參數數量, 方便其他程式呼叫使用 條件: 因依存關係導致結構惡化則不可使用 Example Classification 5/26/2011 26
Classification 5/26/2011 27
Classification 5/26/2011 28
Function Parameters Add Parameters Remove Parameters Separate Query From Modifier Parameterize Method Replace Parameter with Explicit Method Preserve Whole Object Replace Parameter with Method Introduce Parameter Object Classification 5/26/2011 29
Replace Parameter with Methods Motivation 物件換起某個函式, 並將所得結果作為參數, 傳遞給另一個函式 接受該參數的函式也可以喚起前一個函式 條件: 接收端是否可以透過計算而取得函數值 優點: 減少參數數量 使函式容易理解 Example Classification 5/26/2011 30
Classification 5/26/2011 31
Classification 5/26/2011 32 Inline Method (117)
Function Parameters Add Parameters Remove Parameters Separate Query From Modifier Parameterize Method Replace Parameter with Explicit Method Preserve Whole Object Replace Parameter with Method Introduce Parameter Object Classification 5/26/2011 33
Introduce Parameter Object Motivation 一組參數一起被傳遞 將一組參數組織再一起 [Allen] 解決 Add Parameter 的問題 優點: 減少參數量 找出部分程式碼可移到輸入的類別中 Example Classification 5/26/2011 34
Classification 5/26/2011 35
Classification 5/26/2011 36
Classification 5/26/2011 37
Classification 5/26/2011 38 [Allen]  直接存取物件內的資料 _start _end
Private Function Remove Setting Method Hide Method Classification 5/26/2011 39
Hide Method Motivation 類別中某一函式從來沒有被其他類別使用 利用IDE工具找出是否有被外部程式Reference 若沒有則將其設為Private function
Constructor Remove Setting Method Replace Constructor with Factory Method Encapsulate Downcast Classification 5/26/2011 41
Remove Setting Method Motivation 物件中某欄位, 應該在初創時被設置, 然後不再改變 如果不希望再被改變, 則直接不提供 setXXX [Allen]  專用於Final 的變數 Example Classification 5/26/2011 42
Constructor Remove Setting Method Replace Constructor with Factory Method Encapsulate Downcast Classification 5/26/2011 43
Replace Constructor with Factory Method Motivation 當需要使用 Type Code 創建建構式 使用Factory Method實做建構式 [Allen] 依照不同Type有不同行為時 Example * 3 Classification 5/26/2011 44
Classification 5/26/2011 45 ,[object Object],[object Object],[object Object],[object Object]
Encapsulate Downcast Motivation 優點: 將轉型動作封裝 [Allen] 避免外部程式自行轉型 Example Classification 5/26/2011 49
Classification 5/26/2011 50
Exception Replace Error Code with Exception Replace Exception with Test Classification 5/26/2011 51
Replace Error Code with Exception Motivation [Allen] 與Replace Error Code with Exception相反 優點: 將異常與一般程式分開 Example Classification 5/26/2011 52 平行
Classification 5/26/2011 53 說明清楚
Overview Exception Replace Error Code with Exception Replace Exception with Test Classification 5/26/2011 54
Replace Exception with Test Motivation [Allen] 與Replace Error Code with Exception相反 避免Exception濫用 呼叫函式前先檢查必要條件 Example Classification 5/26/2011 55
Classification 5/26/2011 56
Classification 5/26/2011 57
Classification 5/26/2011 58
Summary Classification 5/26/2011 59
Thank  YOU ! Classification 5/26/2011 60
重構—改善既有程式的設計(chapter 10)
重構—改善既有程式的設計(chapter 10)
重構—改善既有程式的設計(chapter 10)

Mais conteúdo relacionado

Mais procurados

Mais procurados (20)

Spring AOP
Spring AOPSpring AOP
Spring AOP
 
ADVANCE SQL-"Sub queries"
ADVANCE SQL-"Sub queries"ADVANCE SQL-"Sub queries"
ADVANCE SQL-"Sub queries"
 
An introduction to apex code test methods developer.force
An introduction to apex code test methods   developer.forceAn introduction to apex code test methods   developer.force
An introduction to apex code test methods developer.force
 
e computer notes - Subqueries
e computer notes - Subqueriese computer notes - Subqueries
e computer notes - Subqueries
 
Spring framework part 2
Spring framework  part 2Spring framework  part 2
Spring framework part 2
 
QTP&UFT Automation Framework
QTP&UFT Automation FrameworkQTP&UFT Automation Framework
QTP&UFT Automation Framework
 
Procedures/functions of rdbms
Procedures/functions of rdbmsProcedures/functions of rdbms
Procedures/functions of rdbms
 
Effective Readable unit testing with junit5
Effective Readable unit testing with junit5Effective Readable unit testing with junit5
Effective Readable unit testing with junit5
 
Function & procedure
Function & procedureFunction & procedure
Function & procedure
 
Qtp Basics
Qtp BasicsQtp Basics
Qtp Basics
 
Plsql
PlsqlPlsql
Plsql
 
Apex Testing and Best Practices
Apex Testing and Best PracticesApex Testing and Best Practices
Apex Testing and Best Practices
 
Oracle: Procedures
Oracle: ProceduresOracle: Procedures
Oracle: Procedures
 
Stored procedure
Stored procedureStored procedure
Stored procedure
 
Ppt Qtp
Ppt QtpPpt Qtp
Ppt Qtp
 
Introduction to Everit Component Registry - B Zsoldos
Introduction to Everit Component Registry - B ZsoldosIntroduction to Everit Component Registry - B Zsoldos
Introduction to Everit Component Registry - B Zsoldos
 
Keyword Driven Automation
Keyword Driven AutomationKeyword Driven Automation
Keyword Driven Automation
 
Functions
FunctionsFunctions
Functions
 
Function Parameters
Function ParametersFunction Parameters
Function Parameters
 
Performance tuning
Performance tuningPerformance tuning
Performance tuning
 

Semelhante a 重構—改善既有程式的設計(chapter 10)

pl/sql Procedure
pl/sql Procedurepl/sql Procedure
pl/sql ProcedurePooja Dixit
 
Rules. Обзор, примеры, API.
Rules. Обзор, примеры, API.Rules. Обзор, примеры, API.
Rules. Обзор, примеры, API.DrupalForumZP2012
 
05 Creating Stored Procedures
05 Creating Stored Procedures05 Creating Stored Procedures
05 Creating Stored Proceduresrehaniltifat
 
Automation with bpt methodology
Automation with bpt methodologyAutomation with bpt methodology
Automation with bpt methodologyGopi Nath
 
object oriented system analysis and design
object oriented system analysis and designobject oriented system analysis and design
object oriented system analysis and designwekineheshete
 
Spring framework in depth
Spring framework in depthSpring framework in depth
Spring framework in depthVinay Kumar
 
Formal Specification in Software Engineering SE9
Formal Specification in Software Engineering SE9Formal Specification in Software Engineering SE9
Formal Specification in Software Engineering SE9koolkampus
 
Query Management system-Iv review
Query Management system-Iv reviewQuery Management system-Iv review
Query Management system-Iv reviewlogeshprabu
 
Enterprise Library 3.0 Policy Injection Applicatoin Block
Enterprise Library 3.0 Policy Injection Applicatoin BlockEnterprise Library 3.0 Policy Injection Applicatoin Block
Enterprise Library 3.0 Policy Injection Applicatoin Blockmcgurk
 
Testware Hierarchy for Test Automation
Testware Hierarchy for Test AutomationTestware Hierarchy for Test Automation
Testware Hierarchy for Test AutomationGregory Solovey
 
Code Refactoring - 3.0
Code Refactoring - 3.0Code Refactoring - 3.0
Code Refactoring - 3.0Arul Prakash
 
TDD And Refactoring
TDD And RefactoringTDD And Refactoring
TDD And RefactoringNaresh Jain
 
ASP.NET MVC controllers
ASP.NET MVC controllersASP.NET MVC controllers
ASP.NET MVC controllersMahmoud Tolba
 
Software test management overview for managers
Software test management overview for managersSoftware test management overview for managers
Software test management overview for managersTJamesLeDoux
 

Semelhante a 重構—改善既有程式的設計(chapter 10) (19)

pl/sql Procedure
pl/sql Procedurepl/sql Procedure
pl/sql Procedure
 
Rules. Обзор, примеры, API.
Rules. Обзор, примеры, API.Rules. Обзор, примеры, API.
Rules. Обзор, примеры, API.
 
Stored procedures
Stored proceduresStored procedures
Stored procedures
 
05 Creating Stored Procedures
05 Creating Stored Procedures05 Creating Stored Procedures
05 Creating Stored Procedures
 
Ch10
Ch10Ch10
Ch10
 
Junit4.0
Junit4.0Junit4.0
Junit4.0
 
Automation with bpt methodology
Automation with bpt methodologyAutomation with bpt methodology
Automation with bpt methodology
 
object oriented system analysis and design
object oriented system analysis and designobject oriented system analysis and design
object oriented system analysis and design
 
Spring framework in depth
Spring framework in depthSpring framework in depth
Spring framework in depth
 
Formal Specification in Software Engineering SE9
Formal Specification in Software Engineering SE9Formal Specification in Software Engineering SE9
Formal Specification in Software Engineering SE9
 
Query Management system-Iv review
Query Management system-Iv reviewQuery Management system-Iv review
Query Management system-Iv review
 
Enterprise Library 3.0 Policy Injection Applicatoin Block
Enterprise Library 3.0 Policy Injection Applicatoin BlockEnterprise Library 3.0 Policy Injection Applicatoin Block
Enterprise Library 3.0 Policy Injection Applicatoin Block
 
Testware Hierarchy for Test Automation
Testware Hierarchy for Test AutomationTestware Hierarchy for Test Automation
Testware Hierarchy for Test Automation
 
Oracle_Procurement_Cloud_Release_8_Whats_New
Oracle_Procurement_Cloud_Release_8_Whats_NewOracle_Procurement_Cloud_Release_8_Whats_New
Oracle_Procurement_Cloud_Release_8_Whats_New
 
Code Refactoring - 3.0
Code Refactoring - 3.0Code Refactoring - 3.0
Code Refactoring - 3.0
 
TDD And Refactoring
TDD And RefactoringTDD And Refactoring
TDD And Refactoring
 
ASP.NET MVC controllers
ASP.NET MVC controllersASP.NET MVC controllers
ASP.NET MVC controllers
 
Software test management overview for managers
Software test management overview for managersSoftware test management overview for managers
Software test management overview for managers
 
plsql les01
 plsql les01 plsql les01
plsql les01
 

Mais de Chris Huang

Data compression, data security, and machine learning
Data compression, data security, and machine learningData compression, data security, and machine learning
Data compression, data security, and machine learningChris Huang
 
Kks sre book_ch10
Kks sre book_ch10Kks sre book_ch10
Kks sre book_ch10Chris Huang
 
Kks sre book_ch1,2
Kks sre book_ch1,2Kks sre book_ch1,2
Kks sre book_ch1,2Chris Huang
 
Real time big data applications with hadoop ecosystem
Real time big data applications with hadoop ecosystemReal time big data applications with hadoop ecosystem
Real time big data applications with hadoop ecosystemChris Huang
 
A Graph Service for Global Web Entities Traversal and Reputation Evaluation B...
A Graph Service for Global Web Entities Traversal and Reputation Evaluation B...A Graph Service for Global Web Entities Traversal and Reputation Evaluation B...
A Graph Service for Global Web Entities Traversal and Reputation Evaluation B...Chris Huang
 
Approaching real-time-hadoop
Approaching real-time-hadoopApproaching real-time-hadoop
Approaching real-time-hadoopChris Huang
 
20130310 solr tuorial
20130310 solr tuorial20130310 solr tuorial
20130310 solr tuorialChris Huang
 
Scaling big-data-mining-infra2
Scaling big-data-mining-infra2Scaling big-data-mining-infra2
Scaling big-data-mining-infra2Chris Huang
 
Applying Media Content Analysis to the Production of Musical Videos as Summar...
Applying Media Content Analysis to the Production of Musical Videos as Summar...Applying Media Content Analysis to the Production of Musical Videos as Summar...
Applying Media Content Analysis to the Production of Musical Videos as Summar...Chris Huang
 
Hbase status quo apache-con europe - nov 2012
Hbase status quo   apache-con europe - nov 2012Hbase status quo   apache-con europe - nov 2012
Hbase status quo apache-con europe - nov 2012Chris Huang
 
Hbase schema design and sizing apache-con europe - nov 2012
Hbase schema design and sizing   apache-con europe - nov 2012Hbase schema design and sizing   apache-con europe - nov 2012
Hbase schema design and sizing apache-con europe - nov 2012Chris Huang
 
重構—改善既有程式的設計(chapter 12,13)
重構—改善既有程式的設計(chapter 12,13)重構—改善既有程式的設計(chapter 12,13)
重構—改善既有程式的設計(chapter 12,13)Chris Huang
 
重構—改善既有程式的設計(chapter 9)
重構—改善既有程式的設計(chapter 9)重構—改善既有程式的設計(chapter 9)
重構—改善既有程式的設計(chapter 9)Chris Huang
 
重構—改善既有程式的設計(chapter 8)part 2
重構—改善既有程式的設計(chapter 8)part 2重構—改善既有程式的設計(chapter 8)part 2
重構—改善既有程式的設計(chapter 8)part 2Chris Huang
 
重構—改善既有程式的設計(chapter 8)part 1
重構—改善既有程式的設計(chapter 8)part 1重構—改善既有程式的設計(chapter 8)part 1
重構—改善既有程式的設計(chapter 8)part 1Chris Huang
 
重構—改善既有程式的設計(chapter 7)
重構—改善既有程式的設計(chapter 7)重構—改善既有程式的設計(chapter 7)
重構—改善既有程式的設計(chapter 7)Chris Huang
 
重構—改善既有程式的設計(chapter 4,5)
重構—改善既有程式的設計(chapter 4,5)重構—改善既有程式的設計(chapter 4,5)
重構—改善既有程式的設計(chapter 4,5)Chris Huang
 
重構—改善既有程式的設計(chapter 2,3)
重構—改善既有程式的設計(chapter 2,3)重構—改善既有程式的設計(chapter 2,3)
重構—改善既有程式的設計(chapter 2,3)Chris Huang
 
重構—改善既有程式的設計(chapter 1)
重構—改善既有程式的設計(chapter 1)重構—改善既有程式的設計(chapter 1)
重構—改善既有程式的設計(chapter 1)Chris Huang
 

Mais de Chris Huang (20)

Data compression, data security, and machine learning
Data compression, data security, and machine learningData compression, data security, and machine learning
Data compression, data security, and machine learning
 
Kks sre book_ch10
Kks sre book_ch10Kks sre book_ch10
Kks sre book_ch10
 
Kks sre book_ch1,2
Kks sre book_ch1,2Kks sre book_ch1,2
Kks sre book_ch1,2
 
Real time big data applications with hadoop ecosystem
Real time big data applications with hadoop ecosystemReal time big data applications with hadoop ecosystem
Real time big data applications with hadoop ecosystem
 
A Graph Service for Global Web Entities Traversal and Reputation Evaluation B...
A Graph Service for Global Web Entities Traversal and Reputation Evaluation B...A Graph Service for Global Web Entities Traversal and Reputation Evaluation B...
A Graph Service for Global Web Entities Traversal and Reputation Evaluation B...
 
Approaching real-time-hadoop
Approaching real-time-hadoopApproaching real-time-hadoop
Approaching real-time-hadoop
 
20130310 solr tuorial
20130310 solr tuorial20130310 solr tuorial
20130310 solr tuorial
 
Scaling big-data-mining-infra2
Scaling big-data-mining-infra2Scaling big-data-mining-infra2
Scaling big-data-mining-infra2
 
Applying Media Content Analysis to the Production of Musical Videos as Summar...
Applying Media Content Analysis to the Production of Musical Videos as Summar...Applying Media Content Analysis to the Production of Musical Videos as Summar...
Applying Media Content Analysis to the Production of Musical Videos as Summar...
 
Wissbi osdc pdf
Wissbi osdc pdfWissbi osdc pdf
Wissbi osdc pdf
 
Hbase status quo apache-con europe - nov 2012
Hbase status quo   apache-con europe - nov 2012Hbase status quo   apache-con europe - nov 2012
Hbase status quo apache-con europe - nov 2012
 
Hbase schema design and sizing apache-con europe - nov 2012
Hbase schema design and sizing   apache-con europe - nov 2012Hbase schema design and sizing   apache-con europe - nov 2012
Hbase schema design and sizing apache-con europe - nov 2012
 
重構—改善既有程式的設計(chapter 12,13)
重構—改善既有程式的設計(chapter 12,13)重構—改善既有程式的設計(chapter 12,13)
重構—改善既有程式的設計(chapter 12,13)
 
重構—改善既有程式的設計(chapter 9)
重構—改善既有程式的設計(chapter 9)重構—改善既有程式的設計(chapter 9)
重構—改善既有程式的設計(chapter 9)
 
重構—改善既有程式的設計(chapter 8)part 2
重構—改善既有程式的設計(chapter 8)part 2重構—改善既有程式的設計(chapter 8)part 2
重構—改善既有程式的設計(chapter 8)part 2
 
重構—改善既有程式的設計(chapter 8)part 1
重構—改善既有程式的設計(chapter 8)part 1重構—改善既有程式的設計(chapter 8)part 1
重構—改善既有程式的設計(chapter 8)part 1
 
重構—改善既有程式的設計(chapter 7)
重構—改善既有程式的設計(chapter 7)重構—改善既有程式的設計(chapter 7)
重構—改善既有程式的設計(chapter 7)
 
重構—改善既有程式的設計(chapter 4,5)
重構—改善既有程式的設計(chapter 4,5)重構—改善既有程式的設計(chapter 4,5)
重構—改善既有程式的設計(chapter 4,5)
 
重構—改善既有程式的設計(chapter 2,3)
重構—改善既有程式的設計(chapter 2,3)重構—改善既有程式的設計(chapter 2,3)
重構—改善既有程式的設計(chapter 2,3)
 
重構—改善既有程式的設計(chapter 1)
重構—改善既有程式的設計(chapter 1)重構—改善既有程式的設計(chapter 1)
重構—改善既有程式的設計(chapter 1)
 

Último

MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 

Último (20)

MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 

重構—改善既有程式的設計(chapter 10)

  • 1. Classification 5/26/2011 1 Refactoring10 – Making Method Calls Simpler Allen Chien 2011-05-15
  • 2. Agenda Function Name Function Parameter Private Function Constructor Exception private example(intparam) Classification 5/26/2011 2
  • 3. Function Name Rename Method Classification 5/26/2011 3
  • 4. Function Parameters Add Parameters Remove Parameters Separate Query From Modifier Parameterize Method Replace Parameter with Explicit Method Preserve Whole Object Replace Parameter with Method Introduce Parameter Object Classification 5/26/2011 4
  • 5. Private Function Hide Method Classification 5/26/2011 5
  • 6. Constructor Remove Setting Method Replace Constructor with Factory Method Encapsulate Downcast Classification 5/26/2011 6
  • 7. Exception Replace Error Code with Exception Replace Exception with Test Classification 5/26/2011 7
  • 8. Function Name Rename Method (忽略 “做法”) Classification 5/26/2011 8
  • 9. Rename Method Motivation 函式名稱應該準確表達它的用途 為函式寫上一句註釋, 然後為該註釋給予一個名稱 Example Classification 5/26/2011 9
  • 10. Function Parameters Add Parameters Remove Parameters Separate Query From Modifier Parameterize Method Replace Parameter with Explicit Method Preserve Whole Object Replace Parameter with Method Introduce Parameter Object Classification 5/26/2011 10
  • 11. Add Parameter Motivation 修改函式後, 需要增加資訊 壞味道 :Data Clumps (P81) 建議:Introduce Parameter Object (295) Classification 5/26/2011 11
  • 12. Function Parameters Add Parameters Remove Parameters Separate Query From Modifier Parameterize Method Replace Parameter with Explicit Method Preserve Whole Object Replace Parameter with Method Introduce Parameter Object Classification 5/26/2011 12
  • 13. Remove Parameter Motivation 修改函式後, 去除參數的重構 在多型的情況下, 需要檢查該函式是否已被其他程式實做 Classification 5/26/2011 13
  • 14. Function Parameters Add Parameters Remove Parameters Separate Query From Modifier Parameterize Method Replace Parameter with Explicit Method Preserve Whole Object Replace Parameter with Method Introduce Parameter Object Classification 5/26/2011 14
  • 15. Separate Query from Modifier Motivation 某個函式既回傳物件狀態值, 又修改物件狀態 (getXXX + setXXX) 既有返回值又有副作用就應該分離 (getXXX only) Meyer’s Rule: 任何有返回值的函式皆不應有副作用 優點: 增加重複查詢的效能 總是獲得相同的結果 [Allen] 函式名稱與期望結果一致 Example Classification 5/26/2011 15
  • 17. Classification 5/26/2011 17 Substitute Algorithm (139)
  • 18. Function Parameters Add Parameters Remove Parameters Separate Query From Modifier Parameterize Method Replace Parameter with Explicit Method Preserve Whole Object Replace Parameter with Method Introduce Parameter Object Classification 5/26/2011 18
  • 19. Parameterize Method Motivation 問題: 某個函式做類似的工作, 但是函式本體卻包含不同的值 方式 使用單一函式, 並以參數來表達不同的值 將少量數值視為參數, 找出重覆的程式碼 優點: 減少重複的程式碼 [Allen] 與 Replace Parameter with Explicit Method 相反 Example Classification 5/26/2011 19
  • 22. Function Parameters Add Parameters Remove Parameters Separate Query From Modifier Parameterize Method Replace Parameter with Explicit Method Preserve Whole Object Replace Parameter with Method Introduce Parameter Object Classification 5/26/2011 22
  • 23. Replace Parameter with Explicit Methods Motivation 與Parameterize Method 相反 條件: 離散取值 函式中以條件事檢查參數 [Allen] 將 IF-ELSE 或 SWITCH 移除 [Allen] Code 不一樣 優點: 避免出現條件式 利用編譯器檢查程式 介面清楚 使用參數時, 則須判斷參數合法性 [Allen] 如 switch之default Example Classification 5/26/2011 23
  • 25. Function Parameters Add Parameters Remove Parameters Separate Query From Modifier Parameterize Method Replace Parameter with Explicit Method Preserve Whole Object Replace Parameter with Method Introduce Parameter Object Classification 5/26/2011 25
  • 26. Preserve Whole Object Motivation 參數為某一物件中取出, 則將該物件當作輸入參數 優點: 避免新增修改參數項 減少參數數量, 方便其他程式呼叫使用 條件: 因依存關係導致結構惡化則不可使用 Example Classification 5/26/2011 26
  • 29. Function Parameters Add Parameters Remove Parameters Separate Query From Modifier Parameterize Method Replace Parameter with Explicit Method Preserve Whole Object Replace Parameter with Method Introduce Parameter Object Classification 5/26/2011 29
  • 30. Replace Parameter with Methods Motivation 物件換起某個函式, 並將所得結果作為參數, 傳遞給另一個函式 接受該參數的函式也可以喚起前一個函式 條件: 接收端是否可以透過計算而取得函數值 優點: 減少參數數量 使函式容易理解 Example Classification 5/26/2011 30
  • 32. Classification 5/26/2011 32 Inline Method (117)
  • 33. Function Parameters Add Parameters Remove Parameters Separate Query From Modifier Parameterize Method Replace Parameter with Explicit Method Preserve Whole Object Replace Parameter with Method Introduce Parameter Object Classification 5/26/2011 33
  • 34. Introduce Parameter Object Motivation 一組參數一起被傳遞 將一組參數組織再一起 [Allen] 解決 Add Parameter 的問題 優點: 減少參數量 找出部分程式碼可移到輸入的類別中 Example Classification 5/26/2011 34
  • 38. Classification 5/26/2011 38 [Allen] 直接存取物件內的資料 _start _end
  • 39. Private Function Remove Setting Method Hide Method Classification 5/26/2011 39
  • 40. Hide Method Motivation 類別中某一函式從來沒有被其他類別使用 利用IDE工具找出是否有被外部程式Reference 若沒有則將其設為Private function
  • 41. Constructor Remove Setting Method Replace Constructor with Factory Method Encapsulate Downcast Classification 5/26/2011 41
  • 42. Remove Setting Method Motivation 物件中某欄位, 應該在初創時被設置, 然後不再改變 如果不希望再被改變, 則直接不提供 setXXX [Allen] 專用於Final 的變數 Example Classification 5/26/2011 42
  • 43. Constructor Remove Setting Method Replace Constructor with Factory Method Encapsulate Downcast Classification 5/26/2011 43
  • 44. Replace Constructor with Factory Method Motivation 當需要使用 Type Code 創建建構式 使用Factory Method實做建構式 [Allen] 依照不同Type有不同行為時 Example * 3 Classification 5/26/2011 44
  • 45.
  • 46. Encapsulate Downcast Motivation 優點: 將轉型動作封裝 [Allen] 避免外部程式自行轉型 Example Classification 5/26/2011 49
  • 48. Exception Replace Error Code with Exception Replace Exception with Test Classification 5/26/2011 51
  • 49. Replace Error Code with Exception Motivation [Allen] 與Replace Error Code with Exception相反 優點: 將異常與一般程式分開 Example Classification 5/26/2011 52 平行
  • 51. Overview Exception Replace Error Code with Exception Replace Exception with Test Classification 5/26/2011 54
  • 52. Replace Exception with Test Motivation [Allen] 與Replace Error Code with Exception相反 避免Exception濫用 呼叫函式前先檢查必要條件 Example Classification 5/26/2011 55
  • 57. Thank YOU ! Classification 5/26/2011 60