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.
- private List<Data> getdata()
- {
- return new List<Data>();
- }
Now, the coder is tasked to iterate over it.
- 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,
- var data=GetData();
- 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,
- if (IsolatedStorageWrapper.LoadString(“ShowTipsAndTricks”) == “”)
- {
- IsolatedStorageWrapper.SaveString(“ShowTipsAndTricks”, “true”);
- }
- else
- {
- IsolatedStorageWrapper.SaveString(“ShowTipsAndTricks”, “”);
- }
After review,
- IsolatedStorageWrapper.SaveString(“ShowTipsAndTricks”, IsolatedStorageWrapper.LoadString(“ShowTipsAndTricks”) == “” ? “true” : “”);
After another review,
- var loadedStringValue=IsolatedStorageWrapper.LoadString(“ShowTipsAndTricks”) == “” ? “true” : “”;
-
- IsolatedStorageWrapper.SaveString(“ShowTipsAndTricks”, loadedStringValue);
After another review,
- var loadedStringValue = IsolatedStorageWrapper.LoadString( “ShowTipsAndTricks” ) == “” ? “true” : “”;
- IsolatedStorageWrapper.SaveString( “ShowTipsAndTricks” , loadedStringValue );