This blog will hold the collection of refactoring and performance techniques.
What is Refactoring?
Refactoring is a process to improve upon the design of existing code, while preserving its intended functionality.
What is Refactoring?
Refactoring is a process to improve upon the design of existing code, while preserving its intended functionality.
Bad Code Smells:
Refactoring Techniques:
Here, you can find an excellent list of refactoring techniques with examples. In below, I am going to duplicate some:
1)
Example:
List
Bad: items.Count
Good: items.Length
Reason:
Length property is faster than Count performance-wise.
2)
Example:
Bad:
int[] items = new int[2];
Good:
List
Reasons:
1. Using Generic list gives you more power and control over the list
2. You do not have to specify the length of the array at the time of its creation
3) Extract Constant
Bad:
public static double CalcCircumference(double diameter)
{ return 3.14 * diameter; }
Good:
public const double PI = 3.14;
public static double CalcCircumference (double diameter)
{ return PI * diameter; }
Reasons:
1. If we need to change PI value, we only need to change it in one place
2. Increased readibility
4) Extract Method
http://www.xtreme-simplicity.net/ExtractMethod.htm
5) Decompose IF Conditional
http://www.xtreme-simplicity.net/DecomposeConditiona.htm
6) Extract Variable
http://www.xtreme-simplicity.net/ExtractVariable.htm
7) Encapsulate Field:
http://msdn.microsoft.com/en-us/library/ms379618.aspx
8) Extract Interface:
http://msdn.microsoft.com/en-us/library/ms379618.aspx
9) Reorder Method Parameters
http://msdn.microsoft.com/en-us/library/ms379618.aspx
10) Rename (type, method, class, namespace, variable)
http://msdn.microsoft.com/en-us/library/ms379618.aspx
11) Promote Local Variable to Parameter
http://msdn.microsoft.com/en-us/library/ms379618.aspx
Reasons:
1) To inject dependency (object, type, etc)
12) Use StringBuilder instead of String
For another list of refactoring techniques, please see http://www.refactoring.com/catalog/index.html
Refactoring Tools:
http://www.jetbrains.com/resharper/
http://www.devexpress.com/
http://www.xtreme-simplicity.net/CSharpRefactory.htm
Learn More:
- An excellent list of refactoring techniques
- Another excellent refactoring source with examples
- http://msdn.microsoft.com/en-us/library/ms379618.aspx
- http://msdn.microsoft.com/en-us/library/ms973839.aspx
- http://msdn.microsoft.com/en-us/library/ms998530.aspx (Performance)
- http://dotnet.sys-con.com/node/46342 (Performance)
- http://books.google.co.uk/books?ct=result&q=refactoring+techniques&hl=en
- http://books.google.co.uk/books?ct=result&q=C%23+performance
- http://se-radio.net/podcast/2007-02/episode-46-refactoring-pt-1 (Podcast - Part1)
- http://se-radio.net/podcast/2007-05/episode-55-refactoring-pt-2 (Podcast - Part2)