World Library  
Flag as Inappropriate
Email this Article

Code refactoring

Article Id: WHEBN0000025871
Reproduction Date:

Title: Code refactoring  
Author: World Heritage Encyclopedia
Language: English
Subject: Test-driven development, Stylus Studio, Rule of three (computer programming), Code refactoring, Extreme programming
Collection: Code Refactoring, Extreme Programming, Technology Neologisms
Publisher: World Heritage Encyclopedia
Publication
Date:
 

Code refactoring

Code refactoring is the process of restructuring existing computer code – changing the factoring – without changing its external behavior. Refactoring improves nonfunctional attributes of the software. Advantages include improved code readability and reduced complexity to improve source code maintainability, and create 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.
—Joshua Kerievsky, Refactoring to Patterns[1]

Typically, refactoring applies a series of standardised basic micro-refactorings, each of which is (usually) a tiny change in a computer program's source code that either preserves the behaviour of the software, or at least does not modify its conformance to functional requirements. Many development environments provide automated support for performing the mechanical aspects of these basic refactorings.

Contents

  • Overview 1
  • List of refactoring techniques 2
  • Hardware refactoring 3
  • History 4
  • Automated code refactoring 5
  • See also 6
  • References 7
  • Further reading 8
  • External links 9

Overview

Refactoring is usually motivated by noticing a code smell.[2] 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, one or more smaller subroutines can be extracted; or for duplicate routines, the duplication can be removed and replaced with one shared function. Failure to perform refactoring can result in accumulating technical debt.

There are two general categories of benefits to the activity of refactoring.

  1. 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.[3] 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.
  2. 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.[1]

Before applying a refactoring to a section of code, a solid set of automatic unit tests is needed. The tests are used to demonstrate that the behavior of the module is correct before the refactoring. If it inadvertently turns out that a test fails, then it's generally best to fix the test first, because otherwise it is hard to distinguish between failures introduced by refactoring and failures that were already there. After the refactoring, the tests are run again to verify the refactoring didn't break the tests. Of course, the tests can never prove that there are no bugs, but the important point is that this process can be cost-effective: good unit tests can catch enough errors to make them worthwhile and to make refactoring safe enough.

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, the last small change is undone and repeated in a different way. Through many small steps the program moves from where it was to where you want it to be. For this very iterative process to be practical, the tests must run very quickly, or the programmer would have to spend a large fraction of his or her time waiting for the tests to finish. 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 micro-refactorings; some of these may only apply to certain languages or language types. A longer list can be found in Fowler's Refactoring book[2] and on Fowler's Refactoring Website.[4] Many development environments provide automated support for these micro-refactorings. For instance, a programmer could click on the name of a variable and then select the "Encapsulate field" refactoring from a context menu. The IDE would then prompt for additional details, typically with sensible defaults and a preview of the code changes. After confirmation by the programmer it would carry out the required changes throughout the code.

  • 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/Strategy[5]
    • Replace conditional with polymorphism [6]
  • Techniques for breaking code apart into more logical pieces
    • Componentization breaks code down into reusable semantic units that present clear, well-defined, simple-to-use interfaces.
    • Extract Class moves part of the code from an existing class into a new class.
    • 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.
  • Techniques for improving names and location of code

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,[7] 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.[8] 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.[9][10] 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.[11] 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.[12]

AMIQ DVT, an IDE for hardware design and verification, provides refactoring capabilities for e (verification language), SystemVerilog, Verilog and VHDL.[13]

History

Although refactoring code has been done informally for years, William Griswold's 1991 Ph.D. dissertation[14] is one of the first major academic works on refactoring functional and procedural programs, followed by William Opdyke's 1992 dissertation[15] on the refactoring of object-oriented programs,[16] 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[2] is the canonical reference.

The first known use of the term "refactoring" in the published literature was in a September, 1990 article by William Opdyke and Ralph Johnson.[17] Griswold's Ph.D. thesis,[14] Opdyke's Ph.D. thesis,[15] published in 1992, also used this term.[16]

