SlideShare a Scribd company logo
1 of 33
Teddy Chen
teddy@teddysoft.tw
http://teddy-chen-tw.blogspot.tw
Mar. 28 2013
Copyright@2013 Teddysoft
3
系統不穩定
4
5
6
7
8
Problem
9
Problem
Copyright@2013 Teddysoft
• Correctness
– Contract Specification(超出範圍)
• Robustness
– Exception Handling
Copyright@2013 Teddysoft
Copyright@2013 Teddysoft
Copyright@2013 Teddysoft
RuntimeException
Throwable
Exception Error
Unchecked
Exception
Checked
Exception
Copyright@2013 Teddysoft
handle
declare
Copyright@2013 Teddysoft
16
EH smell Refactoring
Ignored checked
exception
Replace Ignored Checked Exception with
Unchecked Exception
Unprotected main
program
Avoid Unexpected Termination with Big
Outer Try Block
Dummy handler Replace Dummy Handler with Rethrow
Nested try block Replace Nested Try Block with Method
Ignored checked
exception,
Dummy handler
Introduce Checkpoint Class
Spare handler Introduce Resourceful Try Clause
Copyright@2013 Teddysoft
public void writeFile(String fileName, String data) {
Writer writer = null;
try {
writer = new FileWriter(fileName); /* may throw an IOException */
writer.write(data); /* may throw an IOException */
}
catch (IOException e) {
/* ignoring the exception */
}
}
↓
public void writeFile(String fileName, String data) {
Writer writer = null;
try {
writer = new FileWriter(fileName); /* may throw an IOException */
writer.write(data); /* may throw an IOException */
}
catch (IOException e) {
throw new UnhandledException(e, “message”);
}
}
17
Copyright@2013 Teddysoft
18
static public void main(String[] args) {
/* some code */
}
↓
static public void main(String[] args) {
try {
/* some code */
}
catch (Throwable e) {
/* displaying and/or logging the exception */
}
}
Copyright@2013 Teddysoft
19
catch (AnException e) {
e.printStackTrace();
}
↓
catch (AnException e) {
throw new UnhandledException(e, “message”);
}
Copyright@2013 Teddysoft
20
FileInputStream in = null;
try {
in = new FileInputStream(…);
} finally {
try { if (in != null) in.close (); }
catch (IOException e) { /* log the exception */ }
}
↓
FileInputStream in = null;
try {
in = new FileInputStream(…);
} finally { closeIO (in); }
private void closeIO (Closeable c) {
try {
if (c != null) c.close ();
} catch (IOException e) { /* log the exception */ }
}
Copyright@2013 Teddysoft
21
public void foo () throws FailureException {
try {
/* code that may change the state of the object */
}
catch (AnException e) {
throw new FailureException(e);
} finally {/* code for cleanup */}
}
↓
public void foo () throws FailureException {
Checkpoint cp = new Checkpoint (/* parameters */);
try {
cp. establish (); /* establish a checkpoint */
/* code that may change the state of the object */ }
catch (AnException e) {
cp.restore (); /* restore the checkpoint */
throw new FailureException(e); }
finally { cp.drop(); }
} Copyright@2013 Teddysoft
22
try { /* primary */ }
catch (SomeException e) {
try {/* alternative */}
catch(AnotherException e) {
throw new FailureException(e);
}
}
↓
int attempt = 0; int maxAttempt = 2; boolean retry = false;
do {
try { retry = false;
if (attempt == 0) { /* primary */ }
else { /* alternative */ }
} catch (SomeException e) {
attempt++; retry = true;
if (attempt > maxAttempt) throw new FailureException (e);
}
} while (attempt<= maxAttempt && retry)
Copyright@2013 Teddysoft
Copyright@2013 Teddysoft
• CSS is a banking application for checking
customers’ credit history in processing their
loan applications
24
Credit Scoring
System (CSS)
Gateway
(IBM Message
Queue)
Joint Credit
Information
Center (JCIC)
Authentication
(Domino
Server)
Other
Applications
Copyright@2013 Teddysoft
• The use of exceptions and return code were
mixed in an arbitrary manner
• Many checked exceptions were caught and
ignored
• Unchecked exceptions were not caught which
lead to an unexpected termination of the JSP
program
• Failure atomicity of each component was not
investigated
25
Copyright@2013 Teddysoft
• Bug fixing is a strong force drawing the
developer’s attention to deal with
exceptions
• Cost-effectiveness measurements can be
provided to convince the management to
support the refactoring
– Improving quality, saving maintenance cost, and
increasing customer satisfaction
26
Copyright@2013 Teddysoft
Reported
failure
Failing operation/ possible
reason
Failure
count
Current
restoration
actions
Restoration
time per
failure
Cannot
make a
connection
to Domino
server
User login/
Connections of Domino Internet
Inter-ORB Protocol (DIIOP) were
exhausted. It was likely that DIIOP
connections were not correctly
disposed.
125
1. Rebooting
Domino
server
2. Rebooting
CSS
About 20
minutes
MQ server
connection
is broken
Sending request to JCJS/
MQ connections were exhausted.
Perhaps they were not properly
released.
66
1. Rebooting
MQ Server
2. Rebooting
CSS
About 10
minutes
Cannot send
messages to
MQ server
Sending request to JCJS/
There were two possible reasons:
Message queue was full.
Gateway was offline due to
maintenance or crash.
10
1. Rebooting
MQ Server
2. Rebooting
CSS
About 10
minutes 27
28
Credit Scoring
System (CSS)
Gateway
(IBM Message
Queue)
Joint Credit
Information
Center (JCIC)
Authentication
(Domino
Server 1)
Other
Applications
Authentication
(Domino
Server 2)
X
Copyright@2013 Teddysoft
29
Reported
failure
Failure count from Sep. 2005
to Dec. 2005
Failure count from Sep. 2006
to Dec. 2006
Sep. Oct. Nov. Dec. Sep. Oct. Nov. Dec.
Cannot make a
connection to
Domino server
13 5 9 14 10 5 0 0
MQ Server
connection
broken
6 1 4 8 3/0 0 1/0 0
Cannot send
messages to
MQ Server
0 0 0 2 3/0 1/0 0 0
Copyright@2013 Teddysoft
30
Code type
Total vs.
modified code
Modification
percentage
Non-commented
line of code
14150/371 2.63%
Catch clauses 855/21 2.46%
Copyright@2013 Teddysoft
31
Task Man-hours Sum
Collecting bugs reports in 2005 10 18 (43.9%)
Refactoring cost (Code review) 7 (30.4%)
Refactoring cost (Exception Handling
goal analysis)
4 (17.4%)
Refactoring cost (Coding) 3 (13%)
Refactoring cost (Testing) 9 (39.2%)
Total man-hours in applying
refactoring
23 (56.1%)
Total man-hours in improving
robustness
41 (100%)
Copyright@2013 Teddysoft
32
Cost element Cost calculation
Maintenance cost in 2005 201 (failure) * 2 (hours) * 100 (US$) =
40,200 (US$)
Refactoring cost 41 (man-hours) * 100 (US$) = 4,100 (US$)
Maintenance cost saving in
the following year (money)
40,200 (US$) – 4,100 (US$) = 36,100 (US$)
Maintenance cost saving in
the following year
(percentage)
36,100 (US$) / 40,200 (US$) * 100 = 89.8%
Copyright@2013 Teddysoft
33Copyright@2013 Teddysoft
EH smell Refactoring
Ignored checked
exception
Replace Ignored Checked Exception with
Unchecked Exception
Unprotected main
program
Avoid Unexpected Termination with Big
Outer Try Block
Dummy handler Replace Dummy Handler with Rethrow
Nested try block Replace Nested Try Block with Method
Ignored checked
exception,
Dummy handler
Introduce Checkpoint Class
Spare handler Introduce Resourceful Try Clause

