SlideShare ist ein Scribd-Unternehmen logo
1 von 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)

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (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
 

Ähnlich wie 重構—改善既有程式的設計(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
 

Ähnlich wie 重構—改善既有程式的設計(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
 

Mehr von 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
 

Mehr von 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)
 

Kürzlich hochgeladen

Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxMaryGraceBautista27
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)cama23
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 

Kürzlich hochgeladen (20)

Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptx
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 

重構—改善既有程式的設計(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