Hi,
In this article I’ll list some Common Mistakes that .NET Developers might make ..
UPDATED : According to some comments, I tried to make this post more clear.
1 – Don’t Throw Exception
Wrong :
public void SomeMethod()
{
try
{
// Do Something which failed
}
catch (Exception ex)
{
throw;
}
}
Right :
public void SomeMethod()
{
try
{
// Do Something which failed
}
catch (Exception ex)
{
throw ex;
}
}
UPDATED : The first code will reset the stack trace to the throw point whereas the second code will maintain the original Stack Trace.
2 - Don’t use IDisposable Objects
UPDATED : You may use the “Using” Block. This will ensure that your object will be Disposed after using it.
Wrong :
public void SomeMethod()
{
MyDisposableObject obj = new MyDisposableObject();
obj.DoSomething();
obj.Dispose();
}
Right :
public void SomeMethod()
{
using (MyDisposableObject obj = new MyDisposableObject())
{
obj.DoSomething();
}
}
3 – Use Public Fields
UPDATED : By Using Public Fields, you have no control of the Input Value unlike Properties.
Wrong :
public class MyClass
{
public string _myField;
}
public static void Main()
{
MyClass obj = new MyClass();
obj._myField = "Test";
}
Right :
public class MyClass
{
private string _myField;
public string MyField
{
get { return _myField; }
set { _myField = value; }
}
}
// OR With C# 3 Feature
public class MyClass
{
public string MyField
{
get;
set;
}
}
public static void Main()
{
MyClass obj = new MyClass();
obj.MyField = "Test";
}
4 – Don’t Throw Exception on Try {} Catch{} Block
UPDATED : This may drop an unwanted Exception and make your Application unstable ...
Wrong :
public static void Main()
{
try
{
// Do Something
}
catch { }
}
Right :
public static void Main()
{
try
{
// Do Something
}
catch (Exception ex)
{
throw ex;
}
}
5 – Don’t Using “Using” Block for IDisposable Objects
UPDATED : This will ensure that your object will be Disposed after using it.
Wrong :
public void SomeMethod()
{
MyDisposableObject obj = new MyDisposableObject();
obj.DoSomething();
obj.Dispose();
}
Right :
public void SomeMethod()
{
using (MyDisposableObject obj = new MyDisposableObject())
{
obj.DoSomething();
}
}
6 – Use “as” keyword everywhere
Wrong :
public class MyObject
{
// Some Properties
}
public static void Main()
{
MyObject obj3 = GetObject() as MyObject;
}
Right :
public class MyObject
{
// Some Properties
}
public static void Main()
{
MyObject obj3 = (MyObject)GetObject();
}
I think that you must in most of case know what you do and don't expect that you cast failed
7 – Concat Strings without StringBuilder
UPDATED : Straight Strings Concatenation will instanciate each time a new String in Memory which might be down performances.
However StringBuilder doesn’t instanciate new String on each Append but reallocate String length.
Wrong :
string s = "This";
s += "a";
s += "very";
s += "bad";
s += "code";
Right :
StringBuilder sb = new StringBuilder();
sb.Append("This");
sb.Append("is");
sb.Append("Better!");
8 – Use Hard Coded Numbers
UPDATED : If you use Hard Coded Numbers for Comparison it should be understandable for you, but not for another developer who comes after you.
Moreover, it’s pretty much Readable with Enumerations.
Wrong :
public static void Main()
{
if (CurrentMode == 1)
{
// Do Something
}
else if (CurrentMode == 2)
{
// Do Something
}
}
Right :
public static void Main()
{
if (CurrentMode == Mode.Add)
{
// Do Something
}
else if (CurrentMode == Mode.Delete)
{
// Do Something
}
}
9 – Forgot to Check Null !
Wrong :
MyObject obj = Builder.DoSometing();
String s = obj.Name; // May Throw NullException !
Right :
MyObject obj = Builder.DoSometing();
String s = String.Empty;
if (obj != null)
{
s = obj.Name; // Correct
}
10 – Unnecessary Initializations
UPDATED : Unnecessary Initialization might slow down performances because you instantiate your objet 2 times.
Wrong :
MyObject obj = new MyObject(); // Bad Code !
obj = Builder.DoSometing();
Right :
MyObject obj = null; // Good
obj = Builder.DoSometing();
MyObject obj = Builder.DoSometing(); // Better !
Hope this help’s ! :)
Views(2807)

