Refactoring is the art of reworking your code into a more simplified or efficient form in a disciplined way. Refactoring improves internal code structure without altering its external functionality by transforming functions and rethinking algorithms. Consequently, refactoring is an iterative process. By refactoring your JavaScript, Flash, and Java you can streamline its size, simplify your code, and speed up download and execution speed.
Martin Fowler, who wrote the book on refactoring, calls these changes “transformations.” Individual transformations may make small improvements, but taken as a whole transformations can make a significant improvement to your code. Refactorings include reducing scope, replacing complex instructions with simpler or built-in instructions, and combining multiple statements into one statement. Here’s a simple example that replaces an assignment with an initialization. So instead of this:
function foo() { var i; // ... i = 5; }
Do this:
function foo() { var i = 5; // ... }
Conclusion
Refactoring clarifies your code into a more efficient form. By transforming your code with refactoring techniques it will be faster to change, execute, and download. Refactoring is an excellent best practice to adopt for programmers wanting to improve their productivity.
About the Author
Andy King is the founder of five developer-related sites, and the author of Speed Up Your Site: Web Site Optimization (http://www.speedupyoursite.com) from New Riders Publishing. He publishes the monthly Bandwidth Report, the weekly Optimization Week, and the weekly Speed Tweak of the Week.
Further Reading
- Hacking and Refactoring
- Noted hacker Eric Raymond says that design-by-refactoring perfectly describes how hackers and open-source authors program. Artima Developer, June 14, 2003.
- Refactoring: Improving the Design of Existing Code
- The definitive book on improving source code design. By Martin Fowler, Kent Beck, John Brant, William Opdyke, and Don Roberts (Boston: Addison-Wesley, 1999).
- Refactoring with Martin Fowler
- Bill Venners interviews Martin Fowler on refactoring for Artima Developer, Nov. 4, 2002.
- Refactoring.com
- Martin Fowler’s catalog of refactorings, includes RSS feed with refactoring news.