Assuring the Code Quality of SharePoint Solutions and Apps Matthias Einig, MVP RENCORE AB
Jun 01, 2015
Assuring the Code Quality of
SharePoint Solutions and Apps
Matthias Einig, MVPRENCORE AB
Who is that guy?
www.matthiaseinig.de
Matthias EinigCEO, SharePoint MVP
Stockholm, Sweden
What is Code Quality?
Source: Consortium for IT Software Quality
• Reliability
• Security
• Efficiency
• Maintainability
• Size
Why should we care?• Avoid bugs
• Reduce effort (coding & testing)
• Improve maintainability
• Prepare for changes/upgrade
• Increase team flexibility
Code Quality can’t be easily added later!
How to measure Code Quality?
• Architectural standards
• Coding Practices
• Complexity
• Documentation
• Portability
• Tech./Func. Volume
Reliability
Security
Efficiency
Maintainability
Size
How to analyse Code Quality?
• Static vs. Dynamic
• Source Code vs. Object Code
• Manual vs. Automated
• Ad hoc vs. Continuously
Why is it important for SharePoint?• Solutions / apps are black boxes
• Code quality differs a lot
• Solutions change (ALM)
• Complexity
• Policies / Best Practices
• Dev Teams Change
• Farm stability / security / licensing
So, what is SharePoint Code? .NET code
XML: Manifest, Features, Content Types, Templates
HTML, ASPX, ASCX
CSS, JavaScript
Images, resources, document templates…
Deployment locations!
Is my codecorrect and follows
best practices?
Developer
Is the codewell designed and
maintainable?Architect
Does the codecomply with
company policiesand standards?
Quality Manager
Who needs it and why?
Will the codeharm my farm?
Administrator
What Tools do we have?`ObjectCode FxCop/VSCA
FxCop Metrics
CAT.net
SourceCode StyleCop
SPCode SPDisposeCheck
MSOCAF
Checks against general coding errors
(not SharePoint-specific)
Calculates code metrics
(only .NET code, not SharePoint specific)
Checks coding style guidelines
(only .NET code, not SharePoint specific)
Analyzes code security
(not SharePoint-specific)
Checks memory leaks
(SharePoint-specific)
Combination of FxCop and
SPDisposeCheck for SharePoint Online
The Gap…What’s missing?• Analyze SharePoint specific code quality!
• Analyze SharePoint dependencies!
• Gather SharePoint metrics!
• Document the SharePoint solution / app!
• Assess the migratability to the app model!
Commerical Products
Free version
SPCop Community Edition
Free version
SPCAF Migration Assessment
CODE ANALYSIS
WITH SPCAF
• Run SPCAF manually or on solution build
• Doubleclick results in Error List to open the
line of code
SPCAF in Visual Studio
SPCAF Client Application
Use predefined rulesetse.g. SP2013 Compatibility
Create your own ruleset
Enable/disable rules
Adjust severity of notifications
Information
Warning
Error
Configure Rulesets
Run SPCAF in TeamBuild as Quality Gate
• Custom Build Activity
• Supports TFS 2010/2012/2013, VS Online, Team City etc.
Build in TFS on-premises Build in Visual Studio Online
Continuous Integration
DEVELOP CUSTOM RULES
You want to rule yourself? (1)
using SPCAF.Sdk;using SPCAF.Sdk.Rules;using SPCAF.Sdk.Model;using SPCAF.Sdk.Model.Extensions;
1. Create empty class library
2. Add SPCAF.SDK.dll assembly reference
3. Create class and add usings
4. Add method stub by inheriting from ”Rule”
namespace SP24.Rules{
public class FeatureNameStartsWithSP24 : Rule<FeatureDefinition>{
public override void Visit(FeatureDefinition target, NotificationCollectionnotifications)
{}}}
5. Add Rule Metadata
namespace SP24.Rules{
[RuleMetadata(typeof(Naming),CheckId = "SPC99001",DisplayName = "Feature name should start with SP24",Description = "A feature name should be prefixed with 'SP24'.",DefaultSeverity = Severity.Warning,SharePointVersion = new string[] { "12", "14", "15" },Message = "Feature '{0}' should start with 'SP24'.",Resolution = "Change the folder name of the feature and add the name at the beginning,
e.g.‘SP24.Intranet.Components.ContentTypesFeature'.")]public class FeatureNameStartsWithSP24 : Rule<FeatureDefinition>{...
You want to rule yourself? (2)
7. Build and drop the assembly into the SPCAF installation folder
public class FeatureNameStartsWithSP24 : Rule<FeatureDefinition>{
public override void Visit(FeatureDefinition target, NotificationCollection notifications){
if (!target.FeatureName.StartsWith("SP24")){
string message = string.Format(this.MessageTemplate(), target.FeatureName);Notify(target, message, notifications);
}}
}
6. Implement the rule
You want to rule yourself? (3)
Summary• Code Quality must not be a fluke!
• Helps you to find problems earlier
• Saves your time & money
• Prepares you for changes and upgrades
• QA can be implemented in several steps
Also check out my session:
SharePoint Continuous Integration with VS Online and Azure
D2S3DEV 10/16/2014 11:30 AM
References• SharePoint Code Analysis Framework
spcaf.com
• Free SPCop Code Checkgo.spcaf.com/spcopceI
• Free SPCAF Code Migration Assessmentgo.spcaf.com/spcafma
• SharePoint health check: Extracting SP Customizationsurl.spcaf.com/extract
questions?
WWW.MATTHIASEINIG.DE
@MATTEIN
thank you.
SHAREPOINT AND PROJECT CONFERENCE ADRIATICS
ZAGREB, 10/15/2014 - 10/16/2014