Code refactoring explained

In computer programming and software design, code refactoring is the process of restructuring existing source code—changing the factoring—without changing its external behavior. Refactoring is intended to improve the design, structure, and/or implementation of the software (its non-functional attributes), while preserving its functionality. Potential advantages of refactoring may include improved code readability and reduced complexity; these can improve the source codes maintainability and create a simpler, cleaner, or more expressive internal architecture or object model to improve extensibility. Another potential goal for refactoring is improved performance; software engineers face an ongoing challenge to write programs that perform faster or use less memory.

Typically, refactoring applies a series of standardized basic micro-refactorings, each of which is (usually) a tiny change in a computer program's source code that either preserves the behavior 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 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, and may thus introduce new bugs.

Motivation

Refactoring is usually motivated by noticing a code smell. 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.[1]

Benefits

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

Performance engineering can remove inefficiencies in programs, known as software bloat, arising from traditional software-development strategies that aim to minimize an application's development time rather than the time it takes to run. Performance engineering can also tailor software to the hardware on which it runs, for example, to take advantage of parallel processors and vector units.[3]

Timing and responsibility

There are two possible times for refactoring.

  1. Preventive refactoring – the original developer of the code makes the code more robust when it is still free of smells to prevent the formation of smells in the future.[4]
  2. Corrective refactoring – a subsequent developer performs refactoring to correct code smells as they occur.

A method that balances preventive and corrective refactoring is "shared responsibility for refactoring".This approach splits the refactoring action into two stages and tworoles. The original developer of the code just prepares the code for refactoring, and when the code smells form, a subsequent developer carries out the actual refactoring action.

Challenges

Refactoring requires extracting software system structure, data models, and intra-application dependencies to get back knowledge of an existing software system.[5] The turnover of teams implies missing or inaccurate knowledge of the current state of a system and about design decisions made by departing developers. Further code refactoring activities may require additional effort to regain this knowledge.[6] Refactoring activities generate architectural modifications that deteriorate the structural architecture of a software system. Such deterioration affects architectural properties such as maintainability and comprehensibility which can lead to a complete re-development of software systems.[7]

Code refactoring activities are secured with software intelligence when using tools and techniques providing data about algorithms and sequences of code execution.[8] Providing a comprehensible format for the inner-state of software system structure, data models, and intra-components dependencies is a critical element to form a high-level understanding and then refined views of what needs to be modified, and how.[9]

Testing

Automatic unit tests should be set up before refactoring to ensure routines still behave as expected.[10] 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.[11]

With unit testing in place, 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 their 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.

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[12] and website.[13] 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.

Hardware refactoring

While the term refactoring originally referred exclusively to refactoring of software code, in recent years code written in hardware description languages has also been refactored. The term hardware refactoring is used as a shorthand term for refactoring of code in hardware description languages. Since hardware description languages are not considered to be programming languages by most hardware engineers,[19] 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.[20] 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 hardware description languages, albeit manual refactoring, has also been investigated by Synopsys fellow Mike Keating.[21] [22] His target is to make complex systems easier to understand, which increases the designers' productivity.

History

The first known use of the term "refactoring" in the published literature was in a September, 1990 article by William Opdyke and Ralph Johnson.[23] Although refactoring code has been done informally for decades, William Griswold's 1991 Ph.D. dissertation[24] is one of the first major academic works on refactoring functional and procedural programs, followed by William Opdyke's 1992 dissertation[25] on the refactoring of object-oriented programs,[26] 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 terms "factoring" and "factoring out" have been used in this way in the Forth community since at least the early 1980s. Chapter Six of Leo Brodie's book Thinking Forth (1984)[27] 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[28] 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.

Most dialects include powerful refactoring tools. Many use the original refactoring browser produced in the early '90s by Ralph Johnson.

See also

Further reading

External links

