BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Refactoring
VS 2005+

Visual Studio Extract Method Command

When a method grows too large to be easily manageable, or when a section of a method will be duplicated elsewhere in your code, you may consider extracting part of the code into its own method. Visual Studio can automate this refactoring process.

Extracting a Method with a Return Value

In some cases, the new method will require parameters and a return value. Visual Studio will analyse the selected code to determine if this is the case and create them accordingly. We can demonstrate this by extracting the code that builds a connection string from the server, instance and database names. The new method will require three parameters and will return the connection string.

To extract this method, select the lines of code from "string serverAndInstance;" to the end of the line that begins "string connectionString =". Extract these lines to a new method named "GetConnString". The refactored code should appear as shown below. Note the creation of the parameters and the use of the return value in the original method.

NB: The previously created "ShowDatabaseRequiredMessage" method is omitted for brevity.

public void ReadUserSelectedDatabase()
{
    ShowDatabaseRequiredMessage();

    Console.WriteLine("Server?");
    string server = Console.ReadLine();
    server = server.Trim();

    Console.WriteLine("Instance?");
    string instance = Console.ReadLine();
    instance = instance.Trim();

    Console.WriteLine("Database?");
    string database = Console.ReadLine();
    database = database.Trim();

    string connectionString = GetConnString(server, instance, database);

    OpenDatabaseConnection(connectionString);
    ReadKeyData();
}

private static string GetConnString(string server, string instance, string database)
{
    string serverAndInstance;
    if (instance.Length == 0)
        serverAndInstance = server;
    else
        serverAndInstance = string.Format(@"{0}\{1}", server, instance);

    string connectionString = string.Format(
        "Data Source={0};Initial Catalog={1};Integrated Security=SSPI;",
        serverAndInstance, database);
    return connectionString;
}

Extracting a Method with Multiple Return Values

Sometimes some reusable code can be extracted but only if the new method includes more than one return value. When this is the case, the refactoring operation will create output or reference parameters for these return values. To demonstrate, extract the nine lines of code that request the database information from the user into a new method named "GetDatabaseDetails". The code should now be similar to that shown below:

public void ReadUserSelectedDatabase()
{
    ShowDatabaseRequiredMessage();

    string server;
    string instance;
    string database;
    GetDatabaseDetails(out server, out instance, out database);

    string connectionString = GetConnString(server, instance, database);

    OpenDatabaseConnection(connectionString);
    ReadKeyData();
}

private static void GetDatabaseDetails(
    out string server, out string instance, out string database)
{
    Console.WriteLine("Server?");
    server = Console.ReadLine();
    server = server.Trim();

    Console.WriteLine("Instance?");
    instance = Console.ReadLine();
    instance = instance.Trim();

    Console.WriteLine("Database?");
    database = Console.ReadLine();
    database = database.Trim();
}

The ReadUserSelectedDatabase method is now much shorter and is easier to read and understand than the original version. The code could be tidied further if desired. For example, the method's code could be reformatted manually, as shown below, to make it even more readable.

public void ReadUserSelectedDatabase()
{
    string server, instance, database, connectionString;

    ShowDatabaseRequiredMessage();
    GetDatabaseDetails(out server, out instance, out database);
    connectionString = GetConnString(server, instance, database);
    OpenDatabaseConnection(connectionString);
    ReadKeyData();
}

Further refactoring on the newly created methods could also be undertaken. For example, the code to request a particular piece of information and trim its excess white space could be extracted and used three times. All such changes should be made with the intention of improving the maintainability of the code.

1 January 2009