More Related Content

What's hot

C# conventions & good practices
C# conventions & good practicesC# conventions & good practices
C# conventions & good practicesTan Tran
 
Stop wasting-time-by-applying-clean-code-principles
Stop wasting-time-by-applying-clean-code-principlesStop wasting-time-by-applying-clean-code-principles
Stop wasting-time-by-applying-clean-code-principlesEdorian
 
Back-2-Basics: Code Contracts
Back-2-Basics: Code ContractsBack-2-Basics: Code Contracts
Back-2-Basics: Code ContractsDavid McCarter
 
Refactoring Applications using SOLID Principles
Refactoring Applications using SOLID PrinciplesRefactoring Applications using SOLID Principles
Refactoring Applications using SOLID PrinciplesSteven Smith
 
JIOWA Code Generation Framework & Template Engine
JIOWA Code Generation Framework & Template EngineJIOWA Code Generation Framework & Template Engine
JIOWA Code Generation Framework & Template EngineRobert Mencl
 
The WHY behind TDD/BDD and the HOW with RSpec
The WHY behind TDD/BDD and the HOW with RSpecThe WHY behind TDD/BDD and the HOW with RSpec
The WHY behind TDD/BDD and the HOW with RSpecBen Mabey
 
Returning the right results - Jettro Coenradie
Returning the right results - Jettro CoenradieReturning the right results - Jettro Coenradie
Returning the right results - Jettro CoenradieNLJUG
 
從Bowling Game Kata看敏捷開發
從Bowling Game Kata看敏捷開發從Bowling Game Kata看敏捷開發
從Bowling Game Kata看敏捷開發teddysoft
 

What's hot (11)

C# conventions & good practices
C# conventions & good practicesC# conventions & good practices
C# conventions & good practices
 
Stop wasting-time-by-applying-clean-code-principles
Stop wasting-time-by-applying-clean-code-principlesStop wasting-time-by-applying-clean-code-principles
Stop wasting-time-by-applying-clean-code-principles
 
Back-2-Basics: Code Contracts
Back-2-Basics: Code ContractsBack-2-Basics: Code Contracts
Back-2-Basics: Code Contracts
 
Refactoring Applications using SOLID Principles
Refactoring Applications using SOLID PrinciplesRefactoring Applications using SOLID Principles
Refactoring Applications using SOLID Principles
 
JIOWA Code Generation Framework & Template Engine
JIOWA Code Generation Framework & Template EngineJIOWA Code Generation Framework & Template Engine
JIOWA Code Generation Framework & Template Engine
 
The WHY behind TDD/BDD and the HOW with RSpec
The WHY behind TDD/BDD and the HOW with RSpecThe WHY behind TDD/BDD and the HOW with RSpec
The WHY behind TDD/BDD and the HOW with RSpec
 
Clean Code 2
Clean Code 2Clean Code 2
Clean Code 2
 
Returning the right results - Jettro Coenradie
Returning the right results - Jettro CoenradieReturning the right results - Jettro Coenradie
Returning the right results - Jettro Coenradie
 
TDD with PhpSpec
TDD with PhpSpecTDD with PhpSpec
TDD with PhpSpec
 
從Bowling Game Kata看敏捷開發
從Bowling Game Kata看敏捷開發從Bowling Game Kata看敏捷開發
從Bowling Game Kata看敏捷開發
 
Refactoring
RefactoringRefactoring
Refactoring
 

Viewers also liked

搞懂Java例外處理的難題:Checked與Unchecked Exceptions不再是問題
搞懂Java例外處理的難題:Checked與Unchecked Exceptions不再是問題搞懂Java例外處理的難題:Checked與Unchecked Exceptions不再是問題
搞懂Java例外處理的難題:Checked與Unchecked Exceptions不再是問題teddysoft
 
