Thursday, June 27, 2013

Developer Common Mistake

String Concatenation

They Used String to append , here each time when you add something , a new address is being allocated in the memory.
Whereas in String Builder same address is gets modified.

//INCORRECT
List values = new List(){"This ","is ","Sparta ","!"};
string outputValue = string.Empty;
foreach (var value in values)
{
   outputValue += value;
}


//CORRECT
StringBuilder outputValueBuilder = new StringBuilder();
foreach (var value in values)
{
   outputValueBuilder.Append(value);
}

Not using mapping for rewriting properties

Not Using powerful C# mapper : - http://automapper.org/ 

Using ‘foreach’ instead of ‘for’ for anything else than collections

IF it not a collection then use for loop  is much more efficient than using the ‘foreach’ loop.



No comments:

Post a Comment