Best Coding Practice Rules Using Style Cop, MSOCAF, SharePoint Dispose Checked

Coding a readable, scalable, maintainable code has always been a big challenge in the software industry. There is a famous saying – code for people. The point is that while the machine may understand and execute a complexly written code, a human who needs to modify, upgrade, or reuse that code at a later point in time will generally need a clean code.

A lot of discussions, theories and tools have evolved around this topic over the years of defining software standards.

In the general Microsoft ecospace, what’s emerged are some important and non-negotiable tools to maintain software code – such as style cop, fxCop. In SharePoint, dispose checker is also an important addition.

We will look at some of the important rules these tools define, and why we should try to adhere to these standards while coding.

The Top Tools

FxCop

FxCop is a static code analysis tool. It is provided by Microsoft and it checks .NET managed code assemblies for conformance with Microsoft guidelines. FxCop analyses the complied code not the source code. There are many (200 plus) coding standards which are validated by FxCop.

StyleCop

StyleCop checks C# code and its conformance to Microsoft’s .NET framework design guidelines. It analyses the source code. There are many rules which can broadly be classified into categories like readability, maintainability, documentation, naming guidelines etc. As StyleCop analyses the source code, it is possible to enforce different set of rules from FxCop.

Other Tools

The above mentioned tools are mainly used to analyze custom c# code or assemblies. Though they are extremely important in a SharePoint developer’s work life, there are specific tools pertaining to SharePoint code analysis. MSOCAF (Microsoft SharePoint online code analysis framework) is one.

MSOCAF

Code analysis within MSOCAF focuses on areas like memory management, security vulnerabilities, exception management, object model usage, quality checks for unsupported features and reporting features. This framework heavily relies on FxCop, cat.net and SPDispose check.

MSOCAF can be installed from the MSOCAF download site on microsoftonline.com (https://caf.sharepoint.microsoftonline.com/O15Upgrade.aspx ). The tool provides a very good user interface to check reports, analyze the results, generating deployments and even rollback options.

A very detailed outlook of the tool along with the usage procedures is provided by Microsoft here http://www.microsofttechnology.net/2012/04/microsoft-sharepoint-online-code.html . It is recommended to have a look on the various features of MSOCAF.

SPDisposeCheck

The SPDisposeCheck tool defines a set of static analysis rulesets for SharePoint developers and architects to ensure SharePoint APIs are properly disposed. It checks custom SharePoint solutions that use the SharePoint Object Model helping measure against known Microsoft dispose best practices. This tool may not show all memory leaks in your code. There are a lot of plugins to add the tool as a part of Visual Studio, thus reducing the pain for developers. The best part of the dispose tool is that it also checks for the objects which should not be disposed.

Stay Current

There are lot of best practices that need to be adhered to, and lot of them will get modified with time. From a developers or architect’s perspective it is always recommended to stay updated with the updates and guidelines in best practices. A very good read specially for people from SharePoint field is this MSDN article : http://msdn.microsoft.com/en-us/sharepoint/ff660756.aspx .

December 18, 2015