模式入門第一堂課: 30分鐘寫出一個模式
模式入門第一堂課: 30分鐘寫出一個模式模式入門第一堂課: 30分鐘寫出一個模式
模式入門第一堂課: 30分鐘寫出一個模式teddysoft
 
Design Patterns這樣學就會了:入門班 Day1 教材
Design Patterns這樣學就會了:入門班 Day1 教材Design Patterns這樣學就會了:入門班 Day1 教材
Design Patterns這樣學就會了:入門班 Day1 教材teddysoft
 
重構三兩事
重構三兩事重構三兩事
重構三兩事teddysoft
 
好設計如何好 @ C.C. Agile #14
好設計如何好 @ C.C. Agile #14好設計如何好 @ C.C. Agile #14
好設計如何好 @ C.C. Agile #14teddysoft
 
從五個小故事看敏捷開發精神
從五個小故事看敏捷開發精神從五個小故事看敏捷開發精神
從五個小故事看敏捷開發精神teddysoft
 
那一夜我們說Pattern design patterns 20周年-published
那一夜我們說Pattern design patterns 20周年-published那一夜我們說Pattern design patterns 20周年-published
那一夜我們說Pattern design patterns 20周年-publishedteddysoft
 
了解模式讓你更敏捷 (C C Agile 活動分享)
了解模式讓你更敏捷 (C C Agile 活動分享)了解模式讓你更敏捷 (C C Agile 活動分享)
了解模式讓你更敏捷 (C C Agile 活動分享)teddysoft
 
[演講] Scrum導入經驗分享
[演講] Scrum導入經驗分享[演講] Scrum導入經驗分享
[演講] Scrum導入經驗分享teddysoft
 
軟體開發成功的秘訣
軟體開發成功的秘訣軟體開發成功的秘訣
軟體開發成功的秘訣teddysoft
 
Kanban in Oracle Applications
Kanban in Oracle ApplicationsKanban in Oracle Applications
Kanban in Oracle Applicationsmgarg82
 
HowTo Design your kanban board
HowTo Design your kanban boardHowTo Design your kanban board
HowTo Design your kanban boardJo Seibert
 
Specification by Example
Specification by ExampleSpecification by Example
Specification by ExampleDeclan Whelan
 
O2 c and p2p cycles
O2 c and p2p cyclesO2 c and p2p cycles
O2 c and p2p cyclesgsriramsunil
 
Kanban Board Examples
Kanban Board ExamplesKanban Board Examples
Kanban Board ExamplesShore Labs
 
Kanban boards step by step
Kanban boards step by stepKanban boards step by step
Kanban boards step by stepGiulio Roggero
 
[DSC 2016] 系列活動:李宏毅 / 一天搞懂深度學習
[DSC 2016] 系列活動:李宏毅 / 一天搞懂深度學習[DSC 2016] 系列活動:李宏毅 / 一天搞懂深度學習
[DSC 2016] 系列活動:李宏毅 / 一天搞懂深度學習台灣資料科學年會
 

Viewers also liked (17)

搞懂Java例外處理的難題:Checked與Unchecked Exceptions不再是問題
搞懂Java例外處理的難題:Checked與Unchecked Exceptions不再是問題搞懂Java例外處理的難題:Checked與Unchecked Exceptions不再是問題
搞懂Java例外處理的難題:Checked與Unchecked Exceptions不再是問題
 
模式入門第一堂課: 30分鐘寫出一個模式
模式入門第一堂課: 30分鐘寫出一個模式模式入門第一堂課: 30分鐘寫出一個模式
模式入門第一堂課: 30分鐘寫出一個模式
 
Design Patterns這樣學就會了:入門班 Day1 教材
Design Patterns這樣學就會了:入門班 Day1 教材Design Patterns這樣學就會了:入門班 Day1 教材
Design Patterns這樣學就會了:入門班 Day1 教材
 
重構三兩事
重構三兩事重構三兩事
重構三兩事
 
好設計如何好 @ C.C. Agile #14
好設計如何好 @ C.C. Agile #14好設計如何好 @ C.C. Agile #14
好設計如何好 @ C.C. Agile #14
 
從五個小故事看敏捷開發精神
從五個小故事看敏捷開發精神從五個小故事看敏捷開發精神
從五個小故事看敏捷開發精神
 
那一夜我們說Pattern design patterns 20周年-published
那一夜我們說Pattern design patterns 20周年-published那一夜我們說Pattern design patterns 20周年-published
那一夜我們說Pattern design patterns 20周年-published
 
了解模式讓你更敏捷 (C C Agile 活動分享)
了解模式讓你更敏捷 (C C Agile 活動分享)了解模式讓你更敏捷 (C C Agile 活動分享)
了解模式讓你更敏捷 (C C Agile 活動分享)
 
[演講] Scrum導入經驗分享
[演講] Scrum導入經驗分享[演講] Scrum導入經驗分享
[演講] Scrum導入經驗分享
 
軟體開發成功的秘訣
軟體開發成功的秘訣軟體開發成功的秘訣
軟體開發成功的秘訣
 
Kanban in Oracle Applications
Kanban in Oracle ApplicationsKanban in Oracle Applications
Kanban in Oracle Applications
 
HowTo Design your kanban board
HowTo Design your kanban boardHowTo Design your kanban board
HowTo Design your kanban board
 
Specification by Example
Specification by ExampleSpecification by Example
Specification by Example
 
O2 c and p2p cycles
O2 c and p2p cyclesO2 c and p2p cycles
O2 c and p2p cycles
 
Kanban Board Examples
Kanban Board ExamplesKanban Board Examples
Kanban Board Examples
 
