Misplaced Pages

Code refactoring: Difference between revisions

Article snapshot taken from Wikipedia with creative commons attribution-sharealike license. Give it a read and then ask your questions in the chat. We can research this topic together.
Browse history interactively← Previous editContent deleted Content addedVisualWikitext
Revision as of 17:56, 24 March 2004 edit195.71.52.185 (talk)No edit summary← Previous edit Latest revision as of 20:30, 27 December 2024 edit undoNyq (talk | contribs)Extended confirmed users5,225 editsm lc per MOS:EXPABBR and MOS:SECTIONCAPSTags: Visual edit Mobile edit Mobile web edit Advanced mobile edit 
(866 intermediate revisions by more than 100 users not shown)
Line 1: Line 1:
{{short description|Restructuring existing computer code without changing its external behavior}}
]
{{Redirect|Refactoring|its use on Misplaced Pages|Misplaced Pages:Refactoring talk pages|selfref = true}}
]
{{About-distinguish|a behaviour-preserving change|Rewrite (programming)}}
]
]
'''Refactoring''' is the process of ] written material to improve its ] or structure, with the explicit purpose of keeping its ] or ].


In ] and ], '''code refactoring''' is the process of restructuring existing ]—changing the '']''—without changing its external behavior. Refactoring is intended to improve the design, structure, and/or implementation of the ] (its '']'' attributes), while preserving its ]. Potential advantages of refactoring may include improved code ] and reduced ]; these can improve the ]{{'}}s ] and create a simpler, cleaner, or more expressive internal ] or ] to improve ]. Another potential goal for refactoring is improved performance; software engineers face an ongoing challenge to write programs that perform faster or use less memory.
== Refactoring Code ==
The term refactoring is often used to describe modifying ] without changing its external behavior, and is sometimes informally referred to as "] it up." In this case, refactoring is part of the cycle of create and test code, refactor and test code, add new behavior and test code, refactor and test code, add new behavior and test code, and so on. ] ensures that the code still does what it is supposed to.


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 ] 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 ] or ] 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.
Refactoring is the part of ] which doesn't fix bugs or add new ]. Rather it is designed to improve the ] of the code or change its ] and ], to make it easier for human ] in the future. In particular, adding new behavior to a program might be difficult with the program's given structure, so a developer might refactor it first to make easy, and then add the new behavior.


{{Blockquote|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''<ref name=kerievsky>{{cite book | last = Kerievsky | first = Joshua | title = Refactoring to Patterns | publisher = Addison Wesley | year = 2004 }}</ref>}}
The term is by analogy with the ] of ]s and ]s. For example, <math>x^2 - 1</math> can be factored as <math>(x + 1)(x - 1)</math>, revealing an internal structure that was previously not visible (such as the two zeroes at +1 and -1). Similarly, in software refactoring, the change in visible structure can often reveal the "hidden" internal structure of the original code.


==Motivation==
Refactoring is done as a separate step, to simplify testing. At the end of the refactoring, any change in behavior is clearly a ] and can be fixed separately from the problem of debugging the new behavior.
Refactoring is usually motivated by noticing a ].<ref name="fowler"/> For example, the method at hand may be very long, or it may be a near ] 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 ]; on the other hand, refactoring is one of the primary means of repaying technical debt.<ref>{{cite book|last1=Suryanarayana|first1=Girish|title=Refactoring for Software Design Smells|date=November 2014|publisher=Morgan Kaufmann|isbn=978-0128013977|pages=258}}</ref>
An example of a trivial refactoring is to change a variable name into something more meaningful, such as from a single letter 'i' to 'interestRate'. A more complex refactoring is to turn the code within an if block into a ]. An even more complex refactoring is to replace an if ] with ]. While "cleaning up" code has happened for decades, the key insight in refactoring is to intentionally "clean up" code separately from adding new functionality, using a known catalogue of common useful refactoring methods, and then separately testing the code (knowing that any behavioral changes indicate a bug). The new aspect is explicitly wanting to improve an existing design without altering its intent or behavior.


