How Does Your "Code Smell"

So how does your "Code smell"

 
In computer programming, code smell is any symptom in the source code of a program that possibly indicates a deeper problem. We are going to look at some of them here.
 
1. Catch me if can you handle: Whenever you are doing an exception handling in your code do not just use the throw keyword to throw the exception to your parent method but instead throw with the current exception so that the current stack trace information is also sent and you do not lose original exception information.
 
Do not use 
 
catch (Exception ex)
{
    throw ;
}
 
instead, use
 
catch (Exception ex)
{
    throw ex;
}
 
2. InPlace Replace: We have used the string functions so many times but one among them is the Replace function which returns back the replaced string instead of in-place replacing the string.
 
For example:
  1. string s = "this is my string";  
  2. s.Replace("this""that");  //this will not replace the word  
  3. s = s.Replace("this""that");  //this will replace the word 
3. Number Magix: There are instances where we need to use hardcoded numbers or strings which are also known as "magic numbers/strings" in your code. Try to avoid scattering them in your code but instead create enums or store it in the config files.
  1. // bad code  
  2. if (day == 3) { ... } else if (day == 4) { ... }  
  3. //Use Enumerations wherever possible, so the meaning and not the number, is displayed in the code, it helps debugging and readability  
  4. for you and others who may maintain your code:  
  5.     // good  code  
  6.     if (day == MyEnum.ThirdDay) { ... }  
  7. else if (mode == MyEnum.FourthDay) { ... } 
4. Builder by your side: One should whenever trying to concatenate a lot of string should not try to use the string object since that creates a lot of footprints in your memory but instead use StringBuilder 
  1. // bad code  
  2. string s = string.Empty;  
  3. for (int i = 0; i < 100; i++) {  
  4.     s = s + "This is number " + i.ToString();  
  5. }  
  6. //good code  
  7. StringBuilder mBuilder = new StringBuilder();  
  8. for (int i = 0; i < 100; i++) {  
  9.     mBuilder.Append("This is number " + i.ToString());  
Please also look into String.Concat() if you want to concat less number of string as that would be more ideal.
 
5. Wire/UnWire: Whenever you are writing code to Wire an event also remember to unwire that event as that might give you unexpected results. I have seen comments which discuss that unwiring is taken care of by the garbage collector but that is not the case. So one should unhook event handlers appropriately after wiring them.
 
6. What to catch finally: Many times we have seen programmers use the try-catch block but they do not write anything in the catch block as they just want to pass the code, this may hold true in a case which I will discuss below but ideally one should not do the following:
  1. // bad code  
  2. try  
  3. {  
  4.     // my code may have errors here  
  5.  }  
  6. catch (Exception ex)  
  7. {  
  8.     //I do nothing with this error, just gulp it.  
You should do some logging of the exception or throwing the exception to the parent caller. The case where this could be a possibility is below, but again there are ways to overcome this scenario and if you happen to fall in one of these please leave a comment on what you did:
  1. try {  
  2.     // my code may contain an error.  
  3. catch (Exception ex) {  
  4.     try {  
  5.         LogMyException(ex); // this might also throw an error  
  6.     } catch (Exception ex) {  
  7.         // what to do here now?  
  8.     }  
Also, I have seen coders writing catch block always use generic Exception object and not specific to the expected error, one should first try to catch the specific error and also keep the generic as a fall back like below:
  1. catch (OutOfMemoryException outmemex) {  
  2.     // do something to release the memory, logging  
  3. catch (Exception ex) {  
  4.     // I don't know why an error has occurred, log it.  
  5.  
How can one not use the finally block which guarantees execution of the piece in spite of an exception so please do not forget to add finally block to release objects and connections used in your code.
 
7. Warn ok please: I am sure most of the coders here don't even bother about Compiler Warnings they receive when they are compiling their solutions, it is also important for you to look into the compiler warnings that are showing up. 
 
8. Initialization: We have seen in our code that many time that we do unnecessary initialization of objects like the one below:
  1. DataTable mytable = new DataTable();  // Initialization is not necessary  
  2. mytable = GetDataTable();  
  3. one should instead use  
  4. DataTable mytable = GetDataTable (); 
9. Get, Set, Tools: Use more productivity tools and addins that makes your life easier while coding,  few of them
 
and you may find this link very helpful:
 
10. Are you NULL: So many times we get a null reference exception and that is probably because we always assume that there will be no null data, but that is always not true. One should always check for null before they use the object just to avoid these situations and have an alternate route to your code.
  1. mType mobj = x as mType;  
  2. if (mobj != null) {  
  3.     //more code here  
11. Follow standards and conventions while coding in not doing so you are taking a lot of others time in your team to understand the code by making it hard to read and maybe re-use the code.
 
12. There is no need to call GC.Collect() in your code, the .NET GC is always there to collect your objects in most cases. It is a rare case when we need to interfere with the Garbage Collector work.
 
13. String the String: I have seen many coders doing this just to give you an example
  1. Request.QueryString["myvariable"].ToString(); //there is no need for ToString here  
Request.QueryString returns a string, so one should not call .ToString() here. 
 
14. Comment: Comment and document your code often as you might also not remember that a complex piece or a variable++ had solved some problems in the past. So not commenting on your code will create hard to read code for complex blocks for both you and your peers. Properly commented code is your friend.
 
15. Save the world: Stop thinking of you as a Hero, so when you are stuck with your code just ask for help. It will not only help you solve your problem but also give a second eye to your issue and might come up with a more efficient way to solve the issue.
 
16. Avoid writing duplicate code, just put that logging code in a utility module and keep calling from your code.
 
17. Do not tempt to write long methods. What I mean here is method code which runs in thousands of lines.
 
18. Do not have too many parameters in your methods, be reasonable, and not confuse the caller of the method and have the method readable.
 
19. Test, Test, Test: This will eliminate a lot of your bugs/defects and make your code more robust. This will increase the quality of your code and in turn your product reputation.
 
I hope these points were helpful to you and make your code smell better!


Recommended Free Ebook
Similar Articles