Kanban boards step by step
Kanban boards step by stepKanban boards step by step
Kanban boards step by step
 
[DSC 2016] 系列活動:李宏毅 / 一天搞懂深度學習
[DSC 2016] 系列活動:李宏毅 / 一天搞懂深度學習[DSC 2016] 系列活動:李宏毅 / 一天搞懂深度學習
[DSC 2016] 系列活動:李宏毅 / 一天搞懂深度學習
 

Similar to Java 例外處理壞味道與重構技術

Navigating the xDD Alphabet Soup
Navigating the xDD Alphabet SoupNavigating the xDD Alphabet Soup
Navigating the xDD Alphabet SoupDror Helper
 
Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)
Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)
Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)David Gómez García
 
General Principles of Web Security
General Principles of Web SecurityGeneral Principles of Web Security
General Principles of Web Securityjemond
 
Testing in android
Testing in androidTesting in android
Testing in androidjtrindade
 
Networking and Data Access with Eqela
Networking and Data Access with EqelaNetworking and Data Access with Eqela
Networking and Data Access with Eqelajobandesther
 
[2012 02 03]clean_code 4장
[2012 02 03]clean_code 4장[2012 02 03]clean_code 4장
[2012 02 03]clean_code 4장Jong Pil Won
 
[xp2013] Narrow Down What to Test
[xp2013] Narrow Down What to Test[xp2013] Narrow Down What to Test
[xp2013] Narrow Down What to TestZsolt Fabok
 
Do I need tests when I have the compiler - Andrzej Jóźwiak - TomTom Dev Day 2020
Do I need tests when I have the compiler - Andrzej Jóźwiak - TomTom Dev Day 2020Do I need tests when I have the compiler - Andrzej Jóźwiak - TomTom Dev Day 2020
Do I need tests when I have the compiler - Andrzej Jóźwiak - TomTom Dev Day 2020Andrzej Jóźwiak
 
Grails 1.1 Testing - Unit, Integration & Functional
Grails 1.1 Testing - Unit, Integration & FunctionalGrails 1.1 Testing - Unit, Integration & Functional
Grails 1.1 Testing - Unit, Integration & Functional2Paths Solutions Ltd.
 
Stopping the Rot - Putting Legacy C++ Under Test
Stopping the Rot - Putting Legacy C++ Under TestStopping the Rot - Putting Legacy C++ Under Test
Stopping the Rot - Putting Legacy C++ Under TestSeb Rose
 
2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good TestsTomek Kaczanowski
 
Mockito with a hint of PowerMock
Mockito with a hint of PowerMockMockito with a hint of PowerMock
Mockito with a hint of PowerMockYing Zhang
 
1z0 804 exam-java se 7 programmer ii
1z0 804 exam-java se 7 programmer ii1z0 804 exam-java se 7 programmer ii
1z0 804 exam-java se 7 programmer iiIsabella789
 
33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good TestsTomek Kaczanowski
 

Similar to Java 例外處理壞味道與重構技術 (20)

Getting Started With Testing
Getting Started With TestingGetting Started With Testing
Getting Started With Testing
 
Navigating the xDD Alphabet Soup
Navigating the xDD Alphabet SoupNavigating the xDD Alphabet Soup
Navigating the xDD Alphabet Soup
 
Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)
Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)
Midiendo la calidad de código en WTF/Min (Revisado EUI Abril 2014)
 
General Principles of Web Security
General Principles of Web SecurityGeneral Principles of Web Security
General Principles of Web Security
 
Testing in android
Testing in androidTesting in android
Testing in android
 
Networking and Data Access with Eqela
Networking and Data Access with EqelaNetworking and Data Access with Eqela
Networking and Data Access with Eqela
 
[2012 02 03]clean_code 4장
[2012 02 03]clean_code 4장[2012 02 03]clean_code 4장
[2012 02 03]clean_code 4장
 
[xp2013] Narrow Down What to Test
[xp2013] Narrow Down What to Test[xp2013] Narrow Down What to Test
[xp2013] Narrow Down What to Test
 
Do I need tests when I have the compiler - Andrzej Jóźwiak - TomTom Dev Day 2020
Do I need tests when I have the compiler - Andrzej Jóźwiak - TomTom Dev Day 2020Do I need tests when I have the compiler - Andrzej Jóźwiak - TomTom Dev Day 2020
Do I need tests when I have the compiler - Andrzej Jóźwiak - TomTom Dev Day 2020
 
Grails 1.1 Testing - Unit, Integration & Functional
Grails 1.1 Testing - Unit, Integration & FunctionalGrails 1.1 Testing - Unit, Integration & Functional
Grails 1.1 Testing - Unit, Integration & Functional
 
Clean code
Clean codeClean code
Clean code
 
Stopping the Rot - Putting Legacy C++ Under Test
Stopping the Rot - Putting Legacy C++ Under TestStopping the Rot - Putting Legacy C++ Under Test
Stopping the Rot - Putting Legacy C++ Under Test
 
00_Introduction to Java.ppt
00_Introduction to Java.ppt00_Introduction to Java.ppt
00_Introduction to Java.ppt
 
2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests
 
Mockito with a hint of PowerMock
Mockito with a hint of PowerMockMockito with a hint of PowerMock
Mockito with a hint of PowerMock
 
Java Concurrency
Java ConcurrencyJava Concurrency
Java Concurrency
 
Devoxx 2012 (v2)
Devoxx 2012 (v2)Devoxx 2012 (v2)
Devoxx 2012 (v2)
 
Unit testing - A&BP CC
Unit testing - A&BP CCUnit testing - A&BP CC
Unit testing - A&BP CC
 
1z0 804 exam-java se 7 programmer ii
1z0 804 exam-java se 7 programmer ii1z0 804 exam-java se 7 programmer ii
1z0 804 exam-java se 7 programmer ii
 
