tags:

views:

217

answers:

6

I often heard from professionals blog something like refactoring your code whenever the chance you get. What is it exactly? Rewriting your code in simpler and fewer lines? What is the purpose of doing this?

+6  A: 

Refactoring code is a process of cleaning up your code, reducing the clutter and improving the readability without causing any side effects or changes to features.

Basically, you refactor by applying a series of code change rules that improve code readability and re-usability, without affecting the logic.

Always unit test before and after refactoring to ensure your logic isn't affected.

Andrew Keith
+7  A: 

This Wikipedia article will give you an idea of the types of things included in the general concept of Refactoring.

The idea is adapt / evolve your code as you go. Simple things may be to rename variables or method parameters, but others may be to pass an additional parameter or to drop one, or to change its type. The data model may evolve as well. etc.

Often refactoring, works hand-in-hand with unit-testing, whereby the risk of "breaking something" is offset by the fact that such an issue may likely be discovered by the automatic testing (provide a good coverage and relevant test cases...).

In a nutshell, the ability to refactor (and btw, most IDE or add-ons to the IDEs, offer various tools that make refactoring easier and less error prone) allows one to write more quickly without stressing about some decisions ("should this object include an array or a list etc...) letting the programmer change some of these decisions as times goes, and with the added insight offered by having a workable, if not perfect solution. See a related concept: agile development.

Beware, refactoring doesn't give you license to start coding without putting any thought in design, in the object model, the APIs etc., however it lessens the stiffness of some of these decisions.

mjv
A: 

Refactoring code generally means taking code that has been patched multiple times and re-writing it so that the needs of the later patches are taken into account.

staticsan
@staticsan: -1 This may be what you see happening where you work, but it far from best practise and far from the definition.
quamrana
Could you explain how this is different from "Refactoring neither fixes bugs nor adds new functionality, though it might precede either activity. Rather, it improves the understandability of the code, changes its internal structure and design, and removes dead code." (from Wikipedia's definition).
staticsan
+5  A: 

Martin Fowler has probably done the most to popularize refactoring, but I think good developers have always done these sorts of restructurings. Check out his refactoring web site, and his 1999 Refactoring, which is an excellent introduction and catalog of specific refactorings using Java.

And I see he's a co-author of the brand new Refactoring, Ruby Edition, which should be a great resource.

I find that regularly cleaning up your code like this makes it a lot clearer and more maintainable.

To take one example, I wrote a small (Java 1.6) client library for accessing remote web services (using the REST architectural style). The bulk of this library is in one source file, and about half of that deals with the web services, while the other half is a simple in-memory cache of the responses (for performance). Over time both halves have grown in functionality, to the point where the source file was getting too complex. So today I used Fowler's "Extract Class" refactoring to move the cache logic into a new class. Before that I had to do some "Extract Methods" to isolate the caching logic. Along the way I did a few "Rename Methods" and an "Introduce Explaining Variable".

As other folks have noted, it's very important to have a good set of unit tests to apply after you make each change. They help ensure that you're not introducing new bugs, among other good things.

Jim Ferrans
+2  A: 

In a nutshell, refactoring means improving the design and/or implementation of software, usually without changing its behavior. This is normally done to make the code easier to understand and work with going forward, thereby making future development faster and less bug-prone.

Refactoring is a long-term investment in your code - since it doesn't affect the outward "appearance" of the software, there is very often pressure (from management, etc.) to "just get it working and move on to the next thing." While this may sometimes be the right decision, depending on business drivers, a codebase that undergoes change but never gets refactored will decay into a difficult, buggy mess (See also Technical Debt).

Specifically, the top reasons to refactor are usually the following:

  1. Getting rid of duplicated code
  2. Breaking up a long method into smaller pieces by extracting new methods from sections of the longer method
  3. Breaking up a class that has too many responsibilities into smaller, more targeted classes or subclasses
  4. Moving methods from one class to another. Often this is done so the methods reside in the same class as the data they operate on.
glaxaco
+1 for mentioning Technical Debt!
TrueWill
A: 

In the simplest terms, refactoring code is optimizing code. The criteria for what is "better" code is open to much interpretation as there are various coding styles and patterns out there. A central idea with refactoring is the question of, "Could this code be made better?" A few examples of that criteria can include scalability, maintainability, readablity, performance, size of executable, or minimizing memory used in executing the code.

JB King
Please consider replacing the word "optimizing" with "improving the quality of" or equivalent.
TrueWill
What is the difference between the terms to you? The fact that optimizing is subject to various meanings is part of the point in using that term. While you could just go ahead and make the change to my answer, I'd like a little more of a justification than "because you say so," please.
JB King
@JB King Martin Fowler's definition is "Refactoring is a disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior. Its heart is a series of small behavior preserving transformations. Each transformation (called a 'refactoring') does little, but a sequence of transformations can produce a significant restructuring...". "Optimizing" code might have the side-effect of changing its external behavior so it might not exactly be refactoring.
pageman
@pageman, so if someone changed a sorting algorithm that resulted in a different complexity,e.g. going from bubblesort to quicksort, would that count as a different behavior?
JB King