Tuesday, January 17, 2012

How to code efficiently in a .NET language?

Here are some techniques you may apply in your quest to become a better .NET coder:

[1] Leverage the power of industry-standard tools. There exist many proven tools to help you with various programming needs.

Some, like the the Redgate reflector is a commercial one (with a limited-feature free trial version available). It is used to reconstruct the lost source code (in VB.NET/C#) from the .NET assembly (exe/dll).

On the other hand, a NUnit (used for unit testing .NET components) and NANT (build tool to automate build process of .NET sources) are opensource and 100% free. A fairly comprehensive list of all such tools are available here:

List of free and opensource .NET tools.

[2] Follow proper naming conventions for variables and constants. A good naming convention has several advantages such as providing meta-data about the variable or function name, promotes consistency and readability within a team and provides better understanding in case of code-reuse.
For variables and type names, Microsoft recommends a pascal casing convention where the first letter of the name is capitalized and the rest is in proper case (example "ToString", "Red", "BackColor", "ForeColor", etc.). For parameter names, Microsoft recommends camel casing convention where the first letter of the name is small and the rest is proper case (example "toString", "red", "backColor", "foreColor", etc.)

Microsoft also has recommendations for naming Assemblies & DLLs,  Namespaces, Struct Types, etc. For more information on this, see Microsoft .NET Design guidelines for class libraries

[3] Always keep possible code-reuse in mind while coding. While coding, it is always a good practice to consider the effect of exposing your functionality to other users in the form of a Library/API. Instead of scavenging for bugs later, you will save lots of time and concentration by planning ahead for it.

It is also a good practice to avoid possible formatting of data while returning values from your functions or sub-procedures, and leave those implementation details to the caller instead.

[4] Clean up your unused and local variables. This is the number one efficient factor that directly affects the performance of your code. Always keep a habit of releasing the memory used by local variables in the Finally block. Also, release all .NET and COM Global/Module-level variables when they are no longer needed.

In order to clear COM objects, you have to call Marshal.ReleaseCOMObject() function explicitly, whereas .NET objects will be released by just setting them to null (C#) or Nothing (VB.NET).

[5] Avoid using GoTo statements. There is nothing inherently wrong with using Goto statements in VB.NET, but they reduce the code-readability. Its easy to lose track of the issue at hand, while scavenging for the possible effects by following your Goto statements.

[6] Always use FxCop to audit your code. FxCop is a free tool from Microsoft that checks your assembly for compliance with Microsoft Design guidelines.

Apart from highlighting inconsistencies in issues like naming-conventions, an important advantage of using FxCop is that it brings to light several performance improvizations that could be made to your code. Here is the download page for FxCop version 10.0:

FxCop download page.

No comments:

Post a Comment