Showing posts with label coding standards in .net. Show all posts
Showing posts with label coding standards in .net. Show all posts

07 January 2012

Some Coding Standards in .net



1.    Use Meaningful, descriptive words to name variables. Do not use abbreviations.







    Good:
    string address
    int salary








     Not Good:

           string name




       string addr

      int sal




2. Do not use single character variable names like i, n, s etc. Use names like index, temp



     variable 'i' is used for for iterations in loops



        Ex:  for ( int i = 0; i < count; i++ ){....}






3.  Do not use underscores (_) for local variable names. But member variables must be prefixed with underscore (_)








4.  Prefix boolean variables, properties and methods with “is” or similar prefixes.




      Ex: private bool _isFinished










5.  File name should match with class name.




       Ex:  for the class HelloWorld, the file name should be helloworld.cs (or, helloworld.vb) 









6.  Keep private member variables, properties and methods in the top of the file and public members in the bottom.  








7. Use String.Empty instead of “”




      Ex: txtMobile= String.Empty;














8.   8. Avoid having very large files. If a single file has more than 1000 lines of code, it is a good candidate for refactoring. Split them logically into two or more classes.






9. Use StringBuilder class instead of String when you have to manipulate string objects in a loop. The String object works in weird way in .NET. Each time you append a string, it is actually discarding the old string object and recreating a new object, which is a relatively expensive operations.