The importance of Code Quality has grown multi fold times over the past few years. Gone are the days, when meeting deadlines used to be the sole measure of success. In today’s world, just delivering code is not enough – maintaining high code quality and ensuring software reliability has become a necessity.
In this article, we will understand the importance of Static Code analysis, the role it plays in increasing code quality, the new Live Static Code Analysis feature in Visual Studio 2015 and its benefits. I will also show how to fix memory leaks using an analyzer.
What is Static Code Analysis?
Static Code Analysis is an automated way to review the entire source code, scan it across a predefined set of design/performance/usage rules and identify potential violations in the code – that are not compile-time issues.
We can use Static Code Analysis as a safety net to ensure that we are complying with coding standards and not building up our technical debt.
What is Live Static Code Analysis in Visual Studio 2015?
The Live Static Code Analysis in Visual Studio is instantaneous – we do not need to build our application to get the code analysis results. We get the feedback, as we type.
Live Static Code Analysis in Visual Studio is provided through Analyzers. The analyzers run real time and provide us coding assistance, right when we are writing the code. Thus the feedback time is greatly minimized – we need not wait till post build to identify the common coding problems.
We can add the analyzers to our projects in Visual Studio via Nuget. There are a number of Analyzers currently available – Microsoft.AnalyzerPowerPack, Code Cracker, CSharp Essentials and more.
Each of these analyzers have their own rule set, so we can decide which analyzer to add, based on our requirements. Once installed, the light bulbs and quick actions in VS2015 will pick up the associated rule set and provide the developers with coding assistance on the fly.
How to install Analyzers in our Project?
Now I will show you how to install an analyzer and how it can help us to identify code smells. I will use ‘CodeCracker’ for the purpose of this demo.
We can use Nuget Package Manager to search for ‘CodeCracker’ analyzer Online and install it in our application.
Once installed, we can go to our Solution -> References -> Analyzers node and click on ‘Open Active Rule Set’ to view the entire rule set provided by this analyzer.
Once we build our solution, the analyzer will run the ruleset across the entire codebase and display the violations as Errors, Warnings and Messages.
The best thing about the analyzers is that we have full control on each of these rules, we can categorize the rules (as Errors, Warning, Info and Hidden), we can suppress the rules if we don’t find it beneficial and we can also define our own rule set.
Let us now look at a demonstration of CodeCracker and how it can help to identify Memory Leaks.
What is a Memory Leak?
Memory Leak is an unintentional memory consumption by our application. One of the most common cause of a Memory Leak is the failure to release unmanaged resources.
Garbage Collection is a non-deterministic process, hence we can use the Dispose() method (via IDisposable) to perform a deterministic cleanup. While writing code, it is always a good practice to clean up and release any resources (unmanaged data, database connection, memory streams, file handles) after the object goes out of scope.
Fixing Memory Leaks using Analyzers in Visual Studio 2015
As we write our code, CodeCracker will on the fly identify if a disposable variable has not been disposed, show a red squiggly to the developer and present a code fix to wrap the object creation in a using block or invoke Dispose method explicitly.
In a large application, it is very difficult to find all the instances where the Dispose method is not invoked to free up unmanaged objects. We will use CodeCracker analyzer to find out all these instances and also apply a code fix to dispose the objects automatically.
In the Error List Window, we can filter the Code rule with RuleID CC0022. This will display all instances in the code base, where unmanaged objects have not been disposed.
We can double click on any of the instance above, and the light bulb and quick action in VS2015 will provide a code fix to dispose the objects.
In the above piece of code, the MemoryStream was not disposed. The analyzer will identify such instances, and provide suggestions to fix it –
To ensure that everyone in our Team follows the same coding standards, we can also create our own customized rules and have a fix defined. The analyzers not only provide us with Coding Assistance on the fly but also help us to identify potential issues in our code, which could have been difficult to catch during manual code reviews.
To summarize, there are 3 reasons why I love Live Static Code analysis —
- I get realtime coding assistance.
- I need not wait till post build to identify code smells.
- I can improve my code quality, by complying with a rule set of best coding practices.
I would suggest you to explore this great feature of Visual Studio 2015. Try out the various analyzers available currently and find out the benefits you get from them.
My previous related articles –