33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests
 

More from teddysoft

Dci vs aggregate_dddtw_2021-0.3-16-9
Dci vs aggregate_dddtw_2021-0.3-16-9Dci vs aggregate_dddtw_2021-0.3-16-9
Dci vs aggregate_dddtw_2021-0.3-16-9teddysoft
 
Dci vs aggregate_dddtw_2021-0.3-preview
Dci vs aggregate_dddtw_2021-0.3-previewDci vs aggregate_dddtw_2021-0.3-preview
Dci vs aggregate_dddtw_2021-0.3-previewteddysoft
 
DDD + Clean Architecture: 從需求到實作
DDD + Clean Architecture: 從需求到實作DDD + Clean Architecture: 從需求到實作
DDD + Clean Architecture: 從需求到實作teddysoft
 
漫談重構
漫談重構漫談重構
漫談重構teddysoft
 
Pattern based problem solving-published
Pattern based problem solving-publishedPattern based problem solving-published
Pattern based problem solving-publishedteddysoft
 
Agile the timeless way of software development-2019-05-17-v1.2-published
Agile the timeless way of software development-2019-05-17-v1.2-publishedAgile the timeless way of software development-2019-05-17-v1.2-published
Agile the timeless way of software development-2019-05-17-v1.2-publishedteddysoft
 
當Scrum遇到Pattern
當Scrum遇到Pattern當Scrum遇到Pattern
當Scrum遇到Patternteddysoft
 
說出一嘴好設計 1.1
說出一嘴好設計 1.1說出一嘴好設計 1.1
說出一嘴好設計 1.1teddysoft
 
跟著Teddy讀Pattern
跟著Teddy讀Pattern跟著Teddy讀Pattern
跟著Teddy讀Patternteddysoft
 
洗白你的軟體架構
洗白你的軟體架構洗白你的軟體架構
洗白你的軟體架構teddysoft
 
如何學好設計模式
如何學好設計模式如何學好設計模式
如何學好設計模式teddysoft
 
Bdd atdd sbe_tdd_ddd_published
Bdd atdd sbe_tdd_ddd_publishedBdd atdd sbe_tdd_ddd_published
Bdd atdd sbe_tdd_ddd_publishedteddysoft
 

More from teddysoft (12)

Dci vs aggregate_dddtw_2021-0.3-16-9
Dci vs aggregate_dddtw_2021-0.3-16-9Dci vs aggregate_dddtw_2021-0.3-16-9
Dci vs aggregate_dddtw_2021-0.3-16-9
 
Dci vs aggregate_dddtw_2021-0.3-preview
Dci vs aggregate_dddtw_2021-0.3-previewDci vs aggregate_dddtw_2021-0.3-preview
Dci vs aggregate_dddtw_2021-0.3-preview
 
DDD + Clean Architecture: 從需求到實作
DDD + Clean Architecture: 從需求到實作DDD + Clean Architecture: 從需求到實作
DDD + Clean Architecture: 從需求到實作
 
漫談重構
漫談重構漫談重構
漫談重構
 
Pattern based problem solving-published
Pattern based problem solving-publishedPattern based problem solving-published
Pattern based problem solving-published
 
Agile the timeless way of software development-2019-05-17-v1.2-published
Agile the timeless way of software development-2019-05-17-v1.2-publishedAgile the timeless way of software development-2019-05-17-v1.2-published
Agile the timeless way of software development-2019-05-17-v1.2-published
 
當Scrum遇到Pattern
當Scrum遇到Pattern當Scrum遇到Pattern
當Scrum遇到Pattern
 
說出一嘴好設計 1.1
說出一嘴好設計 1.1說出一嘴好設計 1.1
說出一嘴好設計 1.1
 
跟著Teddy讀Pattern
跟著Teddy讀Pattern跟著Teddy讀Pattern
跟著Teddy讀Pattern
 
洗白你的軟體架構
洗白你的軟體架構洗白你的軟體架構
洗白你的軟體架構
 
如何學好設計模式
如何學好設計模式如何學好設計模式
如何學好設計模式
 
Bdd atdd sbe_tdd_ddd_published
Bdd atdd sbe_tdd_ddd_publishedBdd atdd sbe_tdd_ddd_published
Bdd atdd sbe_tdd_ddd_published
 

Recently uploaded

Kindergarten Assessment Questions Via LessonUp
Kindergarten Assessment Questions Via LessonUpKindergarten Assessment Questions Via LessonUp
Kindergarten Assessment Questions Via LessonUpmainac1
 
Passbook project document_april_21__.pdf
Passbook project document_april_21__.pdfPassbook project document_april_21__.pdf
Passbook project document_april_21__.pdfvaibhavkanaujia
 
WAEC Carpentry and Joinery Past Questions
WAEC Carpentry and Joinery Past QuestionsWAEC Carpentry and Joinery Past Questions
WAEC Carpentry and Joinery Past QuestionsCharles Obaleagbon
 
办理学位证(SFU证书)西蒙菲莎大学毕业证成绩单原版一比一
办理学位证(SFU证书)西蒙菲莎大学毕业证成绩单原版一比一办理学位证(SFU证书)西蒙菲莎大学毕业证成绩单原版一比一
办理学位证(SFU证书)西蒙菲莎大学毕业证成绩单原版一比一F dds
 
Bus tracking.pptx ,,,,,,,,,,,,,,,,,,,,,,,,,,
Bus tracking.pptx ,,,,,,,,,,,,,,,,,,,,,,,,,,Bus tracking.pptx ,,,,,,,,,,,,,,,,,,,,,,,,,,
Bus tracking.pptx ,,,,,,,,,,,,,,,,,,,,,,,,,,bhuyansuprit
 