==Benefits==
Refactoring is an important aspect of ].
There are two general categories of benefits to the activity of refactoring.
# ]. 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>{{cite book | last = Martin | first = Robert |title = Clean Code | publisher = Prentice Hall | year = 2009}}</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.
# ]. It is easier to extend the capabilities of the application if it uses recognizable ], 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 ] to the ] on which it runs, for example, to take advantage of parallel processors and vector units.<ref>{{Cite journal|doi=10.1126/science.aam9744|doi-access=free|title=There's plenty of room at the Top: What will drive computer performance after Moore's law?|year=2020|last1=Leiserson|first1=Charles E.|last2=Thompson|first2=Neil C.|last3=Emer|first3=Joel S.|last4=Kuszmaul|first4=Bradley C.|last5=Lampson|first5=Butler W.|last6=Sanchez|first6=Daniel|last7=Schardl|first7=Tao B.|journal=Science|volume=368|issue=6495|pages=eaam9744|pmid=32499413}}</ref>


==Timing and responsibility==
The book ''Refactoring'' is the classic reference. Although refactoring code has been done informally for years, ]'s ] PhD dissertation is the first known paper to specifically examine refactoring. All of these resources provide a ]; a refactoring method has a ] of how to apply the ] and ]s for when you should (or should not) apply the method.
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 ] to prevent the formation of smells in the future.<ref name="fraivert2022">
{{Cite conference
|last1=Fraivert|first1=Dov
|last2=Lorenz|first2=David H.
|title=Language Support for Refactorability Decay Prevention
|date=2022
|book-title=Proceedings of the 21st ACM SIGPLAN International Conference on Generative Programming: Concepts and Experiences
|pages=122–134
|doi=10.1145/3564719.3568688
|isbn=978-1-4503-9920-3
}}</ref>
# Corrective refactoring – a subsequent developer performs refactoring to correct ]s as they occur.<ref name="fraivert2022" />


A method that balances preventive and corrective refactoring is "shared responsibility for refactoring".
Refactoring is such an important ] that it has been identified as
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 ]s form, a subsequent developer carries out the actual refactoring action.<ref name="fraivert2022" />


==Challenges==
Many refactorings are meant to cure the ills listed in .
Refactoring requires extracting software system structure, data models, and intra-application dependencies to get back knowledge of an existing software system.<ref>
{{Cite conference
|last1=Haendler|first1=Thorsten
|last2=Neumann|first2=Gustaf
|book-title=Proceedings of the 11th International Joint Conference on Knowledge Discovery, Knowledge Engineering and Knowledge Management
|title=A Framework for the Assessment and Training of Software Refactoring Competences
|s2cid=204754665
|date=2019
|pages=307–316
|doi=10.5220/0008350803070316
|isbn=978-989-758-382-7
|doi-access=free
}}</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>
{{Cite conference
|last1=Nassif|first1=Matthieu
|last2=Robillard|first2=Martin P.
|conference=2017 IEEE International Conference on Software Maintenance and Evolution (ICSME)
|title=Revisiting Turnover-Induced Knowledge Loss in Software Projects
|s2cid=13147063
|date=November 2017
|pages=261–272
|doi=10.1109/ICSME.2017.64
|isbn=978-1-5386-0992-7
}}</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>
{{Cite journal
|last1=van Gurp|first1=Jilles
|last2=Bosch|first2=Jan
|date=March 2002
|title= Design erosion: problems and causes
|journal=Journal of Systems and Software
|volume=61
|issue=2
|pages=105–119
|doi=10.1016/S0164-1212(01)00152-2
}}</ref>


