Code refactoring

related topics
{math, number, function}
{system, computer, user}
{work, book, publish}
{theory, work, human}
{area, community, home}

Code refactoring is the process of changing a computer program's source code without modifying its external functional behavior in order to improve some of the nonfunctional attributes of the software. 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.

—- Joshua Kerievsky, Refactoring to Patterns [1]

Contents

Overview

Refactoring is usually motivated by noticing a code smell.[2] 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.

Before refactoring a section of code, a solid set of automatic unit tests is needed. The tests should demonstrate in a few seconds 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 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.

List of refactoring techniques

Here are some examples of code refactorings. A longer list can be found in Fowler's Refactoring book[2] and on Fowler's Refactoring Website.[4]

Full article ▸

related documents
Range encoding
Search engine (computing)
GNU Octave
QuakeC
Zeroth
MOO (programming language)
Atlas Autocode
Dublin Core
Syntactic sugar
Interchange File Format
MD5
Turing completeness
Magma computer algebra system
BPP
PILOT
Dyadic rational
Stirling number
T1 space
Algebraic number
C*-algebra
Category (mathematics)
Regular space
Real line
Cayley's theorem
Hash collision
CLU (programming language)
Subalgebra
Alternative algebra
Partial fractions in integration
Nowhere dense set