Notes and References

  1. Book: Suryanarayana. Girish. Refactoring for Software Design Smells. November 2014. Morgan Kaufmann. 978-0128013977. 258.
  2. Book: Martin, Robert . Clean Code . Prentice Hall . 2009.
  3. 10.1126/science.aam9744. free. There's plenty of room at the Top: What will drive computer performance after Moore's law?. 2020. Leiserson. Charles E.. Thompson. Neil C.. Emer. Joel S.. Kuszmaul. Bradley C.. Lampson. Butler W.. Sanchez. Daniel. Schardl. Tao B.. Science. 368. 6495. eaam9744. 32499413.
  4. Book: Fraivert. Dov. Lorenz. David H.. Language Support for Refactorability Decay Prevention. 2022. Proceedings of the 21st ACM SIGPLAN International Conference on Generative Programming: Concepts and Experiences. 122–134. 10.1145/3564719.3568688. 978-1-4503-9920-3.
  5. Book: Haendler. Thorsten. Neumann. Gustaf. Proceedings of the 11th International Joint Conference on Knowledge Discovery, Knowledge Engineering and Knowledge Management. A Framework for the Assessment and Training of Software Refactoring Competences. 204754665. 2019. 307–316. 10.5220/0008350803070316. 978-989-758-382-7. free.
  6. Book: Nassif. Matthieu. Robillard. Martin P.. 2017 IEEE International Conference on Software Maintenance and Evolution (ICSME). Revisiting Turnover-Induced Knowledge Loss in Software Projects. 13147063. November 2017. 261–272. 10.1109/ICSME.2017.64. 978-1-5386-0992-7.
  7. van Gurp. Jilles. Bosch. Jan. March 2002. Design erosion: problems and causes. Journal of Systems and Software. 61. 2. 105–119. 10.1016/S0164-1212(01)00152-2.
  8. Hassan. Ahmed E.. Xie. Tao. November 2010. Software intelligence: the future of mining software engineering data. In Proceedings of the FSE/SDP Workshop on Future of Software Engineering Research (FoSER '10). 161–166. 10.1145/1882362.1882397. 3485526.
  9. Novais. Renato. Santos. José Amancio. Mendonça. Manoel. 2017. Experimentally assessing the combination of multiple visualization strategies for software evolution analysis. Journal of Systems and Software. 128. 56–71. 10.1016/j.jss.2017.03.006.
  10. Book: Fowler, Martin . Refactoring : improving the design of existing code . 1999 . Addison-Wesley . 978-0201485677 . Reading, MA . 41017370 .
  11. Book: Smart . John Ferguson . Java Power Tools . 2008 . "O'Reilly Media, Inc." . 9781491954546 . 301 . 26 July 2018 . en.
  12. Book: Fowler, Martin. Refactoring. Improving the Design of Existing Code. Addison-Wesley. 1999. 978-0-201-48567-7. 63ff. Martin Fowler (software engineer).
  13. (these are only about OOP however).Refactoring techniques in Fowler's refactoring Website
  14. Ferrante. Jeanne. Ottenstein. Karl J.. Warren. Joe D.. July 1987. The program dependence graph and its use in optimization. ACM Transactions on Programming Languages and Systems . 9. 3. 319–349. ACM. 10.1145/24039.24041. 505075. free.
  15. Book: Donglin. Linag. Harrold. M. J.. Proceedings. International Conference on Software Maintenance (Cat. No. 98CB36272). Slicing objects using system dependence graphs. November 2008. 319–349. IEEE. 10.1109/ICSM.1998.738527. 978-0-8186-8779-2. 18160599.
  16. Web site: Replace type-checking code with State/Strategy.
  17. Web site: Replace conditional with polymorphism.
  18. Bruntink, Magiel, et al. "An evaluation of clone detection techniques for crosscutting concerns." Software Maintenance, 2004. Proceedings. 20th IEEE International Conference on. IEEE, 2004.
  19. [Hardware description languages#HDL and programming languages]
  20. Kaiping Zeng, Sorin A. Huss, "Architecture refinements by code refactoring of behavioral VHDL-AMS models". ISCAS 2006
  21. M. Keating :"Complexity, Abstraction, and the Challenges of Designing Complex Systems", in DAC'08 tutorial http://www.dac.com/events/eventdetails.aspx?id=77-130 "Bridging a Verification Gap: C++ to RTL for Practical Design"
  22. M. Keating, P. Bricaud: Reuse Methodology Manual for System-on-a-Chip Designs, Kluwer Academic Publishers, 1999.
  23. 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 . September 1990.
  24. William G . Griswold . Bill Griswold . Program Restructuring as an Aid to Software Maintenance . Ph.D. . University of Washington . July 1991 . 2011-12-24.
  25. William F . Opdyke . William Opdyke . Refactoring Object-Oriented Frameworks . Ph.D. . University of Illinois at Urbana-Champaign . June 1992 . https://web.archive.org/web/20191216212919/https://dl.acm.org/citation.cfm?id=169783 . 2019-12-16 . compressed Postscript . 2008-02-12 . bot: unknown .
  26. Web site: Martin Fowler, "MF Bliki: EtymologyOfRefactoring".
  27. Book: Brodie . Leo . Thinking Forth . 2004 . 0-9764587-0-5 . 171–196 . Fig Leaf Press, Forth Interest . 3 May 2020 . https://web.archive.org/web/20051216163615/http://thinking-forth.sourceforge.net/ . 16 December 2005 . dead .
  28. News: Andriy . Sokolov . What is code refactoring? .
  29. Web site: What's new in Xcode 9.
  30. Web site: Refactoring in Qt Creator.