Cosumer Willingness to Pay for Sustainable Bricks
Cosumer Willingness to Pay for Sustainable BricksCosumer Willingness to Pay for Sustainable Bricks
Cosumer Willingness to Pay for Sustainable Bricksabhishekparmar618
 
办理学位证(NUS证书)新加坡国立大学毕业证成绩单原版一比一
办理学位证(NUS证书)新加坡国立大学毕业证成绩单原版一比一办理学位证(NUS证书)新加坡国立大学毕业证成绩单原版一比一
办理学位证(NUS证书)新加坡国立大学毕业证成绩单原版一比一Fi L
 
Call Girls In Safdarjung Enclave 24/7✡️9711147426✡️ Escorts Service
Call Girls In Safdarjung Enclave 24/7✡️9711147426✡️ Escorts ServiceCall Girls In Safdarjung Enclave 24/7✡️9711147426✡️ Escorts Service
Call Girls In Safdarjung Enclave 24/7✡️9711147426✡️ Escorts Servicejennyeacort
 
办理(宾州州立毕业证书)美国宾夕法尼亚州立大学毕业证成绩单原版一比一
办理(宾州州立毕业证书)美国宾夕法尼亚州立大学毕业证成绩单原版一比一办理(宾州州立毕业证书)美国宾夕法尼亚州立大学毕业证成绩单原版一比一
办理(宾州州立毕业证书)美国宾夕法尼亚州立大学毕业证成绩单原版一比一F La
 
306MTAMount UCLA University Bachelor's Diploma in Social Media
306MTAMount UCLA University Bachelor's Diploma in Social Media306MTAMount UCLA University Bachelor's Diploma in Social Media
306MTAMount UCLA University Bachelor's Diploma in Social MediaD SSS
 
Call Us ✡️97111⇛47426⇛Call In girls Vasant Vihar༒(Delhi)
Call Us ✡️97111⇛47426⇛Call In girls Vasant Vihar༒(Delhi)Call Us ✡️97111⇛47426⇛Call In girls Vasant Vihar༒(Delhi)
Call Us ✡️97111⇛47426⇛Call In girls Vasant Vihar༒(Delhi)jennyeacort
 
在线办理ohio毕业证俄亥俄大学毕业证成绩单留信学历认证
在线办理ohio毕业证俄亥俄大学毕业证成绩单留信学历认证在线办理ohio毕业证俄亥俄大学毕业证成绩单留信学历认证
在线办理ohio毕业证俄亥俄大学毕业证成绩单留信学历认证nhjeo1gg
 
Introduction-to-Canva-and-Graphic-Design-Basics.pptx
Introduction-to-Canva-and-Graphic-Design-Basics.pptxIntroduction-to-Canva-and-Graphic-Design-Basics.pptx
Introduction-to-Canva-and-Graphic-Design-Basics.pptxnewslab143
 
Call Girls Bapu Nagar 7397865700 Ridhima Hire Me Full Night
Call Girls Bapu Nagar 7397865700 Ridhima Hire Me Full NightCall Girls Bapu Nagar 7397865700 Ridhima Hire Me Full Night
Call Girls Bapu Nagar 7397865700 Ridhima Hire Me Full Nightssuser7cb4ff
 
Architecture case study India Habitat Centre, Delhi.pdf
Architecture case study India Habitat Centre, Delhi.pdfArchitecture case study India Habitat Centre, Delhi.pdf
Architecture case study India Habitat Centre, Delhi.pdfSumit Lathwal
 
VIP Call Girls Service Kukatpally Hyderabad Call +91-8250192130
VIP Call Girls Service Kukatpally Hyderabad Call +91-8250192130VIP Call Girls Service Kukatpally Hyderabad Call +91-8250192130
VIP Call Girls Service Kukatpally Hyderabad Call +91-8250192130Suhani Kapoor
 
PORTAFOLIO 2024_ ANASTASIYA KUDINOVA
PORTAFOLIO   2024_  ANASTASIYA  KUDINOVAPORTAFOLIO   2024_  ANASTASIYA  KUDINOVA
PORTAFOLIO 2024_ ANASTASIYA KUDINOVAAnastasiya Kudinova
 

Recently uploaded (20)

Kindergarten Assessment Questions Via LessonUp
Kindergarten Assessment Questions Via LessonUpKindergarten Assessment Questions Via LessonUp
Kindergarten Assessment Questions Via LessonUp
 
Passbook project document_april_21__.pdf
Passbook project document_april_21__.pdfPassbook project document_april_21__.pdf
Passbook project document_april_21__.pdf
 
Cheap Rate Call girls Kalkaji 9205541914 shot 1500 night
Cheap Rate Call girls Kalkaji 9205541914 shot 1500 nightCheap Rate Call girls Kalkaji 9205541914 shot 1500 night
Cheap Rate Call girls Kalkaji 9205541914 shot 1500 night
 
WAEC Carpentry and Joinery Past Questions
WAEC Carpentry and Joinery Past QuestionsWAEC Carpentry and Joinery Past Questions
WAEC Carpentry and Joinery Past Questions
 
办理学位证(SFU证书)西蒙菲莎大学毕业证成绩单原版一比一
办理学位证(SFU证书)西蒙菲莎大学毕业证成绩单原版一比一办理学位证(SFU证书)西蒙菲莎大学毕业证成绩单原版一比一
办理学位证(SFU证书)西蒙菲莎大学毕业证成绩单原版一比一
 
Bus tracking.pptx ,,,,,,,,,,,,,,,,,,,,,,,,,,
Bus tracking.pptx ,,,,,,,,,,,,,,,,,,,,,,,,,,Bus tracking.pptx ,,,,,,,,,,,,,,,,,,,,,,,,,,
Bus tracking.pptx ,,,,,,,,,,,,,,,,,,,,,,,,,,
 
