There are lots of Coding Best Practices and Tips for C# and in the post I will be covering the top 5 best practices which are
- Adding Comments
- String Vs StringBuilder
- Check for Nulls
- Compiler Warnings
- Using Enums
Starting with the simplest and the most Important which is
We know comments are neither processed nor compiled by .NET framework, and as always some developers working on tight deadlines skip to add comments because it neither give a Warning or Error on Production box. Also sometimes we think that we can add the comments later once the code is done but I tell you that time never comes.
I know of 4 main reasons why we need to add comments
- Code Information: Comments provide info about your code and help you later and other developers to understand it faster and correctly.
- TODO Section: You can add TODO section so that you can advise yourself and other about the Tasks that need to be done at specific points of the code. You can use TODO section while doing Peer Testing of the fellow developer’s code, where you can advise him of the changes that need to be made.
- Intellisense: When we provide comments on the Methods and we access those methods from different places or if some other person is trying to access the method developed by you then he can see the intelisense comments about the method.
- Disabling the Code: Sometime you are working on a piece of code which is not completed and is throwing error , but u want to run the solution then you can comment the code for sometime and then run the other parts of the code. Developers keep using the approach on the day to day basis.
There are two types of comment:
- Single Line Comment: Used to comment a single of Item . Done using double backslash
- Multiple Line Comment: Used to comment lines when they span across multiple line. Done using (/* Comment Here */)
String Vs StringBuilder
String is immutable, means if you try to alter their values, another object gets created. So any changes on current string object will create new memory spaces. Hence using string is not advisable when you are not sure of the no of iterations of concatenation/changes that have to be made to same string .
StringBuilder is mutable, that means once we create StringBuilder object we can perform any operation that appears to change the value without creating new instance for every time. So they provide a lot of code optimization if you have to operate on the same string.
But this does not mean you start using StringBuilder object for all the operation. Mostly prefer using string when you want to have some intermediate results as lots of operations cannot be done with string builder. Also just because you are adding your last name to the first name don’t use stringBuilder because this start with a big payload.
Check for Nulls
We should always check for Null before we start using a member field, object, method and there properties, collections etc.
These errors cannot be identified at compile time but when code runs in production you may find a “NULL Reference Exception” bringing your code down. E.g If your method is being used by someone else and they are passing a string to your method for which you are taking out length then if u don’t check NULL this can generate an error because if he passes NULL then “NULL”.Length will give error.
In C#6.0 We have new concise and beautiful way of checking NULL.
I myself have been doing this sometime , we as developers are more worried about errors and just ignore compiler warning, but I tell you we should avoid this as we may take a bad code to Live environment.
There are case when your compiler keep screaming that you have declared Variable but not used them and we just ignore that warning. We should not do this and should fix all warnings as Priority.
Also ignoring these warning can be costly as the Logic which you intend to write does not conveys properly.
So we can avoid this by forcing Compiler to Treat warning as Errors by going to Project properties > build > and setting the section “Treat warning as Errors” to all.
ENUMS are strongly Typed Constants and they provide a more meaningful name to them. This makes the code more manageable, readable, concise and organized.
So in places where set of constants are related to each other and you want to use those constants to do some logic switch or condition check then it make more sense to use ENUMS.