O'Reilly logo

Visual C# 2005: A Developer's Notebook by Jesse Liberty

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Use Refactoring to Speed Revision of Your Code

For most of us mortal programmers, the first version of complex code we create is not always the best it can be. As we code, we often decide to change the names of variables, factor out common routines into methods, and encapsulate member variables into properties. Some of these tasks can lead to difficulties: for example, changing the name of a variable can create subtle bugs if you forget to update references to the variable throughout the code.

Visual Studio 2005 offers a number of refactoring features that automate this process and ensure that the code is left in a consistent state.

How do I do that?

To learn more about the new refactoring features in Visual Studio 2005, first create a new Console application and name it SimplifyCoding. Replace the code provided by Visual Studio 2005 with the code shown in Example 2-1.

Example 2-1. Starting code for SimplifyCoding example

#region Using directives using System; using System.Collections.Generic; using System.Text; #endregion namespace SimplifyCoding { public class Dog { public int age; public int weight; public Dog(int age, int weight) { this.age = age; this.weight = weight; } public void Method1( ) { Console.WriteLine("This dog is overweight"); } public void Method2(int x) { weight += x; Console.WriteLine("This dog is overweight"); } public override string ToString( ) { return "I weigh " + weight + " and I'm " + age + " years old"; } } class Program { static void Main(string[ ] args) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required