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; these can improve source-code maintainability and create a more expressive internal architecture or object model to improve extensibility.

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. If done extremely well, code refactoring may help software developers discover and fix hidden or dormant bugs or vulnerabilities in the system by simplifying the underlying logic and eliminating unnecessary levels of complexity. If done poorly it may fail the requirement that external functionality not be changed, introduce new bugs, or both.

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]

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; on the other hand, refactoring is one of the primary means of repaying technical debt.[3]

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.[4] 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]

Automatic unit tests should be set up before refactoring to ensure routines still behave as expected.[5] Unit tests can bring stability to even large refactors when performed with a single atomic commit. A common strategy to allow safe and atomic refactors spanning multiple projects is to store all projects in a single repository, known as monorepo.[6]

Refactoring 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 software development 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 Martin Fowler's refactoring book[2][page needed] and website.[7] 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
  • 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,[10] 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.[11] 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.[12][13] His target is to make complex systems easier to understand, which increases the designers' productivity.

History

Although refactoring code has been done informally for decades, 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.[according to whom?]

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. It is possible to refactor application code as well as test code.[19] 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. pp. 63ff. ISBN 0-201-48567-2.
  3. ^ Suryanarayana, Girish (November 2014). Refactoring for Software Design Smells. Morgan Kaufmann. p. 258. ISBN 978-0128013977.
  4. ^ Martin, Robert (2009). Clean Code. Prentice Hall.
  5. ^ 1963-, Fowler, Martin, (1999). Refactoring : improving the design of existing code. Reading, MA: Addison-Wesley. ISBN 0201485672. OCLC 41017370.
  6. ^ Smart, John Ferguson (2008). Java Power Tools. "O'Reilly Media, Inc.". p. 301. ISBN 9781491954546. Retrieved 2018.
  7. ^ (these are only about OOP however).Refactoring techniques in Fowler's refactoring Website
  8. ^ Replace type-checking code with State/Strategy
  9. ^ Replace conditional with polymorphism
  10. ^ Hardware description languages#HDL and programming languages
  11. ^ Kaiping Zeng, Sorin A. Huss, "Architecture refinements by code refactoring of behavioral VHDL-AMS models". ISCAS 2006
  12. ^ 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"
  13. ^ M. Keating, P. Bricaud: Reuse Methodology Manual for System-on-a-Chip Designs, Kluwer Academic Publishers, 1999.
  14. ^ a b Griswold, William G (July 1991). Program Restructuring as an Aid to Software Maintenance (PDF) (Ph.D. thesis). University of Washington. Retrieved .
  15. ^ a b Opdyke, William F (June 1992). Refactoring Object-Oriented Frameworks (compressed Postscript) (Ph.D. thesis). University of Illinois at Urbana-Champaign. Retrieved .[permanent dead link]
  16. ^ a b Martin Fowler, "MF Bliki: EtymologyOfRefactoring"
  17. ^ Opdyke, William F.; Johnson, Ralph E. (September 1990). "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.
  18. ^ Weißgerber, Peter; Diehl, S. (2006). "Identifying Refactorings from Source-Code Changes" (PDF). Proceedings of 21st IEEE/ACM International Conference on Automated Software Engineering (ASE 2006). ACM.
  19. ^ Xuan, Jifeng; Cornu, Benoit; Martinez, Matias; Baudry, Benoit; Seinturier, Lionel; Monperrus, Martin (2016). "B-Refactoring: Automatic test code refactoring to improve dynamic analysis". Information and Software Technology. 76: 65-80. doi:10.1016/j.infsof.2016.04.016.
  20. ^ What's new in Xcode 9

Further reading

External links


  This article uses material from the Wikipedia page available here. It is released under the Creative Commons Attribution-Share-Alike License 3.0.

Refactoring
 



 

Connect with defaultLogic
What We've Done
Led Digital Marketing Efforts of Top 500 e-Retailers.
Worked with Top Brands at Leading Agencies.
Successfully Managed Over $50 million in Digital Ad Spend.
Developed Strategies and Processes that Enabled Brands to Grow During an Economic Downturn.
Taught Advanced Internet Marketing Strategies at the graduate level.


Manage research, learning and skills at defaultlogic.com. Create an account using LinkedIn to manage and organize your omni-channel knowledge. defaultlogic.com is like a shopping cart for information -- helping you to save, discuss and share.


  Contact Us