Code refactoring activities are secured with ] when using tools and techniques providing data about algorithms and sequences of code execution.<ref>
=== Break down large functions ===
{{Cite conference
]s that grow in use also grow in ]. After a certain point, it becomes clear that functions can benefit from using functions themselves. Your ]s probe the function looking for a likely ] to relocate elsewhere. This candidate is chosen for being the largest stretch of code that really has nothing to do with the enclosing function. This lets us easily think up a name that explains what the new function does without having to resort to something contrived just to distinguish it from the original function.
|last1=Hassan|first1=Ahmed E.
|last2=Xie|first2=Tao
|date=November 2010
|title=Software intelligence: the future of mining software engineering data
|book-title=Proceedings of the FSE/SDP Workshop on Future of Software Engineering Research (FoSER '10)
|pages=161–166
|doi=10.1145/1882362.1882397
|s2cid=3485526
}}</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>
{{Cite journal
|last1=Novais|first1=Renato
|last2=Santos|first2=José Amancio
|last3=Mendonça|first3=Manoel
|date=2017
|title=Experimentally assessing the combination of multiple visualization strategies for software evolution analysis
|journal=Journal of Systems and Software
|volume=128
|pages= 56–71
|doi=10.1016/j.jss.2017.03.006
}}</ref>


==Testing==
''This section is originally from ]''
Automatic ] should be set up before refactoring to ensure routines still behave as expected.<ref>{{Cite book |title=Refactoring : improving the design of existing code |last=Fowler |first=Martin |date=1999 |publisher=Addison-Wesley |isbn=978-0201485677 |location=Reading, MA |oclc=41017370 |url=https://archive.org/details/isbn_9780201485677 }}</ref> Unit tests can bring stability to even large refactors when performed with a single ]. A common strategy to allow safe and atomic refactors spanning multiple projects is to store all projects in a single ], known as ].<ref>{{cite book |last1=Smart |first1=John Ferguson |title=Java Power Tools |date=2008 |publisher="O'Reilly Media, Inc." |isbn=9781491954546 |page=301 |url=https://books.google.com/books?id=kE0UDQAAQBAJ&q=visual+sourcesafe+atomic+commit&pg=PA301 |access-date=26 July 2018 |language=en}}</ref>


With unit testing in place, refactoring is then an iterative cycle of making a small ], 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 ] and other ] describe this activity as an integral part of the ].
== Refactoring other Text ==
The term refactoring originated in ], but the concept has also been applied when modifying any text.


==Techniques==
To '''refactor''' text, in a WikiWiki, is to rewrite and often to reorganize the text completely or radically, sometimes dramatically shortening it, but in a way that preserves all the important, relevant content. In a WikiWiki, it is usually '']s'' that are refactored--with the purpose of making the discussion accessible to someone who is interested in the arguments made in the discussion, and the information to be gleaned from it, rather than the history of the discussion itself. ]
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 ]'s refactoring book<ref name="fowler">{{cite book|title=Refactoring. Improving the Design of Existing Code|last=Fowler|first=Martin|publisher=Addison-Wesley|year=1999|isbn=978-0-201-48567-7|pages=|author-link=Martin Fowler (software engineer)|url=https://archive.org/details/isbn_9780201485677/page/63}}</ref>{{page needed|date=July 2018}} and website.<ref name="refactoring.com">(these are only about OOP however).</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 ]. 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 analysis===
] (called "linting" when performed on less strict interpreted languages) detects problems in a valid but substandard program.


* ] - explicit representation of data and control dependencies <ref>
Obviously, refactoring is difficult to do in such a way that will make all ]s in a discussion happy. It requires ], tact, excellent reading comprehension skills, good writing skills, and a lack of ].
{{Cite journal
|last1=Ferrante|first1=Jeanne
|last2=Ottenstein|first2=Karl J.
|last3=Warren|first3=Joe D.
|date=July 1987
|title= The program dependence graph and its use in optimization
|journal=ACM Transactions on Programming Languages and Systems
|volume=9
|issue=3
|pages=319–349
|publisher=ACM
|doi=10.1145/24039.24041
|s2cid=505075
|doi-access=free
}}</ref>
* System dependence graph - representation of procedure calls between PDG <ref>
{{Cite conference
|last1=Donglin|first1=Linag
|last2=Harrold|first2=M. J.
|book-title=Proceedings, International Conference on Software Maintenance (Cat. No. 98CB36272)
|title=Slicing objects using system dependence graphs
|date=November 2008
|pages=319–349
|publisher=IEEE
|doi=10.1109/ICSM.1998.738527
|isbn=978-0-8186-8779-2
|s2cid=18160599
}}</ref>
* ] analysis.
* ] - reverse engineers the initial state to understand existing intra-application dependencies


