Refactoring talk pages
   HOME

TheInfoList



OR:

In computer programming and
software design Software design is the process by which an agent creates a specification of a software artifact intended to accomplish goals, using a set of primitive components and subject to constraints. Software design may refer to either "all the activity ...
, code refactoring is the process of restructuring existing computer 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 Readability is the ease with which a reader can understand a written text. In natural language, the readability of text depends on its content (the complexity of its vocabulary and syntax) and its presentation (such as typographic aspects that a ...
and reduced
complexity Complexity characterises the behaviour of a system or model whose components interaction, interact in multiple ways and follow local rules, leading to nonlinearity, randomness, collective dynamics, hierarchy, and emergence. The term is generall ...
; these can improve the source codes maintainability and create a simpler, cleaner, or more expressive internal architecture or object model to improve
extensibility Extensibility is a software engineering and systems design principle that provides for future growth. Extensibility is a measure of the ability to extend a system and the level of effort required to implement the extension. Extensions can be th ...
. 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 Bugs may refer to: * Plural of bug Arts, entertainment and media Fictional characters * Bugs Bunny, a character * Bugs Meany, a character in the ''Encyclopedia Brown'' books Films * ''Bugs'' (2003 film), a science-fiction-horror film * ''Bugs ...
or
vulnerabilities Vulnerability refers to "the quality or state of being exposed to the possibility of being attacked or harmed, either physically or emotionally." A window of vulnerability (WOV) is a time frame within which defensive measures are diminished, com ...
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.


Benefits

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. 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 Extensibility is a software engineering and systems design principle that provides for future growth. Extensibility is a measure of the ability to extend a system and the level of effort required to implement the extension. Extensions can be th ...
. It is easier to extend the capabilities of the application if it uses recognizable
design patterns ''Design Patterns: Elements of Reusable Object-Oriented Software'' (1994) is a software engineering book describing software design patterns. The book was written by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, with a foreword ...
, 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.


Challenges

Refactoring requires extracting software system structure, data models, and intra-application dependencies to get back knowledge of an existing software system. 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. 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. Code refactoring activities are secured with software intelligence when using tools and techniques providing data about algorithms and sequences of code execution. 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.


Testing

Automatic
unit tests In computer programming, unit testing is a software testing method by which individual units of source code—sets of one or more computer program modules together with associated control data, usage procedures, and operating procedures&mda ...
should be set up before refactoring to ensure routines still behave as expected. 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 Repository may refer to: Archives and online databases * Content repository, a database with an associated set of data management tools, allowing application-independent access to the content * Disciplinary repository (or subject repository), an ...
, known as monorepo. 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 In software development, agile (sometimes written Agile) practices include requirements discovery and solutions improvement through the collaborative effort of self-organizing and cross-functional teams with their customer(s)/ end user(s), ad ...
describe this activity as an integral part of the
software development cycle In software engineering, a software development process is a process of dividing software development work into smaller, parallel, or sequential steps or sub-processes to improve Software design, design, Software product management, product man ...
.


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 and website.(these are only about OOP however
Refactoring techniques in Fowler's refactoring Website
/ref> 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 understanding ** Program Dependence Graph - explicit representation of data and control dependencies ** System Dependence Graph - representation of procedure calls between PDG ** Software intelligence - reverse engineers the initial state to understand existing intra-application dependencies * 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 ** Replace conditional with
polymorphism Polymorphism, polymorphic, polymorph, polymorphous, or polymorphy may refer to: Computing * Polymorphism (computer science), the ability in programming to present the same programming interface for differing underlying forms * Ad hoc polymorphis ...
* 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 In software engineering, the Extract Class refactoring is applied when a class becomes overweight with too many methods Method ( grc, μέθοδος, methodos) literally means a pursuit of knowledge, investigation, mode of prosecuting such inquir ...
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 ** Move method or move field – move to a more appropriate
class Class or The Class may refer to: Common uses not otherwise categorized * Class (biology), a taxonomic rank * Class (knowledge representation), a collection of individuals or objects * Class (philosophy), an analytical concept used differentl ...
or source file ** Rename method or rename field – changing the name into a new one that better reveals its purpose ** Pull up – in object-oriented programming (OOP), move to a superclass ** Push down – in OOP, move to a subclass * Automatic clone detection


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, hardware refactoring is to be considered a separate field from traditional code refactoring. Automated refactoring of analog hardware descriptions (in
VHDL-AMS VHDL-AMS is a derivative of the hardware description language VHDL (IEEE standard 1076-1993). It includes analog and mixed-signal extensions (AMS) in order to define the behavior of analog and mixed-signal systems (IEEE 1076.1-1999). The VHDL-AMS ...
) has been proposed by Zeng and Huss. 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. 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 William F. "Bill" Opdyke (born c. 1958) is an American computer scientist and enterprise architect at JPMorgan Chase, known for his early work on code refactoring... Education Opdyke received a B.S. from Drexel University in 1979, an M.S. from ...
and Ralph Johnson. Griswold's Ph.D. thesis, Opdyke's Ph.D. thesis, published in 1992, also used this term. Although refactoring code has been done informally for decades, William Griswold's 1991 Ph.D. dissertation is one of the first major academic works on refactoring functional and procedural programs, followed by
William Opdyke William F. "Bill" Opdyke (born c. 1958) is an American computer scientist and enterprise architect at JPMorgan Chase, known for his early work on code refactoring... Education Opdyke received a B.S. from Drexel University in 1979, an M.S. from ...
's 1992 dissertation on the refactoring of object-oriented programs, 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 Forth or FORTH may refer to: Arts and entertainment * ''forth'' magazine, an Internet magazine * ''Forth'' (album), by The Verve, 2008 * ''Forth'', a 2011 album by Proto-Kaw * Radio Forth, a group of independent local radio stations in Scotla ...
community since at least the early 1980s. Chapter Six of
Leo Brodie The Brodie family are a fictional family from the soap opera ''River City'' that appeared on-screen from 2010 onwards. Creation On 1 September 2010, ATV News Network announced that a new mixed-race family would join ''River City''. The family w ...
's book ''
Thinking Forth In their most common sense, the terms thought and thinking refer to conscious cognitive processes that can happen independently of sensory stimulation. Their most paradigmatic forms are judging, reasoning, concept formation, problem solving, a ...
'' (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 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. Here is a list of a few of these editors, or so-called refactoring browsers. * DMS Software Reengineering Toolkit (Implements large-scale refactoring for C, C++, C#, COBOL, Java, PHP and other languages) * Eclipse based: **
Eclipse An eclipse is an astronomical event that occurs when an astronomical object or spacecraft is temporarily obscured, by passing into the shadow of another body or by having another body pass between it and the viewer. This alignment of three ce ...
(for Java, and to a lesser extent, C++, PHP, Ruby and JavaScript) **
PyDev PyDev is a third-party plug-in for Eclipse. It is an Integrated Development Environment (IDE) used for programming in Python supporting code refactoring, graphical debugging, code analysis among other features. History PyDev was originally ...
(for Python) ** Photran (a Fortran plugin for the
Eclipse IDE Eclipse is an integrated development environment (IDE) used in computer programming. It contains a base workspace and an extensible plug-in system for customizing the environment. It is the second-most-popular IDE for Java development, and, un ...
) * Embarcadero Delphi * IntelliJ based: ** Resharper (for C#) ** AppCode (for Objective-C, C and C++) ** IntelliJ IDEA (for Java) ** PyCharm (for Python) ** WebStorm (for JavaScript) ** PhpStorm (for PHP) ** Android Studio (for Java and C++) * JDeveloper (for Java) * NetBeans (for Java) *
Smalltalk Smalltalk is an object-oriented, dynamically typed reflective programming language. It was designed and created in part for educational use, specifically for constructionist learning, at the Learning Research Group (LRG) of Xerox PARC by Alan Ka ...
: Most dialects include powerful refactoring tools. Many use the original refactoring browser produced in the early '90s by Ralph Johnson. * Visual Studio based: ** Visual Studio (for .NET and C++) **
CodeRush CodeRush Classic is a refactoring and productivity Plug-in (computing), plugin by DevExpress that extends native functionality of Microsoft Visual Studio (Visual Studio .NET 2012, 2013, 2015, 2017 and 2019). CodeRush Classic provides solution-wi ...
(addon for Visual Studio) **
Visual Assist Visual Assist is a plug-in for Microsoft Visual Studio developed by Whole Tomato Software. The plug-in primarily enhances IntelliSense and syntax highlighting. It also enhances code suggestions, provides refactoring commands, and includes spell c ...
(addon for Visual Studio with refactoring support for C# and C++) *
Wing IDE The Wing Python IDE family of integrated development environments (IDEs) from Wingware was created specifically for the Python (programming language), Python programming language, with support for editing, testing, debugging, inspecting/browsin ...
(for Python) * Xcode (for C, Objective-C, and Swift) * Qt Creator (for C++, Objective-C and QML)


See also

*
Amelioration pattern In software engineering, an amelioration pattern is an anti-pattern formed when an existing software design pattern was edited (i.e. rearranged, added or deleted) to better suit a particular problem so as to achieve some further effect or behavior ...
*
Code review Code review (sometimes referred to as peer review) is a software quality assurance activity in which one or several people check a program mainly by viewing and reading parts of its source code, and they do so after implementation or as an interru ...
*
Database refactoring A database refactoring is a simple change to a database schema that improves its design while retaining both its behavioral and informational semantics. Database refactoring does not change the way data is interpreted or used and does not fix bug ...
* Decomposition (computer science) * Modular programming * Obfuscated code * Prefactoring * Separation of concerns * Software peer review * Test-driven development


References


Further reading

* * * * * * *


External links


What Is Refactoring?
(c2.com article)
Martin Fowler's homepage about refactoring
* {{Authority control Extreme programming Technology neologisms