The term "factoring" has been used in the Forth community since at least the early 1980s. 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.

Refactorings can also be reconstructed[18] posthoc to produce concise descriptions of complex software changes recorded in software repositories like CVS or SVN.

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.

See also

References

  1. ^ a b Kerievsky, Joshua (2004). Refactoring to Patterns. Addison Wesley. 
  2. ^ a b c Fowler, Martin (1999). Refactoring: Improving the design of existing code. Addison Wesley. 
  3. ^ Martin, Robert (2009). Clean Code. Prentice Hall. 
  4. ^ Refactoring techniques in Fowler's refactoring Website
  5. ^ Replace type-checking code with State/Strategy
  6. ^ Replace conditional with polymorphism
  7. ^ Hardware description languages#HDL and programming languages
  8. ^ Kaiping Zeng, Sorin A. Huss, "Architecture refinements by code refactoring of behavioral VHDL-AMS models". ISCAS 2006
  9. ^ M. Keating :"Complexity, Abstraction, and the Challenges of Designing Complex Systems", in DAC'08 tutorial [1]"Bridging a Verification Gap: C++ to RTL for Practical Design"
  10. ^ M. Keating, P. Bricaud: Reuse Methodology Manual for System-on-a-Chip Designs, Kluwer Academic Publishers, 1999.
  11. ^ http://newsgroups.derkeiler.com/Archive/Comp/comp.lang.vhdl/2008-06/msg00173.html
  12. ^ Sigasi launches its first production release for a VHDL development environment
  13. ^ www.dvteclipse.com
  14. ^ a b  
  15. ^ a b  
  16. ^ a b Martin Fowler, "MF Bliki: EtymologyOfRefactoring"
  17. ^  
  18. ^ Weißgerber, Peter; Diehl, S. (2006). "Proceedings of 21st IEEE/ACM International Conference on Automated Software Engineering (ASE 2006)". ACM. 

Further reading

  •  
  • Wake, William C. (2003). Refactoring Workbook. Addison-Wesley.  
  • 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
  • Feathers, Michael C (2004). Working Effectively with Legacy Code. Prentice Hall.  
  • Kerievsky, Joshua (2004). Refactoring To Patterns. Addison-Wesley.  
  • Arsenovski, Danijel (2008). Professional Refactoring in Visual Basic. Wrox.  
  • Arsenovski, Danijel (2009). Professional Refactoring in C# and ASP.NET. Wrox.  
  • Ritchie, Peter (2010). Refactoring with Visual Studio 2010. Packt.  

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é
  • Refactoring at DMOZ
  • 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
This article was sourced from Creative Commons Attribution-ShareAlike License; additional terms may apply. World Heritage Encyclopedia content is assembled from numerous content providers, Open Access Publishing, and in compliance with The Fair Access to Science and Technology Research Act (FASTR), Wikimedia Foundation, Inc., Public Library of Science, The Encyclopedia of Life, Open Book Publishers (OBP), PubMed, U.S. National Library of Medicine, National Center for Biotechnology Information, U.S. National Library of Medicine, National Institutes of Health (NIH), U.S. Department of Health & Human Services, and USA.gov, which sources content from all federal, state, local, tribal, and territorial government publication portals (.gov, .mil, .edu). Funding for USA.gov and content contributors is made possible from the U.S. Congress, E-Government Act of 2002.
 
Crowd sourced content that is contributed to World Heritage Encyclopedia is peer reviewed and edited by our editorial staff to ensure quality scholarly research articles.
 
By using this site, you agree to the Terms of Use and Privacy Policy. World Heritage Encyclopedia™ is a registered trademark of the World Public Library Association, a non-profit organization.
 


Copyright © World Library Foundation. All rights reserved. eBooks from Project Gutenberg are sponsored by the World Library Foundation,
a 501c(4) Member's Support Non-Profit Organization, and is NOT affiliated with any governmental agency or department.