June 13 2010
SharePoint 2007 and Code Analysis
In this article I will cover what is code analysis and why we need it especially in context of SharePoint 2007.
Introduction
Lately I am involved in a big SharePoint 2007 project. In this project I have to develop new components and extend existing ones. It’s a big SharePoint 2007 publishing web site. About 90% of my time, I play with SharePoint object model, particularly classes which implement IDisposable patterns. SPSite and SPWeb are two such classes. Well just for your information not everything in SharePoint world is managed, the main backbone has some unmanaged components. SharePoint sites exist in database. Everything in SharePoint is template, which exists in database and file system. This eventually is managed code wrapping some of unmanaged code. So this means SharePoint needs to talk to Database and file system for its operations (Template and contents)
Database connections and file handles are unmanaged resources. SharePoint engine uses these two kinds of resources heavily. From basics of .NET we know that we need to dispose unmanaged pieces when we are finished with them.
I come from hard core ASP.NET, .Net programming and C/C++ programming world. I know what is cost of resources, like memory and also what can happen when they are not properly handled. SharePoint also has some requirement. Incorrectly used code constructs, improper use of objects can lead to very bad results. First bad result is poor performance.
Why Code Analysis?
Code analysis is very important in .NET programming to ensure best practices in many areas. Like naming conventions, security rules, proper code constructs etc. If you expect better performance from your code, be sure to do code analysis. I do many times code analysis manually (called as “Code Auditing”), but that is possible only when all the code is written by me. And that is not case all the time. And I can do mistake too
.So I need some kind of tool.
FxCop is a code analysis tool, which is popular from many years. You can check more about FxCop here
FxCop helps you find problems in your code for as per best design guidelines. Now FxCop is also part of Visual Studio (2008 and 2010)
There are few more tools like SPDisposeCheck, which I will cover in my next articles. It is a different kind of tool and for same purpose of code analysis in different way.
Well, the biggest advantages (as per me) of code analysis are having same coding and design practice followed in SharePoint project.
Code Analysis with SharePoint 2007
VS 2008 is used for development for SharePoint 2007. Once tools like VseWSS and WSPBuilder are installed, VS 2008 is ready for SharePoint 2007.
Code analysis is built-in VS 2008. FxCop is tool for code analysis in VS 2008. FxCop is also available as standalone application. It is fully extensible. We can build our own rule sets. Rule sets are basically rules built as per best practices suggested by Microsoft. FxCop has rule sets for these areas –
- Library design
- Globalization
- Naming Conventions
- Performance
- Interoperability and portability
- Security
- Usage
These rules are very important for any .NET project. It ensures we are writing everything as per best practices and guidelines. And the rule sets which come by default in VS 2008 cover every possible area. One better thing with FxCop, it is extensible and rule sets can be created by almost anybody.
But unfortunately there is no rule set for SharePoint, in default installation of VS 2008. I Googled and found this useful project on CodePlex http://sovfxcoprules.codeplex.com/. Name of project is “SharePoint of View”.
Guys over here built a rule set for SharePoint. I am very grateful to them. Now I can not only check my code, but also check code written by others and we can make sure what we have written with good intentions is full proof. This has an advantage that now all code what we write looks like to come from same “Software Factory” and ensures same practice in overall project.
Now let us start using it. Download it from here and extract it. Downloaded package contains a dll named “SharePoint.FxCop.BestPracticesRules.dll”.
Copy this dll to rules folder of FxCop tool in Visual Studio. This folder is typically at location – “%ProgramFiles%\Microsoft Visual Studio 9.0\Team Tools\Static Analysis Tools\FxCop\Rules”.
Now also download the test project named “SoVFxCopRules Test Project.zip” from the project web site. This is a very good test application to learn code analysis in context of SharePoint. These are really in interest of serious SharePoint developers. Unzip and open the project.
Check references section under in project. You can see almost all SharePoint pieces there. This project covers possible code problem in almost all areas of SharePoint, For example Publishing, Portal, and Core SharePoint etc.
Now open the property page of project and go to code analysis tab.
You can see that there is entry for “SharePoint Best Practices Rules”. Expand it and you can see all rules as per best practices.
Go ahead and build the project. You will get error and warning as per best practices in the project.
You can check code in “Test.cs” file for all these rules.So this was an overview of sample project downloaded from sample website. Now if you need to test your code, just go to Code Analysis tab of your SharePoint project and check the “SharePoint Best Practices Rules”.
Now you have enabled SharePoint code rules in code analysis. To run code analysis, right click your project and select “Run Code Analysis”. Optionally you can check “Enable Code Analysis on Build (defines CODE_ANALYSIS constant)”, so that every time you build, you will be notified for possible problems.
Also you can specify that treat this warning as error. These help strict rules in project.
Conclusion
Code analysis is very important for all software projects to ensure quality and performance. SharePoint uses unmanaged resources, which makes code analysis a must. A poorly written code to target SharePoint will lead to disastrous results. With help of FxCop and “SharePoint of View”, we can perform code analysis for SharePoint. SharePoint of View covers all the best practices for SharePoint. I suggest you to give it a try to be a better SharePoint programmer and ensure that code produced is of top quality and gives best performance.









