Incomputer programming andsoftware design,code refactoring is the process of restructuring existingsource code—changing thefactoring—without changing its external behavior. Refactoring is intended to improve the design, structure, and/or implementation of thesoftware (itsnon-functional attributes), while preserving itsfunctionality. Potential advantages of refactoring may include improved codereadability and reducedcomplexity; these can improve the source code'smaintainability and create a simpler, cleaner, or more expressive internalarchitecture orobject model to improveextensibility. 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 basicmicro-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. Manydevelopment 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 dormantbugs orvulnerabilities 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.
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.
— Joshua Kerievsky,Refactoring to Patterns[1]
Refactoring is usually motivated by noticing acode smell.[2] For example, the method at hand may be very long, or it may be a nearduplicate of another nearby method. Once recognized, such problems can be addressed byrefactoring 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 accumulatingtechnical 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.
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 tailorsoftware to thehardware on which it runs, for example, to take advantage of parallel processors and vector units.[5]
There are two possible times for refactoring.
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 thecode smells form, a subsequent developer carries out the actual refactoring action.[6]
Refactoring requires extracting software system structure, data models, and intra-application dependencies to get back knowledge of an existing software system.[7]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.[8]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.[9]
Code refactoring activities are secured withsoftware intelligence when using tools and techniques providing data about algorithms and sequences of code execution.[10] 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.[11]
Automaticunit tests should be set up before refactoring to ensure routines still behave as expected.[12] Unit tests can bring stability to even large refactors when performed with a singleatomic commit. A common strategy to allow safe and atomic refactors spanning multiple projects is to store all projects in a singlerepository, known asmonorepo.[13]
With unit testing in place, refactoring is then an iterative cycle of making a smallprogram 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 ofextreme programming and otheragile software development describe this activity as an integral part of thesoftware development cycle.
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 inMartin Fowler's refactoring book[2][page needed] and website.[14] 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 acontext 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 program analysis (called "linting" when performed on less strict interpreted languages) detects problems in a valid but substandard program.
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.
While the termrefactoring originally referred exclusively to refactoring of software code, in recent years code written inhardware description languages has also been refactored. The termhardware refactoring is used as a shorthand term for refactoring of code in hardware description languages. Since hardware description languages are not considered to beprogramming languages by most hardware engineers,[20] hardware refactoring is to be considered a separate field from traditional code refactoring.
Automated refactoring of analog hardware descriptions (inVHDL-AMS) has been proposed by Zeng and Huss.[21] 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 bySynopsysfellow Mike Keating.[22][23] His target is to make complex systems easier to understand, which increases the designers' productivity.
The first known use of the term "refactoring" in the published literature was in a September, 1990 article byWilliam Opdyke andRalph Johnson.[24]Although refactoring code has been done informally for decades,William Griswold's 1991 Ph.D. dissertation[25]is one of the first major academic works on refactoring functional and procedural programs, followed byWilliam Opdyke's 1992 dissertation[26]on the refactoring of object-oriented programs,[27] although all the theory and machinery have long been available asprogram transformation systems. All of these resources provide a catalog of common methods for refactoring; a refactoring method has a description of how to apply themethod and indicators for when you should (or should not) apply the method.
Martin Fowler's bookRefactoring: Improving the Design of Existing Code is the canonical reference.[according to whom?]
The terms "factoring" and "factoring out" have been used in this way in theForth community since at least the early 1980s. Chapter Six ofLeo Brodie's bookThinking Forth (1984)[28] 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" (orfunction) into smaller, more easily maintained functions.
Refactorings can also be reconstructed[29] posthoc to produce concise descriptions of complex software changes recorded in software repositories likeGit.
This sectionneeds additional citations forverification. Please helpimprove this article byadding citations to reliable sources in this section. Unsourced material may be challenged and removed. Find sources: "Code refactoring" – news ·newspapers ·books ·scholar ·JSTOR(July 2018) (Learn how and when to remove this message) |
Many softwareeditors andIDEs have automated refactoring support. Here is a list of a few of these editors, or so-calledrefactoring browsers.
{{cite thesis}}
: CS1 maint: bot: original URL status unknown (link)