===Transformations===
Several good discussions are found at the WikiWeb at .
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 ]
----
** ] – force code to access the field with getter and setter methods
** ] – create more general types to allow for more code sharing
** Replace type-checking code with state/strategy<ref>{{cite web| url = http://refactoring.com/catalog/replaceTypeCodeWithStateStrategy.html| title = Replace type-checking code with State/Strategy}}</ref>
** Replace conditional with ]<ref>{{cite web| url = http://refactoring.com/catalog/replaceConditionalWithPolymorphism.html| title = Replace conditional with polymorphism}}</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.
** ] moves part of the code from an existing class into a new class.
** Extract method, to turn part of a larger ] into a new method. By breaking down code in smaller pieces, it is more easily understandable. This is also applicable to ]s.
* Techniques for improving names and location of code
** Move method or move field – move to a more appropriate ] or source file
** Rename method or rename field – changing the name into a new one that better reveals its purpose
** Pull up – in ] (OOP), move to a ]
** Push down – in OOP, move to a ]<ref name="refactoring.com"/>
* Automatic ]<ref>{{cite conference |first1=M. |last1=Bruntink |first2=A. |last2=van Deursen |first3=T. |last3=Tourwe |first4=R. |last4=van Engelen |title=An evaluation of clone detection techniques for crosscutting concerns |book-title=Proceedings, 20th IEEE International Conference on Software Maintenance |publisher=IEEE |date=2004 |doi=10.1109/ICSM.2004.1357804}}</ref>


For policy on refactoring in Misplaced Pages, see ]. In the context of Misplaced Pages, usually ]s are refactored, but occasionally, when an article has been developed in a particularly haphazard way, it needs to be changed so radically that the resulting change can be called a refactoring.


==Hardware refactoring==
While the term ''refactoring'' originally referred exclusively to refactoring of software code, in recent years code written in ]s 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 ]s by most hardware engineers,<ref>]</ref> hardware refactoring is to be considered a separate field from traditional code refactoring.

Automated refactoring of analog hardware descriptions (in ]) 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 ] ] Mike Keating.<ref>M. Keating :"Complexity, Abstraction, and the Challenges of Designing Complex Systems", in DAC'08 tutorial {{Webarchive|url=https://web.archive.org/web/20160328163412/https://dac.com/events/eventdetails.aspx?id=77-130|date=2016-03-28}}"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.