Call Girls in Pratap Nagar, 9953056974 Escort Service
Call Girls in Pratap Nagar,  9953056974 Escort ServiceCall Girls in Pratap Nagar,  9953056974 Escort Service
Call Girls in Pratap Nagar, 9953056974 Escort Service
 
Cosumer Willingness to Pay for Sustainable Bricks
Cosumer Willingness to Pay for Sustainable BricksCosumer Willingness to Pay for Sustainable Bricks
Cosumer Willingness to Pay for Sustainable Bricks
 
办理学位证(NUS证书)新加坡国立大学毕业证成绩单原版一比一
办理学位证(NUS证书)新加坡国立大学毕业证成绩单原版一比一办理学位证(NUS证书)新加坡国立大学毕业证成绩单原版一比一
办理学位证(NUS证书)新加坡国立大学毕业证成绩单原版一比一
 
Call Girls In Safdarjung Enclave 24/7✡️9711147426✡️ Escorts Service
Call Girls In Safdarjung Enclave 24/7✡️9711147426✡️ Escorts ServiceCall Girls In Safdarjung Enclave 24/7✡️9711147426✡️ Escorts Service
Call Girls In Safdarjung Enclave 24/7✡️9711147426✡️ Escorts Service
 
办理(宾州州立毕业证书)美国宾夕法尼亚州立大学毕业证成绩单原版一比一
办理(宾州州立毕业证书)美国宾夕法尼亚州立大学毕业证成绩单原版一比一办理(宾州州立毕业证书)美国宾夕法尼亚州立大学毕业证成绩单原版一比一
办理(宾州州立毕业证书)美国宾夕法尼亚州立大学毕业证成绩单原版一比一
 
306MTAMount UCLA University Bachelor's Diploma in Social Media
306MTAMount UCLA University Bachelor's Diploma in Social Media306MTAMount UCLA University Bachelor's Diploma in Social Media
306MTAMount UCLA University Bachelor's Diploma in Social Media
 
Call Us ✡️97111⇛47426⇛Call In girls Vasant Vihar༒(Delhi)
Call Us ✡️97111⇛47426⇛Call In girls Vasant Vihar༒(Delhi)Call Us ✡️97111⇛47426⇛Call In girls Vasant Vihar༒(Delhi)
Call Us ✡️97111⇛47426⇛Call In girls Vasant Vihar༒(Delhi)
 
在线办理ohio毕业证俄亥俄大学毕业证成绩单留信学历认证
在线办理ohio毕业证俄亥俄大学毕业证成绩单留信学历认证在线办理ohio毕业证俄亥俄大学毕业证成绩单留信学历认证
在线办理ohio毕业证俄亥俄大学毕业证成绩单留信学历认证
 
Introduction-to-Canva-and-Graphic-Design-Basics.pptx
Introduction-to-Canva-and-Graphic-Design-Basics.pptxIntroduction-to-Canva-and-Graphic-Design-Basics.pptx
Introduction-to-Canva-and-Graphic-Design-Basics.pptx
 
Call Girls Bapu Nagar 7397865700 Ridhima Hire Me Full Night
Call Girls Bapu Nagar 7397865700 Ridhima Hire Me Full NightCall Girls Bapu Nagar 7397865700 Ridhima Hire Me Full Night
Call Girls Bapu Nagar 7397865700 Ridhima Hire Me Full Night
 
Architecture case study India Habitat Centre, Delhi.pdf
Architecture case study India Habitat Centre, Delhi.pdfArchitecture case study India Habitat Centre, Delhi.pdf
Architecture case study India Habitat Centre, Delhi.pdf
 
young call girls in Pandav nagar 🔝 9953056974 🔝 Delhi escort Service
young call girls in Pandav nagar 🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Pandav nagar 🔝 9953056974 🔝 Delhi escort Service
young call girls in Pandav nagar 🔝 9953056974 🔝 Delhi escort Service
 
VIP Call Girls Service Kukatpally Hyderabad Call +91-8250192130
VIP Call Girls Service Kukatpally Hyderabad Call +91-8250192130VIP Call Girls Service Kukatpally Hyderabad Call +91-8250192130
VIP Call Girls Service Kukatpally Hyderabad Call +91-8250192130
 
PORTAFOLIO 2024_ ANASTASIYA KUDINOVA
PORTAFOLIO   2024_  ANASTASIYA  KUDINOVAPORTAFOLIO   2024_  ANASTASIYA  KUDINOVA
PORTAFOLIO 2024_ ANASTASIYA KUDINOVA
 

