This post is about refactoring, and to give you basic understanding what refactoring can help you with and what you can do with refactoring.
If you are familiar with the Agile methodology you know that a project is splitted into several small iterations. The idea of iteration is that you cannot get a design right the first time, you need to refine it while you are developing the software. During each iteration you can use Test Driven Development to not only help you to test your application at an early stage or increasing the quality of the software, it also will help you with the design of the Software you’re building. By iteration over the design several times, you get a better design. During each iteration and after, you are also Refactoring, refactoring will also help you create a better design and a code that do not smell (ugly code). Martin Fowler describes Refactoring in two ways:
"(Noun) A change made to the internal structure of software to make is easier to understand and cheaper to modify without changing its observable behavior."
"(Werb) To restructure software by applying a series of refactorings without changing its observable behavior."
With refactoring you will make your Software easier to understand, not only for you, also for other developers that are going to add new functions or change bugs in existing Software. Refactoring will also help you to find bugs and help you program faster. You do the refactoring when you add a function or need to fix a bug or when you do a code review. By reafactoring you could get a better understanding of the software where you should fix a bug or add a function to. With refactoring you can also remove duplicated code for not making the code smell badly. You can also reduce the use of comments in the code, some comments can make the code difficult to read and with refactoring you can create code that doesn’t need comments. It will not remove comments completely, some comments need to be added to the code for example, making other developer to understand a complex algorithm in the code or if you during the day leave the code, you should know what you should do when you came back the next day, the comments will works as a reminder. There are two ways you can do refactoring, either by using a tool or do it manually. By using a tool will make the reafactoring easier. There is a refactoring tool shipped with Visual Studio 2005. The tool has not all the refactorings I use often, but it’s better than nothing. To not make this post to large (I like small posts) I will show you one of the most common refactoring, Extract Method. With Extract method you can turn a fragment into a method whose name explains the purpose of the method:
public void PrintOwing()
{
PrintBanner();
//Print details
Console.WriteLine("name: " + _name);
Console.WriteLine("amount " + GetOutstanding());
}
If we refactoring the code to use the refactoring Extract Mehthod, the code will look like the following code:
public void PrintOwing()
{
PrintBanner();
PrintDetails(GetOutstanding());
}
public void PrintDetails (double outstanding)
{
Console.WriteLine("name: " + _name);
Console.WriteLine("amount " + outstanding);
}
The Extract Method is only one of several Refactorings. You can find a catalog of Refactorings on the www.refactoring.com site.
When I’m building Software, I’m always refactoring my code. I will not get deeply into the different refactorings, the purpose of this post was to make you interested about refactoring.