code refactoring refactor redirects here for the use of refactor on wikipedia see wikipediarefactoring talk pages this article is about a behaviourpreserving change it is not to be confused with rewrite programming code refactoring is the process of restructuring existing computer codechanging the factoringwithout changing its external behavior refactoring is intended to improve nonfunctional attributes of the software advantages include improved code readability and reduced complexity these can improve sourcecode maintainability and create a more expressive internal architecture or object model to improve extensibility typically refactoring applies a series of standardised basic microrefactorings each of which is usually a tiny change in a computer programs source code that either preserves the behaviour 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 extremely 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 introduce new bugs or both 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 adding new features if you get into the hygienic habit of refactoring continuously youll find that it is easier to extend and maintain codejoshua 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 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 wellnamed singlepurpose 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 testing automatic unit tests should be set up before refactoring to ensure routines still behave as expected 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 list of refactoring techniques here are some examples of microrefactorings some of these may only apply to certain languages or language types a longer list can be found in martin fowlers refactoring book many development environments provide automated support for these microrefactorings 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 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 typechecking code with statestrategy replace conditional with polymorphism techniques for breaking code apart into more logical pieces componentization breaks code down into reusable semantic units that present clear welldefined simpletouse 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 objectoriented 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 hdls has also been refactored the term hardware refactoring is used as a shorthand term for refactoring of code in hardware description languages since hdls 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 vhdlams has been proposed by zeng and huss in their approach refactoring preserves the simulated behavior of a hardware design the nonfunctional measurement that improves is that refactored code can be processed by standard synthesis tools while the original code cannot refactoring of digital hdls 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 although refactoring code has been done informally for decades william griswolds 1991 phd dissertation 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 fowlers book refactoring improving the design of existing code the first known use of the term refactoring in the published literature was in a september 1990 article by william opdyke and ralph johnson griswolds phd thesis opdykes phd thesis the term factoring has been used in the forth community since at least the early 1980s chapter six of leo brodies 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 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 socalled refactoring browsers dms software reengineering toolkit implements largescale refactoring for c c c cobol java php and other languages eclipse based eclipse for java and to a lesser extent c php ruby and javascript pydev for python photran a fortran plugin for the eclipse ide embarcadero delphi intellij based appcode for objectivec c and c intellij idea for java pycharm for python webstorm for javascript android studio for java 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 coderush addon for visual studio visual assist addon for visual studio with refactoring support for c and c wing ide for python xcode for c objectivec and swift code review database refactoring decomposition computer science design pattern computer science obfuscated code software peer review prefactoring separation of concerns testdriven development modular programming amelioration pattern wake william c 2003 refactoring workbook addisonwesley isbn0321109295 mens t tourwe t nd a survey of software refactoring ieee transactions on software engineering 30 2 126139 doi101109tse20041265817 issn00985589 feathers michael c 2004 working effectively with legacy code prentice hall isbn0131177052 kerievsky joshua 2004 refactoring to patterns addisonwesley isbn0321213351 arsenovski danijel 2008 professional refactoring in visual basic wrox isbn0470179791 arsenovski danijel 2009 professional refactoring in c and aspnet wrox isbn9780470434529 ritchie peter 2010 refactoring with visual studio 2010 packt isbn9781849680103 what is refactoring c2com article martin fowlers homepage about refactoring refactoring at curlie authority control lccn sh99002049 ndl 01191220 