Some Examples Of How To Do Good Programming In C#

There are multiple ways to write code - novice, intermediate, and ninja-level. Let's see some examples below and see how good coding practices can improve code performance as well. Based on the examples below, I urge each developer to review their codes at least once or twice to improve it.
 
Let us say we have a method.
  1. private List<Data> getdata()  
  2. {  
  3.    return new List<Data>(); // Let us suppose this method returns 10000 rows of Data Object  
  4. }  
Now, the coder is tasked to iterate over it.
  1. for( i=0; i < getdata (); i++) { // }  
The above method was written casually or it may be there wasn't much time to spend on it or perhaps the coder was new to programming.
 
After a review, it was changed to below,
  1. var data=GetData();  
  2. for( i=0; i < data ; i++) { // }   
The call to GetData() is reduced to one, and thus it increased performance overheads.
 
Let us take another good example, it involves if-else statements.
 
See code below,
  1. if (IsolatedStorageWrapper.LoadString(“ShowTipsAndTricks”) == “”)  
  2. {  
  3.    IsolatedStorageWrapper.SaveString(“ShowTipsAndTricks”, “true”);  
  4. }  
  5. else  
  6. {  
  7.    IsolatedStorageWrapper.SaveString(“ShowTipsAndTricks”, “”);  
  8. }  
After review,
  1. IsolatedStorageWrapper.SaveString(“ShowTipsAndTricks”, IsolatedStorageWrapper.LoadString(“ShowTipsAndTricks”) == “” ? “true” : “”);     
After another review,
  1. var loadedStringValue=IsolatedStorageWrapper.LoadString(“ShowTipsAndTricks”) == “” ? “true” : “”;  
  2.   
  3. IsolatedStorageWrapper.SaveString(“ShowTipsAndTricks”, loadedStringValue);  
After another review,
  1. var loadedStringValue = IsolatedStorageWrapper.LoadString( “ShowTipsAndTricks” ) == “” ? “true” : “”;  
  2. IsolatedStorageWrapper.SaveString( “ShowTipsAndTricks” , loadedStringValue );