Metric Driven Refactoring
Metrics
• The Bad
• The Good
• The Compromise
The Bad
• Often misleading
• Often misunderstood

• Lines of Code
• Number of Defects
• Turn time
The Good
• Can add objectivity to a
  subjective exercise
• Can help us focus on
  problem areas
• Can provide guidance at...
The Compromise
• Metrics must be easily
  understood
• Metrics must provide
  actionable data
• Metric must be easy to
  c...
What is Refactoring
• Small incremental
  changes
• Changes the internal
  structure
• Does not change the
  external beha...
Why Refactor
•   Improve maintainability
•   Improve performance
•   Improve legibility
•   Make code reusable
•   Pay off...
Code Smells
• Indicate that something
  maybe wrong with code
• Smells point to the
  need to refactor
• Individual smells...
Sample Code Smells
•   Long method
•   Feature Envy
•   Switch Statement Smell
•   Shot Gun Surgery
•   Large Class
•   Co...
Smelly Metrics
• Metrics that Help
  identify Code in need of
  Refactoring
• Metric provides discrete
  values that can b...
Instruction Count
• Number of IL
  Instructions in a
  Method
• More instructions =
  Longer Method
• More Instructions =
...
Cyclomatic Complexity
• Number of Logical Paths
  Through a Method
• Number of Test Cases
  Needed for Full
  Coverage
• H...
Guidelines
• Guidelines – Not Hard
  and Fast Rules
• Exceptions Allowed but
  Must be Understood
• 200 Instructions per
 ...
Running Metrics
Common Refactors
• Extract Method
• Replace Method with
  Method Object
• Decompose Conditional
Apply Some Refactors
New Best Practices
• Review Metrics Before
  and After Each Round of
  Code Changes
• “Do No Harm”
• Improve the Methods
 ...
Where to Get More Information
• http://www.geekswithb
  logs.net/nharrison
• http://www.simple-
  talk.com/author/nick-
  ...
Upcoming SlideShare
Loading in …5
×

Metric driven refactoring

1,043
-1

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,043
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Metric driven refactoring

  1. 1. Metric Driven Refactoring
  2. 2. Metrics • The Bad • The Good • The Compromise
  3. 3. The Bad • Often misleading • Often misunderstood • Lines of Code • Number of Defects • Turn time
  4. 4. The Good • Can add objectivity to a subjective exercise • Can help us focus on problem areas • Can provide guidance at an early stage
  5. 5. The Compromise • Metrics must be easily understood • Metrics must provide actionable data • Metric must be easy to calculate
  6. 6. What is Refactoring • Small incremental changes • Changes the internal structure • Does not change the external behavior
  7. 7. Why Refactor • Improve maintainability • Improve performance • Improve legibility • Make code reusable • Pay off design deficit
  8. 8. Code Smells • Indicate that something maybe wrong with code • Smells point to the need to refactor • Individual smells are often associated with specific refactors
  9. 9. Sample Code Smells • Long method • Feature Envy • Switch Statement Smell • Shot Gun Surgery • Large Class • Comments
  10. 10. Smelly Metrics • Metrics that Help identify Code in need of Refactoring • Metric provides discrete values that can be compared against guidelines
  11. 11. Instruction Count • Number of IL Instructions in a Method • More instructions = Longer Method • More Instructions = Less Focused Method • More Instructions = More Effort to Support
  12. 12. Cyclomatic Complexity • Number of Logical Paths Through a Method • Number of Test Cases Needed for Full Coverage • Higher Complexity = More Difficult to Support • Higher Complexity = More likely to Have Errors
  13. 13. Guidelines • Guidelines – Not Hard and Fast Rules • Exceptions Allowed but Must be Understood • 200 Instructions per Method or Less • Cyclomatic Complexity below 10 • Cyclomatic Complexity = 1 in a View
  14. 14. Running Metrics
  15. 15. Common Refactors • Extract Method • Replace Method with Method Object • Decompose Conditional
  16. 16. Apply Some Refactors
  17. 17. New Best Practices • Review Metrics Before and After Each Round of Code Changes • “Do No Harm” • Improve the Methods that You Touch • Adhering to these Guidelines will Create Better Software
  18. 18. Where to Get More Information • http://www.geekswithb logs.net/nharrison • http://www.simple- talk.com/author/nick- harrison/ • http://www.refactoring. com/catalog/index.html • http://www.red- gate.com/products/refl ector/index.htm

×