==History==
The first known use of the term "refactoring" in the published literature was in a September, 1990 article by ] and ].<ref name="opdyke90">{{cite conference
| first = William F.
| last = Opdyke
| author-link = William Opdyke
|author2=Johnson, Ralph E.
| title = Refactoring: An Aid in Designing Application Frameworks and Evolving Object-Oriented Systems
| book-title = Proceedings of the Symposium on Object Oriented Programming Emphasizing Practical Applications (SOOPPA)
| publisher = ACM
|date=September 1990
}}</ref>
Although refactoring code has been done informally for decades, ]'s 1991 Ph.D. dissertation<ref name="griswold-thesis">{{cite thesis
| first = William G
| last = Griswold
| author-link = Bill Griswold
| title = Program Restructuring as an Aid to Software Maintenance
| degree = Ph.D.
| publisher = University of Washington
|date=July 1991
| url = http://cseweb.ucsd.edu/~wgg/Abstracts/gristhesis.pdf
| access-date = 2011-12-24
}}</ref>
is one of the first major academic works on refactoring functional and procedural programs, followed by ]'s 1992 dissertation<ref name="opdyke-thesis">{{cite thesis
| first = William F
| last = Opdyke
| author-link = William Opdyke
| title = Refactoring Object-Oriented Frameworks
| degree = Ph.D.
| publisher = University of Illinois at Urbana-Champaign
| date = June 1992
| url = http://dl.acm.org/citation.cfm?id=169783
| archive-url = https://web.archive.org/web/20191216212919/https://dl.acm.org/citation.cfm?id=169783
| archive-date = 2019-12-16
| format = compressed Postscript
| access-date = 2008-02-12
| url-status = bot: unknown
}}</ref>
on the refactoring of object-oriented programs,<ref name="etymology">{{cite web| url = http://martinfowler.com/bliki/EtymologyOfRefactoring.html| title = Martin Fowler, "MF Bliki: EtymologyOfRefactoring"}}</ref> although all the theory and machinery have long been available as ] systems. All of these resources provide a catalog of common methods for refactoring; a refactoring method has a description of how to apply the ] and indicators for when you should (or should not) apply the method.

]'s book ''Refactoring: Improving the Design of Existing Code'' is the canonical reference. {{According to whom|date=July 2018}}

The terms "factoring" and "factoring out" have been used in this way in the ] community since at least the early 1980s. Chapter Six of ]'s book '']'' (1984)<ref>{{cite book
|last1=Brodie
|first1=Leo
|title=Thinking Forth
|year=2004
|isbn=0-9764587-0-5
|pages=171–196
|publisher=Fig Leaf Press, Forth Interest
|url=http://thinking-forth.sourceforge.net
|access-date=3 May 2020
|archive-url=https://web.archive.org/web/20051216163615/http://thinking-forth.sourceforge.net/
|archive-date=16 December 2005
|url-status=dead
}}</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 ]) into smaller, more easily maintained functions.

Refactorings can also be reconstructed<ref name="What-is-code-refactoring?">{{cite news
| first = Andriy
| last = Sokolov
| title = What is code refactoring?
| url = https://duecode.io/blog/what-is-code-refactoring/
}}</ref> posthoc to produce concise descriptions of complex software changes recorded in software repositories like CVS or SVN.

==Automated code refactoring==
{{More citations needed|section|date=July 2018}}
Many software ] and ] have automated refactoring support. Here is a list of a few of these editors, or so-called ].
* ] (Implements large-scale refactoring for C, C++, C#, COBOL, Java, PHP and other languages)
* Eclipse based:
** ] (for ], and to a lesser extent, C++, PHP, Ruby and JavaScript)
** ] (for ])
* ]
* IntelliJ based:
** ] (for ], C and C++)
** ] (for ])
** ] (for ])
** ] (for ])
** ] (for ])
** ] (for ] and C++)
* ] (for ])
* ] (for ])
*]: Most dialects include powerful refactoring tools. Many use the original refactoring browser produced in the early '90s by ].
* Visual Studio based:
** ] (for .NET and C++)
** ] (addon for Visual Studio with refactoring support for C# and C++)
* ] (for ])
* ] (for C, ], and ])<ref>{{cite web| url = https://developer.apple.com/library/content/documentation/DeveloperTools/Conceptual/WhatsNewXcode/xcode_9/xcode_9.html| title = What's new in Xcode 9}}</ref>
* ] (for C++, Objective-C and QML)<ref>{{cite web| url = https://doc.qt.io/qtcreator/creator-overview.html| title = Overview {{!}} Qt Creator Documentation}}</ref>


==See also== ==See also==
* ]
* ] * ]
* ] * ]
* ]
* ]
* ]
* ]
* ]
* ]
* ]
* ]


==Bibliography== ==References==
{{Reflist}}
* ]: ''Refactoring. Improving the Design of Existing Code'', Addison-Wesley Professional 1999, ISBN 0201485672

