411 REFERENCE · Wikibooks
Introduction to Software Engineering/Testing/Refactoring
Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.
Code refactoring is "a disciplined way to restructure code",Scott Ambler undertaken in order to improve some of the nonfunctional attributes of the software.
Typically, this is done by applying series of "refactorings", each of which is a (usually) tiny change in a computer program's source code that does not modify its functional requirements. Advantages include improved code readability and reduced complexity to improve the maintainability of the source code, as well as a more expressive internal architecture or object model to improve extensibility.
By continuously improving the design of code, we make it easier and easier to work with. This is in sharp contrast to what typically happens: little refactoring and a great deal of attention paid to expediently adding new features. If you get into the hygienic habit of refactoring continuously, you'll find that it is easier to extend and maintain code. 20px 20px - Joshua Kerievsky, Refactoring to Patterns Kerievsky Joshua Refactoring to Patterns Addison Wesley 2004
Refactoring does not take place in a vacuum, but typically the refactoring process takes place in a context of adding features to software:
"... refactoring and adding new functionality are two different but complementary tasks" -- Scott Ambler
Overview
Refactoring is usually motivated by noticing a code smell. Fowler Martin Refactoring: Improving the design of existing code Addison Wesley 1999 For example the method at hand may be very long, or it may be a near duplicate of another nearby method. Once recognized, such problems can be addressed by refactoring the source code, or transforming it into a new form that behaves the same as before but that no longer "smells". For a long routine, extract one or more smaller subroutines. Or for duplicate routines, remove the duplication and utilize one shared function in their place. Failure to perform refactoring can result in accumulating technical debt.
There are two general categories of benefits to the activity of refactoring.
Maintainability. It is easier to fix bugs because the source code is easy to read and the intent of its author is easy to grasp. Martin Robert Clean Code Prentice Hall 2009 This might be achieved by reducing large monolithic routines into a set of individually concise, well-named, single-purpose methods. It might be achieved by moving a method to a more appropriate class, or by removing misleading comments.
Extensibility. It is easier to extend the capabilities of the application if it uses recognizable design patterns, and it provides some flexibility where none before may have existed.
Before refactoring a section of code, a solid set of automatic unit tests is needed. The tests should demonstrate in a few seconds February 2011 that the behavior of the module is correct. The process is then an iterative cycle of making a small program transformation, testing it to ensure correctness, and making another small transformation. If at any point a test fails, you undo your last small change and try again in a different way. Through many small steps the program moves from where it was to where you want it to be. Proponents of extreme programming and other agile methodologies describe this activity as an integral part of the software development cycle.
List of refactoring techniques
Here are some examples of code refactorings; some of these may only apply to certain languages or language types. A longer list can be found in Fowler's Refactoring book and on Fowler's Refactoring Website.Refactoring techniques in Fowler's refactoring Website
Techniques that allow for more abstraction
Encapsulate Field – force code to access the field with getter and setter methods
Generalize Type – create more general types to allow for more code sharing
Replace type-checking code with State/StrategyReplace type-checking code with State/Strategy
Replace conditional with polymorphismReplace conditional with polymorphism
Techniques for breaking code apart into more logical pieces
Extract Method, to turn part of a larger method into a new method. By breaking down code in smaller pieces, it is more easily understandable. This is also applicable to functions.
Extract Class moves part of the code from an existing class into a new class.
Techniques for improving names and location of code
Move Method or Move Field – move to a more appropriate Class or source file
Rename Method or Rename Field – changing the name into a new one that better reveals its purpose
Pull Up – in OOP, move to a superclass
Push Down – in OOP, move to a subclass
Hardware refactoring
While the term refactoring originally referred exclusively to refactoring of software code, in recent years code written in hardware description languages (HDLs) has also been refactored. The term hardware refactoring is used as a shorthand term for refactoring of code in hardware description languages. Since HDLs are not considered to be programming languages by most hardware engineers,Hardware description languages and programming languages hardware refactoring is to be considered a separate field from traditional code refactoring.
Automated refactoring of analog hardware descriptions (in VHDL-AMS) has been proposed by Zeng and Huss.Kaiping Zeng, Sorin A. Huss, "Architecture refinements by code refactoring of behavioral VHDL-AMS models". ISCAS 2006 In their approach, refactoring preserves the simulated behavior of a hardware design. The non-functional measurement that improves is that refactored code can be processed by standard synthesis tools, while the original code cannot.
Refactoring of digital HDLs, albeit manual refactoring, has also been investigated by Synopsys fellow Mike Keating.M. Keating :"Complexity, Abstraction, and the Challenges of Designing Complex Systems", in DAC'08 tutorial "Bridging a Verification Gap: C++ to RTL for Practical Design"M. Keating, P. Bricaud: Reuse Methodology Manual for System-on-a-Chip Designs, Kluwer Academic Publishers, 1999. His target is to make complex systems easier to understand, which increases
the designers' productivity.
In the summer of 2008, there was an intense discussion about refactoring of VHDL code on the news://comp.lang.vhdl newsgroup.http://newsgroups.derkeiler.com/Archive/Comp/comp.lang.vhdl/2008-06/msg00173.html The discussion revolved around a specific manual refactoring performed by one engineer, and the question to whether or not automated tools for such refactoring exist.
As of late 2009, Sigasi is offering automated tool support for VHDL refactoring.www.eetimes.com/news/latest/showArticle.jhtml?articleID=222001855
History
In the past refactoring was avoided in development processes. One example of this is that CVS (created in 1984) does not version the moving or renaming of files and directories.
Although refactoring code has been done informally for years, William Opdyke's 1992 Ph.D. dissertation is the first known paper to specifically examine refactoring,Martin Fowler, "MF Bliki: EtymologyOfRefactoring" although all the theory and machinery have long been available as program transformation systems. All of these resources provide a catalog of common methods for refactoring; a refactoring method has a description of how to apply the method and indicators for when you should (or should not) apply the method.
Martin Fowler's book Refactoring: Improving the Design of Existing Code is the canonical reference.
The first known use of the term "refactoring" in the published literature was in a September, 1990 article by William F. Opdyke and Ralph E. Johnson. William F.
Opdyke
William Opdyke
Johnson, Ralph E.
Refactoring: An Aid in Designing Application Frameworks and Evolving Object-Oriented Systems
Proceedings of the Symposium on Object Oriented Programming Emphasizing Practical Applications (SOOPPA)
ACM
1990
September
Opdyke's Ph.D. thesis, William F
Opdyke
William Opdyke
Refactoring Object-Oriented Frameworks
Ph.D. thesis
University of Illinois at Urbana-Champaign
1992
June
ftp://st.cs.uiuc.edu/pub/papers/refactoring/opdyke-thesis.ps.Z
compressed Postscript
2008-02-12 published in 1992, also used this term.
The term "factoring" has been used in the Forth community since at least the early 1980sFebruary 2011. Chapter Six of Leo Brodie's book Thinking Forth (1984) is dedicated to the subject.
In extreme programming, the Extract Method refactoring technique has essentially the same meaning as factoring in Forth; to break down a "word" (or function) into smaller, more easily maintained functions.
Automated code refactoring
Many software editors and IDEs have automated refactoring support. Here is a list of a few of these editors, or so-called refactoring browsers.
IntelliJ IDEA (for Java)
Eclipse's Java Development Toolkit (JDT)
NetBeans (for Java)
and RefactoringNG, a Netbeans module for refactoring where you can write transformations rules of the program's abstract syntax tree.
Embarcadero Delphi
Visual Studio (for .NET)
JustCode (addon for Visual Studio)
ReSharper (addon for Visual Studio)
Coderush (addon for Visual Studio)
Visual Assist (addon for Visual Studio with refactoring support for VB, VB.NET. C# and C++)
DMS Software Reengineering Toolkit (Implements large-scale refactoring for C, C++, C#, COBOL, Java, PHP and other languages)
Photran a Fortran plugin for the Eclipse IDE
SharpSort addin for Visual Studio 2008
Sigasi Studio - standalone or plugin software for VHDL and System Verilog
XCode
Smalltalk Refactoring Browser (for Smalltalk)
Simplifide (for Verilog, VHDL and SystemVerilog)
Tidier (for Erlang)
References
30em
Further reading
Martin
Fowler
Martin Fowler
1999
Refactoring. Improving the Design of Existing Code
Addison-Wesley
0-201-48567-2
William C.
Wake
2003
Refactoring Workbook
Addison-Wesley
0-321-10929-5
Mens, Tom and Tourwé, Tom (2004) A Survey of Software Refactoring, IEEE Transactions on Software Engineering, February 2004 (vol. 30 no. 2), pp. 126-139
Michael C
Feathers
2004
Working Effectively with Legacy Code
Prentice Hall
0-13-117705-2
Joshua
Kerievsky
2004
Refactoring To Patterns
Addison-Wesley
0-321-21335-1
Danijel
Arsenovski
2008
Professional Refactoring in Visual Basic
Wrox
0-47-017979-1
Danijel
Arsenovski
2009
Professional Refactoring in C# and ASP.NET
Wrox
978-0470434529
Peter
Ritchie
2010
Refactoring with Visual Studio 2010
Packt
978-1849680103
External links
What Is Refactoring? (c2.com article)
Martin Fowler's homepage about refactoring
Aspect-Oriented Refactoring by Ramnivas Laddad
A Survey of Software Refactoring by Tom Mens and Tom Tourwé
Computers/Programming/Methodologies/Refactoring Refactoring
Refactoring Java Code
Refactoring To Patterns Catalog
Extract Boolean Variable from Conditional (a refactoring pattern not listed in the above catalog)
Test-Driven Development With Refactoring
Revisiting Fowler’s Video Store: Refactoring Code, Refining Abstractions