Vous êtes sur la page 1sur 3

"Refactor" redirects here. For the use of 'refactor' on Wikipedia, see Wikipedia:Refactoring talk pages.

Code refactoring is "disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior",[1] undertaken in order to improve some of the nonfunctional attributes of the software. Typically, this is done by applying series of "refactorings", each of which is a (usually) tiny change in a computer program's source code that does not modify its functional requirements. Advantages include improved code readability and reduced complexity to improve the maintainability of the source code, as well as a more expressive internal architecture or object model to improve extensibility.

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, you'll find that it is easier to extend and maintain code.

Overview
Refactoring is usually motivated by noticing a code smell.[3] 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, extract one or more smaller subroutines. Or for duplicate routines, remove the duplication and utilize one shared function in their place. Failure to perform refactoring can result in accumulating technical debt. 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.[4] 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.[2] Before refactoring a section of code, a solid set of automatic unit tests is needed. The tests should demonstrate in a few seconds[citation needed] that the behavior of the module is correct. The process 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, you undo your last small change and try again, possibly in a different way. Through many small steps the program moves from where it was to where you want it to be. Proponents of extreme

programming and other agile methodologies describe this activity as an integral part of the software development cycle.

[edit] List of refactoring techniques


Here are some examples of code refactorings; some of these may only apply to certain languages or language types. A longer list can be found in Fowler's Refactoring book[3] and on Fowler's Refactoring Website.[5]

Techniques that allow for more abstraction o Encapsulate Field force code to access the field with getter and setter methods o Generalize Type create more general types to allow for more code sharing [6] o Replace type-checking code with State/Strategy [7] o Replace conditional with polymorphism Techniques for breaking code apart into more logical pieces o 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. o Extract Class moves part of the code from an existing class into a new class. Techniques for improving names and location of code o Move Method or Move Field move to a more appropriate Class or source file o Rename Method or Rename Field changing the name into a new one that better reveals its purpose o Pull Up in OOP, move to a superclass o Push Down in OOP, move to a subclass

Automated code refactoring


Many software editors and IDEs have automated refactoring support. Here is a list of a few of these editors, or so-called refactoring browsers.

IntelliJ IDEA (for Java) Eclipse's Java Development Toolkit (JDT) NetBeans (for Java) o and RefactoringNG, a Netbeans module for refactoring where you can write transformations rules of the program's abstract syntax tree. JDeveloper (for Java) Embarcadero Delphi Visual Studio (for .NET) Telerik JustCode (addon for Visual Studio) ReSharper (addon for Visual Studio) Coderush (addon for Visual Studio)

Visual Assist (addon for Visual Studio with refactoring support for VB, VB.NET. C# and C++) DMS Software Reengineering Toolkit (Implements large-scale refactoring for C, C++, C#, COBOL, Java, PHP and other languages) Photran (a Fortran plugin for the Eclipse IDE) SharpSort addin for Visual Studio 2008 Sigasi HDT (for VHDL) Xcode Smalltalk Refactoring Browser (for Smalltalk) Simplifide (for Verilog, VHDL and SystemVerilog) Tidier (for Erlang) AMIQ DVT (for e, SystemVerilog, Verilog and VHDL)

Vous aimerez peut-être aussi