* ]: ''Refactoring Workbook'', Addison-Wesley, ISBN 0-321-10929-5
==Further reading==
* ]: ''Refactoring To Patterns'', Addison-Wesley, ISBN 0321213351
*{{cite book
| first = William C.
| last = Wake
| year = 2003
| title = Refactoring Workbook
| publisher = Addison-Wesley
| isbn = 978-0-321-10929-3
}}
* {{cite journal|first1=T.|last1=Mens|first2=T.|last2=Tourwe|title=A survey of software refactoring|url=https://zenodo.org/record/848931|journal=IEEE Transactions on Software Engineering|date=February 2004|issn=0098-5589|pages=126–139|volume=30|issue=2|doi=10.1109/tse.2004.1265817|s2cid=206778272}}
*{{cite book
| first = Michael C
| last = Feathers
| year = 2004
| title = Working Effectively with Legacy Code
| publisher = Prentice Hall
| isbn = 978-0-13-117705-5
}}
*{{cite book
| first = Joshua
| last = Kerievsky
| year = 2004
| title = Refactoring To Patterns
| publisher = Addison-Wesley
| isbn = 978-0-321-21335-8
}}
*{{cite book
| first = Danijel
| last = Arsenovski
| year = 2008
| title = Professional Refactoring in Visual Basic
| publisher = Wrox
| isbn = 978-0-470-17979-6
}}
*{{cite book
| first = Danijel
| last = Arsenovski
| year = 2009
| title = Professional Refactoring in C# and ASP.NET
| publisher = Wrox
| isbn = 978-0-470-43452-9
}}
*{{cite book
| first = Peter
| last = Ritchie
| year = 2010
| title = Refactoring with Visual Studio 2010
| publisher = Packt
| isbn = 978-1-84968-010-3
}}


==External links== ==External links==
* (c2.com article) * (c2.com article)
* *

* by ]
{{Authority control}}
* by ]

* by ] and ]
]
* "" by ]
]
*
]
* - alphabetical list
*
*

Latest revision as of 20:30, 27 December 2024

Restructuring existing computer code without changing its external behavior "Refactoring" redirects here. For its use on Misplaced Pages, see Misplaced Pages:Refactoring talk pages. This article is about a behaviour-preserving change. Not to be confused with Rewrite (programming).

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.

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

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.

  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. 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.

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.
  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 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.

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 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, 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 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 and website. 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 analysis

Static program analysis (called "linting" when performed on less strict interpreted languages) detects problems in a valid but substandard program.

Transformations

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
    • Replace conditional with polymorphism
  • 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
  • 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) 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 and Ralph Johnson. 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'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 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 posthoc to produce concise descriptions of complex software changes recorded in software repositories like CVS or SVN.

Automated code refactoring

This section needs additional citations for verification. Please help improve this article by adding 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 software editors and IDEs have automated refactoring support. Here is a list of a few of these editors, or so-called refactoring browsers.

See also

