Bilal+Ahmad+Project+1

= = SSE 659 Design Quality and Maintenance toc
 * Name:** Bilal Ahmad

Introduction
Open source software Mini C sharp lab is utilized for refactoring which is a tool for short C sharp code running and testing where a user can save time on waiting for visual studio startup and avoid creating a lot of one-time only project files. The codes in Mini C sharp lab is analyzed for possibility of refactoring at appropriate places so that the codes can be more meaningful and easier to follow through. The chapters 3 to 7 are utilized for refactoring topics.

**1.0 Bad Smells in Code**

 * Objective:** The focus of this chapter is to understand when to apply refactoring techniques to a program by observing certain structures in the code that requires the possibility of refactoring. It is also important to know how to operate the mechanics of a refactoring so that it can be easier to identify the smells in the code. In this chapter I will identify the code that needs improvement and utilize refactoring techniques from chapter 3.

**1.1 Duplicated Code**
If the same code structure is found in more than one place, then it is better to find a way to unify the code. Duplicated code is where there is the same expression in two methods of the same class or where there is the same expression in two sibling subclasses. The snippet code shown below from Mini C sharp lab open source software contains file dialogs with filter property in two places. The way to fix the code is to utilize the Extract Method shown in section 2.1.

code format="csharp" private void rbCS_CheckedChanged(object sender, EventArgs e) { if (rbCS.Checked) { //cdp = csp; gbCode.Text = "C# Code"; mnuNew_Click(null, null); openFileDialog1.Filter = "C# (*.cs)|*.cs|All Files (*.*)|*.*"; saveFileDialog1.Filter = openFileDialog1.Filter; ChangeHighlightingStrategy("C#"); }   }

private void rbVB_CheckedChanged(object sender, EventArgs e) { if (rbVB.Checked) { //cdp = vbp; gbCode.Text = "VB.NET Code"; mnuNew_Click(null, null); openFileDialog1.Filter = "VB.NET (*.vb)|*.vb|All Files (*.*)|*.*"; saveFileDialog1.Filter = openFileDialog1.Filter; ChangeHighlightingStrategy("VBNET"); }   }

code

** 1.2 Long Method**
Extract Method is useful when the code is similar but not the same and it facilitate in shortening a method. Shorter methods make the code cleaner and easier to follow. Another best approach to utilize in shorting a long method is to use the Replace Method with Method Object which turns local variables into fields on the method object. The snippet code in Form1.cs shown below from Mini C Sharp lab contains a long method call Run. In section 2.02, Replace Method with Method object is utilized in shortening the Run method.

code format="csharp" /// /// Compiles and Runs the specified code. /// /// The code.

public void Run(string code) { timer1.Enabled = false;

rtbOutput.Clear; rtbOutput.SelectionColor = Color.Brown;

cp.ReferencedAssemblies.Clear; //2008-05-29 by Jeffrey //Support additional reference //REFDLL System.Web;System.Data.OracleClient

cp.ReferencedAssemblies.Add("System.dll"); //Add ref to Linq by default when use .NET 3.5 if (rbNET35.Checked) { //cp.ReferencedAssemblies.Add("System.Linq.dll"); cp.ReferencedAssemblies.Add("System.Data.Linq.dll"); cp.ReferencedAssemblies.Add("System.Xml.Linq.dll"); cp.ReferencedAssemblies.Add("System.Core.dll"); }

Match m = null; if ((m = Regex.Match(code, "(?ims)^[/']{2}REFDLL (? .+?)$")).Success) { foreach (string refDll in m.Groups["ref"].Value.Split(new char[] { ';', ',' })) { //2008-06-18 by Jeffrey, remove redundant \r string mdfyRefDll = refDll.Replace("\r", "").Replace("\n", ""); //trim the ending .dll if exists if (mdfyRefDll.ToLower.EndsWith(".dll")) mdfyRefDll = mdfyRefDll.Substring(0, mdfyRefDll.Length - 4); string lcRefDll = mdfyRefDll.ToLower; if (lcRefDll == "system.data.linq" || lcRefDll == "system"             || lcRefDll == "system.xml.linq" || lcRefDll == "system.core") continue; cp.ReferencedAssemblies.Add(mdfyRefDll + ".dll"); }     }

cp.GenerateInMemory = true;

Stopwatch timer = new Stopwatch; timer.Start; //use CodeDomProvider, support both C# and VB.NET cdp = rbVB.Checked ? (CodeDomProvider)vbp : csp; cr = cdp.CompileAssemblyFromSource(cp, code); timer.Stop;

Console.SetOut(sw);

if (cr.Errors.HasErrors) { output(Color.Red, String.Format("Compilation Errors:{0}", Environment.NewLine)); for (int x = 0; x < cr.Errors.Count; x++) output(Color.Red, String.Format("Line: {0} Column {2} - {1}{3}",

cr.Errors[x].Line, cr.Errors[x].ErrorText, cr.Errors[x].Column, Environment.NewLine)); Console.SetOut(origConOut); //2008-09-01 by Jeffrey //close the application if failed to compile in batch mode if (_isBatchMode) this.Close; return; }

output(Color.Green, String.Format("Built successfully in ms!{0}", Environment.NewLine), timer.ElapsedMilliseconds); output(Color.Black, String.Format("Prepare to run...{0}{1}{0}", Environment.NewLine, splitLine));

rtbOutput.SelectionColor = Color.Blue;

try { Assembly asm = cr.CompiledAssembly; codeObj = asm.CreateInstance("CSharpLab"); if (codeObj == null) { outputError("Class 'CSharpLab' not found!"); Console.SetOut(origConOut); return; }     } catch (Exception ex) { outputError(String.Format("Runtime error!{0}", Environment.NewLine) + ex.Message); }

runThread = new System.Threading.Thread(new System.Threading.ThreadStart(threadRun)); timer1.Enabled = true; runThread.Start; } code

**1.3 Large Class**
When a class has too many instance variables, duplicated code is most likely to be there which can cause confusion in code. A better way to fix a large class is to use an Extract Class which is a useful technique in moving the relevant fields and method from the old class into the new class. The snippet code in Run.cs shown below from Mini C Sharp lab is utilized in which I will move some responsibility from Run.cs class to another class called FormatControls.c in section 2.3.

code format="csharp" using System; using System.Collections.Generic; using System.Text; using System.Drawing; using System.Drawing.Printing; using System.Windows.Forms; using System.Diagnostics; using System.Reflection; using System.IO; using System.CodeDom.Compiler; using ICSharpCode.TextEditor.Actions; using ICSharpCode.TextEditor.Document; using Microsoft.CSharp; using System.Text.RegularExpressions; using Microsoft.VisualBasic;

namespace MiniCSharpLab {   class Run {           private string code; private MemoryStream ms = new MemoryStream; private StreamWriter sw = null; private CSharpCodeProvider csp = new CSharpCodeProvider; private VBCodeProvider vbp = new VBCodeProvider; private CodeDomProvider cdp = null; public CompilerParameters cp = new CompilerParameters; private CompilerResults cr = null; private object codeObj = null; private System.Threading.Thread runThread = null; private TextWriter origConOut = Console.Out; private const int SPLIT_WIDTH = 50;

private bool isBatchMode; string splitLine = new string('=', SPLIT_WIDTH);

public Run(string _code) {               this.code = _code; }

// Original body of Run method is now in Format

public void Format {

Form1 _form1 = new Form1; Form1 _output = new Form1; Form1 _rbNet = new Form1; Form1 _rbVb = new Form1; Form1 _close = new Form1; Form1 _threadRun = new Form1; Form1 _outputError = new Form1; Form1 _out = new Form1;

_form1.timer1.Enabled = false;

_output.rtbOutput.Clear; _output.rtbOutput.SelectionColor = Color.Brown;

cp.ReferencedAssemblies.Clear; //2008-05-29 by Jeffrey //Support additional reference //REFDLL System.Web;System.Data.OracleClient

cp.ReferencedAssemblies.Add("System.dll"); //Add ref to Linq by default when use .NET 3.5 if (_rbNet.rbNET35.Checked) {             //cp.ReferencedAssemblies.Add("System.Linq.dll"); cp.ReferencedAssemblies.Add("System.Data.Linq.dll"); cp.ReferencedAssemblies.Add("System.Xml.Linq.dll"); cp.ReferencedAssemblies.Add("System.Core.dll"); }

Match m = null; if ((m = Regex.Match(code, "(?ims)^[/']{2}REFDLL

(? .+?)$")).Success)               {                    foreach (string refDll in

m.Groups["ref"].Value.Split(new char[] { ';', ',' }))                   {                        //2008-06-18 by Jeffrey, remove redundant \r

string mdfyRefDll = refDll.Replace("\r", "").Replace("\n", ""); //trim the ending .dll if exists if (mdfyRefDll.ToLower.EndsWith(".dll"))

mdfyRefDll = mdfyRefDll.Substring(0, mdfyRefDll.Length - 4); string lcRefDll = mdfyRefDll.ToLower; if (lcRefDll == "system.data.linq" || lcRefDll == "system"  || lcRefDll == "system.xml.linq" || lcRefDll == "system.core") continue;

cp.ReferencedAssemblies.Add(mdfyRefDll + ".dll"); }               }

cp.GenerateInMemory = true;

Stopwatch timer = new Stopwatch; timer.Start; //use CodeDomProvider, support both C# and VB.NET cdp = _rbVb.rbVB.Checked ? (CodeDomProvider)vbp : csp; cr = cdp.CompileAssemblyFromSource(cp, code); timer.Stop; Console.SetOut(sw);

if (cr.Errors.HasErrors) {

_out.output(Color.Red, String.Format("Compilation

Errors:{0}", Environment.NewLine));                   for (int x = 0; x < cr.Errors.Count; x++) _out.output(Color.Red, String.Format("Line: {0} Column {2} - {1}{3}",

cr.Errors[x].Line, cr.Errors[x].ErrorText, cr.Errors[x].Column,

Environment.NewLine)); Console.SetOut(origConOut); //2008-09-01 by Jeffrey //close the application if failed to compile in batch mode if (_isBatchMode) _close.Close; return;

}

_out.output(Color.Green, String.Format("Built

successfully in ms!{0}", Environment.NewLine),

timer.ElapsedMilliseconds); _out.output(Color.Black, String.Format("Prepare to run...{0}{1}{0}",

Environment.NewLine, splitLine));

_output.rtbOutput.SelectionColor = Color.Blue;

try {                   Assembly asm = cr.CompiledAssembly; codeObj = asm.CreateInstance("CSharpLab"); if (codeObj == null) {

_outputError.outputError("Class 'CSharpLab' not found!"); Console.SetOut(origConOut); return; }               }                catch (Exception ex) {                   _outputError.outputError(String.Format("Runtime

error!{0}", Environment.NewLine) + ex.Message);               }

runThread = new System.Threading.Thread(new

System.Threading.ThreadStart(_threadRun.threadRun));

_form1.timer1.Enabled = true; runThread.Start;

}

public bool _isBatchMode {               get { return isBatchMode; }

set { isBatchMode = value; } }

}

} code

In this section refactoring techniques are applied to an open source software from chapter 3 to chapter 7 to make the code easier to follow and to improve its design.

** 2.1 Extract Method **
Extract Method is utilized when there is a code fragment that can be grouped together. I took the openfileDialogs1.Filter and savefileDialog1.Filter and put them in its own method. The snippet code in section 1.0.1 turns the file dialogs into a method and can easily be utilized as FileDialogs(language) in the form1.cs. This takes care of the duplicated code since the same code structure is used in more than one place, I utilized Extract Method as shown below:

code format="csharp" private void rbCS_CheckedChanged(object sender, EventArgs e) { if (rbCS.Checked) { //cdp = csp; gbCode.Text = "C# Code"; mnuNew_Click(null, null); string language = "C# (*.cs)|*.cs|All Files (*.*)|*.*";

FileDialogs(language); ChangeHighlightingStrategy("C#"); }   }

private void rbVB_CheckedChanged(object sender, EventArgs e) { if (rbVB.Checked) { //cdp = vbp; gbCode.Text = "VB.NET Code"; mnuNew_Click(null, null); string language = "VB.NET (*.vb)|*.vb|All Files (*.*)|*.*";

FileDialogs(language); ChangeHighlightingStrategy("VBNET");

}

}   public void FileDialogs(string language) {

openFileDialog1.Filter = language; saveFileDialog1.Filter = openFileDialog1.Filter;

}

code

** 2.2 Replace Method with Method Object **
Long methods are hard to understand and difficult to modify since the code including variables and parameters are usually not organized appropriately. The long method Run in section 1.02 is refactored where a new class called Run.cs is created which is named after the long method. The new class contains a constructor that takes the parameter and a method called Format is placed in Run.cs class as shown below:

code format="csharp" using System; using System.Collections.Generic; using System.Text; using System.Drawing; using System.Drawing.Printing; using System.Windows.Forms; using System.Diagnostics; using System.Reflection; using System.IO; using System.CodeDom.Compiler; using ICSharpCode.TextEditor.Actions; using ICSharpCode.TextEditor.Document; using Microsoft.CSharp; using System.Text.RegularExpressions; using Microsoft.VisualBasic;

namespace MiniCSharpLab {   class Run {           private string code;

private MemoryStream ms = new MemoryStream; private StreamWriter sw = null; private CSharpCodeProvider csp = new CSharpCodeProvider; private VBCodeProvider vbp = new VBCodeProvider; private CodeDomProvider cdp = null; public CompilerParameters cp = new CompilerParameters; private CompilerResults cr = null; private object codeObj = null; private System.Threading.Thread runThread = null; private TextWriter origConOut = Console.Out; private const int SPLIT_WIDTH = 50;

private bool isBatchMode; string splitLine = new string('=', SPLIT_WIDTH);

public Run(string _code) {               this.code = _code; }

// Original body of Run method is now in Format public void Format {               Form1 _form1 = new Form1; Form1 _output = new Form1; Form1 _rbNet = new Form1; Form1 _rbVb = new Form1; Form1 _close = new Form1; Form1 _threadRun = new Form1; Form1 _outputError = new Form1; Form1 _out = new Form1;

_form1.timer1.Enabled = false;

_output.rtbOutput.Clear; _output.rtbOutput.SelectionColor = Color.Brown;

cp.ReferencedAssemblies.Clear; //2008-05-29 by Jeffrey //Support additional reference //REFDLL System.Web;System.Data.OracleClient

cp.ReferencedAssemblies.Add("System.dll"); //Add ref to Linq by default when use .NET 3.5 if (_rbNet.rbNET35.Checked) {             //cp.ReferencedAssemblies.Add("System.Linq.dll"); cp.ReferencedAssemblies.Add("System.Data.Linq.dll"); cp.ReferencedAssemblies.Add("System.Xml.Linq.dll"); cp.ReferencedAssemblies.Add("System.Core.dll"); }

Match m = null; if ((m = Regex.Match(code, "(?ims)^[/']{2}REFDLL

(? .+?)$")).Success)               {                foreach (string refDll in

m.Groups["ref"].Value.Split(new char[] { ';', ',' }))                   {        //2008-06-18 by Jeffrey, remove redundant \r string mdfyRefDll = refDll.Replace("\r", "").Replace("\n", "");        //trim the ending .dll if exists     if (mdfyRefDll.ToLower.EndsWith(".dll"))

mdfyRefDll = mdfyRefDll.Substring(0, mdfyRefDll.Length - 4); string lcRefDll = mdfyRefDll.ToLower; if (lcRefDll == "system.data.linq" || lcRefDll == "system" || lcRefDll == "system.xml.linq" || lcRefDll == "system.core") continue; cp.ReferencedAssemblies.Add(mdfyRefDll + ".dll"); }               }

cp.GenerateInMemory = true;

Stopwatch timer = new Stopwatch; timer.Start; //use CodeDomProvider, support both C# and VB.NET cdp = _rbVb.rbVB.Checked ? (CodeDomProvider)vbp : csp; cr = cdp.CompileAssemblyFromSource(cp, code); timer.Stop; Console.SetOut(sw);

if (cr.Errors.HasErrors) {

_out.output(Color.Red, String.Format("Compilation Errors:{0}",

Environment.NewLine)); for (int x = 0; x < cr.Errors.Count; x++) _out.output(Color.Red, String.Format("Line: {0} Column {2} - {1}{3}",

cr.Errors[x].Line, cr.Errors[x].ErrorText, cr.Errors[x].Column,

Environment.NewLine)); Console.SetOut(origConOut); //2008-09-01 by Jeffrey //close the application if failed to compile in batch mode if (_isBatchMode) _close.Close; return;

}

_out.output(Color.Green, String.Format("Built successfully in

ms!{0}", Environment.NewLine), timer.ElapsedMilliseconds);

_out.output(Color.Black, String.Format("Prepare to run...{0}{1}{0}",

Environment.NewLine, splitLine));

_output.rtbOutput.SelectionColor = Color.Blue;

try {          Assembly asm = cr.CompiledAssembly; codeObj = asm.CreateInstance("CSharpLab"); if (codeObj == null) {

_outputError.outputError("Class 'CSharpLab' not found!"); Console.SetOut(origConOut); return; }               }   catch (Exception ex) { _outputError.outputError(String.Format("Runtime error!{0}",

Environment.NewLine) + ex.Message); }

runThread = new System.Threading.Thread(new

System.Threading.ThreadStart(_threadRun.threadRun));               _form1.timer1.Enabled = true;                runThread.Start;

}

public bool _isBatchMode {               get { return isBatchMode; }

set { isBatchMode = value; } }

}

code The Form1.cs contains the old method which is replaced with one that creates the new object and calls Format. The old method is shorted to few lines of code and this is a result of using the refactoring technique Replace Method with Method object.

code format="csharp" public void Run (string code) {       Run _run = new Run(code); _run.Format; }

code

**2.3 Extract Class**
The Extract class shrinks down the large class by moving responsibilities to another class which is important since as responsibility grows, the class becomes too complicated. The Run.cs contains lots of data that needs to separated and moved to a new class with appropriate name. I called the new class Format Controls.cs which takes care of the controls utilized in the form1.cs The Run.cs shown below is much shorter and cleaner with instance variables are appropriately organized.

code format="csharp" using System; using System.Collections.Generic; using System.Text; using System.Drawing; using System.Drawing.Printing; using System.Windows.Forms; using System.Diagnostics; using System.Reflection; using System.IO; using System.CodeDom.Compiler; using ICSharpCode.TextEditor.Actions; using ICSharpCode.TextEditor.Document; using Microsoft.CSharp; using System.Text.RegularExpressions; using Microsoft.VisualBasic;

namespace MiniCSharpLab {   class Run {           private string code; private MemoryStream ms = new MemoryStream; private StreamWriter sw = null; private CSharpCodeProvider csp = new CSharpCodeProvider; private VBCodeProvider vbp = new VBCodeProvider; private CodeDomProvider cdp = null; public CompilerParameters cp = new CompilerParameters; private CompilerResults cr = null; private object codeObj = null; private System.Threading.Thread runThread = null; private TextWriter origConOut = Console.Out; private const int SPLIT_WIDTH = 50;

private bool isBatchMode; string splitLine = new string('=', SPLIT_WIDTH);

public Run(string _code) {               this.code = _code; }           // Original body of Run method is now in Format

public void Format {               FormatControls fc = new FormatControls(cp, csp, vbp); fc.Controls(code); }

public bool _isBatchMode {               get { return isBatchMode; }

set { isBatchMode = value; } }       }    } code The Run.cs is now much cleaner and shrinked to a smaller class with less responsibilities; The format method shown below in Run.cs is also much cleaner and takes care of too many variables and utilize only those variables needed for the Run.cs.

code format="csharp" public void Format {               FormatControls fc = new FormatControls(cp, csp, vbp); fc.Controls(code); } code

The instance variables used for controls included in Run.cs are moved into FormatControl.cs class and therefore the new class is given the responsibility of managing controls utilized on the Form1.cs. A constructor is created to take in the parameters appropriately and reponsibilities are split from Run.cs to ControlForm.cs as shown below:

code using System; using System.Collections.Generic; using System.Text; using System.Drawing; using System.Drawing.Printing; using System.Windows.Forms; using System.Diagnostics; using System.Reflection; using System.IO; using System.CodeDom.Compiler; using ICSharpCode.TextEditor.Actions; using ICSharpCode.TextEditor.Document; using Microsoft.CSharp; using System.Text.RegularExpressions; using Microsoft.VisualBasic;

namespace MiniCSharpLab {   class FormatControls {

private CompilerResults cr = null; private CodeDomProvider cdp = null; private TextWriter origConOut = Console.Out; private object codeObj = null; private const int SPLIT_WIDTH = 50; string splitLine = new string('=', SPLIT_WIDTH); private System.Threading.Thread runThread = null; public Run _run;

CompilerParameters cp; CSharpCodeProvider csp; StreamWriter sw = null; VBCodeProvider vbp;

public FormatControls(CompilerParameters parameter, CSharpCodeProvider

provider, VBCodeProvider visual)                {                        this.cp= parameter;                        this.csp = provider;                        this.vbp = visual;                 }

public void Controls(string code) {           Form1 _form1 = new Form1; Form1 _output = new Form1; Form1 _rbNet = new Form1; Form1 _rbVb = new Form1; Form1 _close = new Form1; Form1 _threadRun = new Form1; Form1 _outputError = new Form1; Form1 _out = new Form1;

_form1.timer1.Enabled = false;

_output.rtbOutput.Clear; _output.rtbOutput.SelectionColor = Color.Brown;

cp.ReferencedAssemblies.Clear; //2008-05-29 by Jeffrey //Support additional reference //REFDLL System.Web;System.Data.OracleClient

cp.ReferencedAssemblies.Add("System.dll"); //Add ref to Linq by default when use .NET 3.5 if (_rbNet.rbNET35.Checked) {               //cp.ReferencedAssemblies.Add("System.Linq.dll"); cp.ReferencedAssemblies.Add("System.Data.Linq.dll"); cp.ReferencedAssemblies.Add("System.Xml.Linq.dll"); cp.ReferencedAssemblies.Add("System.Core.dll"); }

Match m = null; if ((m = Regex.Match(code, "(?ims)^[/']{2}REFDLL (? .+?)$")).Success) {               foreach (string refDll in m.Groups["ref"].Value.Split(new char[] { ';', ',' })) {                   //2008-06-18 by Jeffrey, remove redundant \r string mdfyRefDll = refDll.Replace("\r", "").Replace("\n", ""); //trim the ending .dll if exists if (mdfyRefDll.ToLower.EndsWith(".dll"))

mdfyRefDll = mdfyRefDll.Substring(0, mdfyRefDll.Length - 4); string lcRefDll = mdfyRefDll.ToLower; if (lcRefDll == "system.data.linq" || lcRefDll == "system" || lcRefDll == "system.xml.linq" || lcRefDll == "system.core")

continue;

cp.ReferencedAssemblies.Add(mdfyRefDll + ".dll"); }           }

cp.GenerateInMemory = true;

Stopwatch timer = new Stopwatch; timer.Start; //use CodeDomProvider, support both C# and VB.NET cdp = _rbVb.rbVB.Checked ? (CodeDomProvider)vbp : csp; cr = cdp.CompileAssemblyFromSource(cp, code); timer.Stop; Console.SetOut(sw);

if (cr.Errors.HasErrors) {

_out.output(Color.Red, String.Format("Compilation Errors:{0}",

Environment.NewLine)); for (int x = 0; x < cr.Errors.Count; x++)

_out.output(Color.Red, String.Format("Line: {0} Column {2} - {1}{3}",

cr.Errors[x].Line, cr.Errors[x].ErrorText, cr.Errors[x].Column,

Environment.NewLine)); Console.SetOut(origConOut); //2008-09-01 by Jeffrey //close the application if failed to compile in batch mode

if (_run._isBatchMode) _close.Close; return;

}

_out.output(Color.Green, String.Format("Built successfully in

ms!{0}", Environment.NewLine), timer.ElapsedMilliseconds);

_out.output(Color.Black, String.Format("Prepare to run...{0}{1}{0}",

Environment.NewLine, splitLine));

_output.rtbOutput.SelectionColor = Color.Blue;

try {               Assembly asm = cr.CompiledAssembly; codeObj = asm.CreateInstance("CSharpLab"); if (codeObj == null) {

_outputError.outputError("Class 'CSharpLab' not found!");

Console.SetOut(origConOut); return; }           }            catch (Exception ex) {               _outputError.outputError(String.Format("Runtime error!{0}", Environment.NewLine) + ex.Message); }

runThread = new System.Threading.Thread(new System.Threading.ThreadStart(_threadRun.threadRun)); _form1.timer1.Enabled = true; runThread.Start; }   } }

code

3.0 Summary
Refactoring helps the program to function efficiently and faster by improving readability, reducing bugs and improving the design itself. It is important to avoid duplication in the code since it leads to longer classes or methods which can then introduce bugs and errors into the program. Extract class can be utilized for large classes where a new class can be created to manage certain responsiblities. A FormatControl.cs is created to manage controls on the form1.cs; To minimize the errors in the Long method in the Form1.cs,I utilize the refactoring technique called Replacing Method with Method object which takes care of temporary variables and parameters. I created a new class which contain a constructor that takes the parameter and a method call Format and placed it in to Run.cs; the Extract Method can then be utilized in Run.cs to avoid code duplication. In Summary, it is important to know how to operate the mechanics of a refactoring so that it can be easier to identify the smells in the code.