Java 例外處理壞味道與重構技術

  • 4. 4
  • 5. 5
  • 6. 6
  • 7. 7
  • 11. • Correctness – Contract Specification(超出範圍) • Robustness – Exception Handling Copyright@2013 Teddysoft
  • 16. 16 EH smell Refactoring Ignored checked exception Replace Ignored Checked Exception with Unchecked Exception Unprotected main program Avoid Unexpected Termination with Big Outer Try Block Dummy handler Replace Dummy Handler with Rethrow Nested try block Replace Nested Try Block with Method Ignored checked exception, Dummy handler Introduce Checkpoint Class Spare handler Introduce Resourceful Try Clause Copyright@2013 Teddysoft
  • 17. public void writeFile(String fileName, String data) { Writer writer = null; try { writer = new FileWriter(fileName); /* may throw an IOException */ writer.write(data); /* may throw an IOException */ } catch (IOException e) { /* ignoring the exception */ } } ↓ public void writeFile(String fileName, String data) { Writer writer = null; try { writer = new FileWriter(fileName); /* may throw an IOException */ writer.write(data); /* may throw an IOException */ } catch (IOException e) { throw new UnhandledException(e, “message”); } } 17 Copyright@2013 Teddysoft
  • 18. 18 static public void main(String[] args) { /* some code */ } ↓ static public void main(String[] args) { try { /* some code */ } catch (Throwable e) { /* displaying and/or logging the exception */ } } Copyright@2013 Teddysoft
  • 19. 19 catch (AnException e) { e.printStackTrace(); } ↓ catch (AnException e) { throw new UnhandledException(e, “message”); } Copyright@2013 Teddysoft
  • 20. 20 FileInputStream in = null; try { in = new FileInputStream(…); } finally { try { if (in != null) in.close (); } catch (IOException e) { /* log the exception */ } } ↓ FileInputStream in = null; try { in = new FileInputStream(…); } finally { closeIO (in); } private void closeIO (Closeable c) { try { if (c != null) c.close (); } catch (IOException e) { /* log the exception */ } } Copyright@2013 Teddysoft
  • 21. 21 public void foo () throws FailureException { try { /* code that may change the state of the object */ } catch (AnException e) { throw new FailureException(e); } finally {/* code for cleanup */} } ↓ public void foo () throws FailureException { Checkpoint cp = new Checkpoint (/* parameters */); try { cp. establish (); /* establish a checkpoint */ /* code that may change the state of the object */ } catch (AnException e) { cp.restore (); /* restore the checkpoint */ throw new FailureException(e); } finally { cp.drop(); } } Copyright@2013 Teddysoft
  • 22. 22 try { /* primary */ } catch (SomeException e) { try {/* alternative */} catch(AnotherException e) { throw new FailureException(e); } } ↓ int attempt = 0; int maxAttempt = 2; boolean retry = false; do { try { retry = false; if (attempt == 0) { /* primary */ } else { /* alternative */ } } catch (SomeException e) { attempt++; retry = true; if (attempt > maxAttempt) throw new FailureException (e); } } while (attempt<= maxAttempt && retry) Copyright@2013 Teddysoft
  • 24. • CSS is a banking application for checking customers’ credit history in processing their loan applications 24 Credit Scoring System (CSS) Gateway (IBM Message Queue) Joint Credit Information Center (JCIC) Authentication (Domino Server) Other Applications Copyright@2013 Teddysoft
  • 25. • The use of exceptions and return code were mixed in an arbitrary manner • Many checked exceptions were caught and ignored • Unchecked exceptions were not caught which lead to an unexpected termination of the JSP program • Failure atomicity of each component was not investigated 25 Copyright@2013 Teddysoft
  • 26. • Bug fixing is a strong force drawing the developer’s attention to deal with exceptions • Cost-effectiveness measurements can be provided to convince the management to support the refactoring – Improving quality, saving maintenance cost, and increasing customer satisfaction 26 Copyright@2013 Teddysoft
  • 27. Reported failure Failing operation/ possible reason Failure count Current restoration actions Restoration time per failure Cannot make a connection to Domino server User login/ Connections of Domino Internet Inter-ORB Protocol (DIIOP) were exhausted. It was likely that DIIOP connections were not correctly disposed. 125 1. Rebooting Domino server 2. Rebooting CSS About 20 minutes MQ server connection is broken Sending request to JCJS/ MQ connections were exhausted. Perhaps they were not properly released. 66 1. Rebooting MQ Server 2. Rebooting CSS About 10 minutes Cannot send messages to MQ server Sending request to JCJS/ There were two possible reasons: Message queue was full. Gateway was offline due to maintenance or crash. 10 1. Rebooting MQ Server 2. Rebooting CSS About 10 minutes 27
  • 28. 28 Credit Scoring System (CSS) Gateway (IBM Message Queue) Joint Credit Information Center (JCIC) Authentication (Domino Server 1) Other Applications Authentication (Domino Server 2) X Copyright@2013 Teddysoft
  • 29. 29 Reported failure Failure count from Sep. 2005 to Dec. 2005 Failure count from Sep. 2006 to Dec. 2006 Sep. Oct. Nov. Dec. Sep. Oct. Nov. Dec. Cannot make a connection to Domino server 13 5 9 14 10 5 0 0 MQ Server connection broken 6 1 4 8 3/0 0 1/0 0 Cannot send messages to MQ Server 0 0 0 2 3/0 1/0 0 0 Copyright@2013 Teddysoft
  • 30. 30 Code type Total vs. modified code Modification percentage Non-commented line of code 14150/371 2.63% Catch clauses 855/21 2.46% Copyright@2013 Teddysoft
  • 31. 31 Task Man-hours Sum Collecting bugs reports in 2005 10 18 (43.9%) Refactoring cost (Code review) 7 (30.4%) Refactoring cost (Exception Handling goal analysis) 4 (17.4%) Refactoring cost (Coding) 3 (13%) Refactoring cost (Testing) 9 (39.2%) Total man-hours in applying refactoring 23 (56.1%) Total man-hours in improving robustness 41 (100%) Copyright@2013 Teddysoft
  • 32. 32 Cost element Cost calculation Maintenance cost in 2005 201 (failure) * 2 (hours) * 100 (US$) = 40,200 (US$) Refactoring cost 41 (man-hours) * 100 (US$) = 4,100 (US$) Maintenance cost saving in the following year (money) 40,200 (US$) – 4,100 (US$) = 36,100 (US$) Maintenance cost saving in the following year (percentage) 36,100 (US$) / 40,200 (US$) * 100 = 89.8% Copyright@2013 Teddysoft
  • 33. 33Copyright@2013 Teddysoft EH smell Refactoring Ignored checked exception Replace Ignored Checked Exception with Unchecked Exception Unprotected main program Avoid Unexpected Termination with Big Outer Try Block Dummy handler Replace Dummy Handler with Rethrow Nested try block Replace Nested Try Block with Method Ignored checked exception, Dummy handler Introduce Checkpoint Class Spare handler Introduce Resourceful Try Clause