References

  1. ^ Kerievsky, Joshua (2004). Refactoring to Patterns. Addison Wesley.
  2. ^ Fowler, Martin (1999). Refactoring. Improving the Design of Existing Code. Addison-Wesley. pp. 63ff. ISBN 978-0-201-48567-7.
  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. Leiserson, Charles E.; Thompson, Neil C.; Emer, Joel S.; Kuszmaul, Bradley C.; Lampson, Butler W.; Sanchez, Daniel; Schardl, Tao B. (2020). "There's plenty of room at the Top: What will drive computer performance after Moore's law?". Science. 368 (6495): eaam9744. doi:10.1126/science.aam9744. PMID 32499413.
  6. ^ Fraivert, Dov; Lorenz, David H. (2022). "Language Support for Refactorability Decay Prevention". Proceedings of the 21st ACM SIGPLAN International Conference on Generative Programming: Concepts and Experiences. pp. 122–134. doi:10.1145/3564719.3568688. ISBN 978-1-4503-9920-3.
  7. Haendler, Thorsten; Neumann, Gustaf (2019). "A Framework for the Assessment and Training of Software Refactoring Competences". Proceedings of the 11th International Joint Conference on Knowledge Discovery, Knowledge Engineering and Knowledge Management. pp. 307–316. doi:10.5220/0008350803070316. ISBN 978-989-758-382-7. S2CID 204754665.
  8. Nassif, Matthieu; Robillard, Martin P. (November 2017). Revisiting Turnover-Induced Knowledge Loss in Software Projects. 2017 IEEE International Conference on Software Maintenance and Evolution (ICSME). pp. 261–272. doi:10.1109/ICSME.2017.64. ISBN 978-1-5386-0992-7. S2CID 13147063.
  9. van Gurp, Jilles; Bosch, Jan (March 2002). "Design erosion: problems and causes". Journal of Systems and Software. 61 (2): 105–119. doi:10.1016/S0164-1212(01)00152-2.
  10. Hassan, Ahmed E.; Xie, Tao (November 2010). "Software intelligence: the future of mining software engineering data". Proceedings of the FSE/SDP Workshop on Future of Software Engineering Research (FoSER '10). pp. 161–166. doi:10.1145/1882362.1882397. S2CID 3485526.
  11. 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. doi:10.1016/j.jss.2017.03.006.
  12. Fowler, Martin (1999). Refactoring : improving the design of existing code. Reading, MA: Addison-Wesley. ISBN 978-0201485677. OCLC 41017370.
  13. Smart, John Ferguson (2008). Java Power Tools. "O'Reilly Media, Inc.". p. 301. ISBN 9781491954546. Retrieved 26 July 2018.
  14. ^ (these are only about OOP however).Refactoring techniques in Fowler's refactoring Website
  15. 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). ACM: 319–349. doi:10.1145/24039.24041. S2CID 505075.
  16. Donglin, Linag; Harrold, M. J. (November 2008). "Slicing objects using system dependence graphs". Proceedings, International Conference on Software Maintenance (Cat. No. 98CB36272). IEEE. pp. 319–349. doi:10.1109/ICSM.1998.738527. ISBN 978-0-8186-8779-2. S2CID 18160599.
  17. "Replace type-checking code with State/Strategy".
  18. "Replace conditional with polymorphism".
  19. Bruntink, M.; van Deursen, A.; Tourwe, T.; van Engelen, R. (2004). "An evaluation of clone detection techniques for crosscutting concerns". Proceedings, 20th IEEE International Conference on Software Maintenance. IEEE. doi:10.1109/ICSM.2004.1357804.
  20. Hardware description languages#HDL and programming languages
  21. Kaiping Zeng, Sorin A. Huss, "Architecture refinements by code refactoring of behavioral VHDL-AMS models". ISCAS 2006
  22. M. Keating :"Complexity, Abstraction, and the Challenges of Designing Complex Systems", in DAC'08 tutorial Archived 2016-03-28 at the Wayback Machine"Bridging a Verification Gap: C++ to RTL for Practical Design"
  23. M. Keating, P. Bricaud: Reuse Methodology Manual for System-on-a-Chip Designs, Kluwer Academic Publishers, 1999.
  24. 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.
  25. Griswold, William G (July 1991). Program Restructuring as an Aid to Software Maintenance (PDF) (Ph.D. thesis). University of Washington. Retrieved 2011-12-24.
  26. Opdyke, William F (June 1992). Refactoring Object-Oriented Frameworks (Ph.D. thesis). University of Illinois at Urbana-Champaign. Archived from the original on 2019-12-16. Retrieved 2008-02-12.{{cite thesis}}: CS1 maint: bot: original URL status unknown (link)
  27. "Martin Fowler, "MF Bliki: EtymologyOfRefactoring"".
  28. Brodie, Leo (2004). Thinking Forth. Fig Leaf Press, Forth Interest. pp. 171–196. ISBN 0-9764587-0-5. Archived from the original on 16 December 2005. Retrieved 3 May 2020.
  29. Sokolov, Andriy. "What is code refactoring?".
  30. "What's new in Xcode 9".
  31. "Overview | Qt Creator Documentation".

Further reading

External links

Categories: