Template:Short description Template:Redirect Template:About-distinguish
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 code's 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.
<templatestyles src="Template:Blockquote/styles.css" />
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 add new features. If you get into the hygienic habit of refactoring continuously, you'll find that it is easier to extend and maintain code.{{#if:|{{#if:|}}
— {{#if:|, in }}Template:Comma separated entries}}
{{#invoke:Check for unknown parameters|check|unknown=Template:Main other|preview=Page using Template:Blockquote with unknown parameter "_VALUE_"|ignoreblank=y| 1 | 2 | 3 | 4 | 5 | author | by | char | character | cite | class | content | multiline | personquoted | publication | quote | quotesource | quotetext | sign | source | style | text | title | ts }}
MotivationEdit
Refactoring is usually motivated by noticing a code smell.<ref name="fowler"/> 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.<ref>Template:Cite book</ref>
BenefitsEdit
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.<ref name=martin>Template:Cite book</ref> 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.<ref name=kerievsky/>
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.<ref>Template:Cite journal</ref>
Timing and responsibilityEdit
There are two possible times for refactoring.
- 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.<ref name="fraivert2022">
Template:Cite conference</ref>
- Corrective refactoring – a subsequent developer performs refactoring to correct code smells as they occur.<ref name="fraivert2022" />
A method that balances preventive and corrective refactoring is "shared responsibility for refactoring". This approach splits the refactoring action into two stages and two roles. 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.<ref name="fraivert2022" />
ChallengesEdit
Refactoring requires extracting software system structure, data models, and intra-application dependencies to get back knowledge of an existing software system.<ref> Template:Cite conference</ref> 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.<ref> Template:Cite conference</ref> 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. <ref> Template:Cite journal</ref>
Code refactoring activities are secured with software intelligence when using tools and techniques providing data about algorithms and sequences of code execution.<ref> Template:Cite conference</ref> 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.<ref> Template:Cite journal</ref>
TestingEdit
Automatic unit tests should be set up before refactoring to ensure routines still behave as expected.<ref>Template:Cite book</ref> 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.<ref>Template:Cite book</ref>
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.
TechniquesEdit
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<ref name="fowler">Template:Cite book</ref>Template:Page needed and website.<ref name="refactoring.com">(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.
Static analysisEdit
Static program analysis (called "linting" when performed on less strict interpreted languages) detects problems in a valid but substandard program.
- Program dependence graph - explicit representation of data and control dependencies <ref>
Template:Cite journal</ref>
- System dependence graph - representation of procedure calls between PDG <ref>
Template:Cite conference</ref>
- Cyclometric complexity analysis.
- Software intelligence - reverse engineers the initial state to understand existing intra-application dependencies
TransformationsEdit
Transformations modify the syntactic representation of a program. Some modifications alter the semantics or structure of the program in a way which improves its flexibility or robustness. Such modifications require knowledge of the problem domain and intended logic, and thus are infeasible to automate. Modifications exist which make the program easier to read and modify but which to not alter the underlying logic of the program; these transformations can be automated.
- 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<ref>{{#invoke:citation/CS1|citation
|CitationClass=web }}</ref>
- Replace conditional with polymorphism<ref>{{#invoke:citation/CS1|citation
|CitationClass=web }}</ref>
- 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
- 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 object-oriented programming (OOP), move to a superclass
- Push down – in OOP, move to a subclass<ref name="refactoring.com"/>
- Automatic clone detection<ref>Template:Cite conference</ref>
Hardware refactoringEdit
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,<ref>Hardware description languages#HDL and programming languages</ref> 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.<ref>Kaiping Zeng, Sorin A. Huss, "Architecture refinements by code refactoring of behavioral VHDL-AMS models". ISCAS 2006</ref> 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.<ref>M. Keating :"Complexity, Abstraction, and the Challenges of Designing Complex Systems", in DAC'08 tutorial [1] Template:Webarchive"Bridging a Verification Gap: C++ to RTL for Practical Design"</ref><ref>M. Keating, P. Bricaud: Reuse Methodology Manual for System-on-a-Chip Designs, Kluwer Academic Publishers, 1999.</ref> His target is to make complex systems easier to understand, which increases the designers' productivity.
HistoryEdit
The first known use of the term "refactoring" in the published literature was in a September, 1990 article by William Opdyke and Ralph Johnson.<ref name="opdyke90">Template:Cite conference</ref> Although refactoring code has been done informally for decades, William Griswold's 1991 Ph.D. dissertation<ref name="griswold-thesis">Template:Cite thesis</ref> is one of the first major academic works on refactoring functional and procedural programs, followed by William Opdyke's 1992 dissertation<ref name="opdyke-thesis">Template:Cite thesis</ref> on the refactoring of object-oriented programs,<ref name="etymology">{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> 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. Template:According to whom
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)<ref>Template:Cite book</ref> 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<ref name="What-is-code-refactoring?">Template:Cite news</ref> posthoc to produce concise descriptions of complex software changes recorded in software repositories like Git.
Automated code refactoringEdit
Template:More citations needed Many software editors and IDEs have automated refactoring support. 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:
- Embarcadero Delphi
- IntelliJ based:
- 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: 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++)
- Visual Assist (addon for Visual Studio with refactoring support for C# and C++)
- Wing IDE (for Python)
- Xcode (for C, Objective-C, and Swift)<ref>{{#invoke:citation/CS1|citation
|CitationClass=web }}</ref>
- Qt Creator (for C++, Objective-C and QML)<ref>{{#invoke:citation/CS1|citation
|CitationClass=web }}</ref>
See alsoEdit
- Amelioration pattern
- Code review
- Database refactoring
- Decomposition (computer science)
- Modular programming
- Obfuscated code
- Prefactoring
- Rewrite (programming)
- Separation of concerns
- Software peer review
- Test-driven development
ReferencesEdit
Further readingEdit
- Template:Cite book
- Template:Cite journal
- Template:Cite book
- Template:Cite book
- Template:Cite book
- Template:Cite book
- Template:Cite book
External linksEdit
- What Is Refactoring? (c2.com article)
- Martin Fowler's homepage about refactoring