Skip to main content

Consolidate Duplicate Code to

  • 3 minutes to read
In This Article

Extracts code from multiple locations into a single method, removing duplicate code from the solution.

Duplicate code can be consolidated into the current class, an existing helper class, a new helper class, or a new ancestor class. The locations available depend upon the location of the duplicate code. For example, if all the duplicate code is in a single class, it can be consolidated into a single method in the same class. To select the location of the new method, choose the appropriate sub item from the Consolidate Duplicate Code to smart tag menu item.

ConsolidateCodeToSubitems

#Availability

From the context menus or via shortcuts:

  • when the caret is on a duplicate code section.

#Example

private List<string> _Data;
public void OutputData(string[] data)
{
  if (data != null &amp;&amp; data.Length &gt; 0 &amp;&amp; data[0] != null &amp;&amp; data[0] != string.Empty &amp;&amp; data[1] != null &amp;&amp; data[1] != string.Empty)
    Console.WriteLine(String.Format("{0}: {1}", data[0], data[1]));
  else
    Console.WriteLine("The data is not complete");
}
public void AddData(string[] data)
{
  if (data != null && data.Length > 0 && data[0] != null && data[0] != string.Empty && data[1] != null && data[1] != string.Empty)
    _Data.Add(String.Format("{0}: {1}", data[0], data[1]));
  else
    throw new Exception("The data is not complete");
}
Public Class MyClassDup
  Private _Data As List(Of String)
  Public Sub OutputData(ByVal data As String())
    If Not data is Nothing AndAlso data.Length > 0 AndAlso Not data(0) is Nothing AndAlso Not data(0) is String.Empty AndAlso Not data(1) is Nothing AndAlso Not data(1) is String.Empty Then
      Console.WriteLine(String.Format("{0}: {1}", data(0), data(1)))
    Else
      Console.WriteLine("The data is not complete")
    End If
  End Sub
  Public Sub AddData(ByVal data As String())
    If Not data is Nothing AndAlso data.Length > 0 AndAlso Not data(0) is Nothing AndAlso Not data(0) is String.Empty AndAlso Not data(1) is Nothing AndAlso Not data(1) is String.Empty Then
      _Data.Add(String.Format("{0}: {1}", data(0), data(1)))
    Else
      Throw New Exception("The data is not complete")
    End If
  End Sub
End Class

Result:

private List<string> _Data;
public void OutputData(string[] data)
{
  if (IsDataValid(data))
    Console.WriteLine(String.Format("{0}: {1}", data[0], data[1]));
  else
    Console.WriteLine("The data is not complete");
}
public void AddData(string[] data)
{
  if (IsDataValid(data))
    _Data.Add(String.Format("{0}: {1}", data[0], data[1]));
  else
    throw new Exception("The data is not complete");
}
private bool IsDataValid(string[] data)
{
  return data != null && data.Length > 0 && data[0] != null && data[0] != string.Empty && data[1] != null && data[1] != string.Empty;
}
Private _Data As List(Of String)
Public Sub OutputData(ByVal data As String())
  If IsDataValid(data) Then
    Console.WriteLine(String.Format("{0}: {1}", data(0), data(1)))
  Else
    Console.WriteLine("The data is not complete")
  End If
End Sub
Public Sub AddData(ByVal data As String())
  If IsDataValid(data) Then
    _Data.Add(String.Format("{0}: {1}", data(0), data(1)))
  Else
    Throw New Exception("The data is not complete")
  End If
End Sub
Private Function IsDataValid(ByVal data As String()) As Boolean
  Return Not data Is Nothing AndAlso data.Length > 0 AndAlso Not data(0) Is Nothing AndAlso Not data(0) Is String.Empty AndAlso Not data(1) Is Nothing AndAlso Not data(1) Is String.Empty
End Function