Friday, April 29, 2011
Refactoring to Patterns
Please click here to get the presentation that I made years ago about refactoring and design pattern.
The key ideas:
What is Refactoring:
• Improving design by restructuring / clean up code
• Without altering external behavior / adding functionality
When to do refactoring:
• Maintenance / fixing bugs
• To understand the codes before adding functionality (e.g. legacy code, systems with integration problem)
When not to do refactoring:
• when the code is too messy, better to rebuild from scratch
• Close to deadline
Best practices:
• Build regression-tests first
• Small steps
• Reversible (e.g. version control)
• Apply Pareto principle, using profiling to prioritize
• The end result targeting Design patterns
Reasons for Refactoring:
• to increase readibility
• to reduce duplication,
• to increace flexibility,
• to increace modularity,
Which lead to:
• Easier to maintain
• Easier to extend
• Faster software development
• Easier to find & solve bugs
Signs that your code needs refactoring:
• Too large classes
• Duplicated codes
• Too long methods
• Too long parameters
• Comments to explain difficult code
• A class / method that doesn’t do much
• Inappropriate intimacy
• Divergent changes in a class
• Shotgun surgery: changes in many classes
Design patterns:
• proven solutions to commonly occurring problems in software design
• don’t reinvent the wheel
• Provide common terminologies for communication
• Variations on implementations depends on domain, tools (e.g. adapter pattern: multiple inheritance in C++, inheritance & interface/reference in Java)
• Nice way to learn software design: peruse design pattern books if you want to become an architect
Conclusions:
• Refactoring: improving design by restructuring without altering external behavior
• Refactoring can help understanding the codes and bugs fixing
• Some best practices during refactoring are: build test first, small & reversible steps, prioritize (Pareto principle)
• Design Patterns: proven solutions to commonly occurring problems in software design. Design Patterns can provide targets for refactoring.
• In practice: refactoring BB-Volg+ shows improvements of some metrics (e.g. reduce duplication, method length, nested depth, cohesion), & other benefits: easier to extend, reusable.
Furthermore, this presentation also discuss:
• several refactoring techniques e.g. extract class, extract methods, parameter object, explaining object, refactoring conditionals, refactoring database, refactoring css and many other techniques.
• several (enterprise) design patterns e.g. singleton, template/strategy, observer, wrapper, factory, mvc,
• a case study: refactoring in one of my project, which resulting in improvements of some metrics (e.g. reduce duplication, method length, nested depth, cohesion), & other benefits: easier to extend, reusable.
PS. You don't have to implement these design patterns from scratch. For example I use Java frameworks such as Struts (MVC pattern, observer pattern, page controller pattern), Hibernate (an object-relational-mapping that implements active records and many transaction management/concurrency patterns), Velocity (template pattern), and Spring (many patterns... proxy, factory, MVC, template, etc).
Please leave your comments to discuss about refactoring & design patterns.
In future I will write blogs about refactoring and design patterns for SOA, so stay tuned!
This presentation is based on my study with these books:
Refactoring
A classic, one of the standard read for software developers.
Patterns of Enterprise Application Architecture
One of the most famous books for J2EE developers
Design Pattern
A seminal literature, one of the standard read for software developer.
Code Complete
Another standard read for software developers.
Labels:
architecture / design,
java
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment