Code Optimization Tools in Visual Studio 2013

Posted by: Subodh Sohoni , on 11/26/2013, in Category Visual Studio
Views: 89010
Abstract: Visual Studio 2013 comes with some code optimization tools that enables developers to write consistently high quality code with high productivity

Development Productivity is always a matter of concern for the management of organizations that develop software. The biggest portion of spending in development process, is on code development, simply because the number of people involved in that activity are relatively very high compared to other roles. It becomes imperative for us to focus on improving productivity of code development. At the same time, it is also of utmost importance to the team, that the code that is developed, should be of highest quality possible. There is a misconception in the minds of many people that productivity will suffer if we need to maintain high quality of code. That is not true. If code quality is maintained right from the beginning, then the rework and bugs reduce. With the reduction of the bugs, time wasted on fixing those bugs and testing them again, also reduces. Finally the productivity improves.

This article is published from the DotNetCurry .NET Magazine – A Free High Quality Digital Magazine for .NET professionals published once every two months. Subscribe to this eMagazine for Free and get access to hundreds of free tutorials from experts

IDE’s like Visual Studio plays a big role in maintaining high quality of code and improving the productivity of coding at the same time. Visual Studio traditionally provides tools that are built-in to create and edit code, do compilation and debug the code that may have some bugs in it. Since Visual Studio 2005, Microsoft has provided some additional tools built in for checking and maintaining quality of code as per policies and standards of the organization developing the code.

In Visual Studio 2013, Microsoft has included tools that provide visibility within the editor when a team is working on a project. It provides the status of the code, tests run on that code, other components that give reference to code under inspection, the relations between methods and also provides the tools to check whether the code follows certain rules or not. This visibility makes developers aware of what they have not done to improve the quality of code without running time consuming manual testing. Most of the features explained in this article works with Visual Studio Ultimate.

Code Lens

One of the most interesting features that is added in Visual Studio 2013 is called CodeLens. It provides the related status above every declaration of class and method.

code-lens

Figure 1: CodeLens on a class and a method

CodeLens contains a list of references to this code from other components. In the example shown in Figure 1, there are 12 references to the various methods in this class and one reference to the method GetBasicEmpoyeeData.

By selecting references, you can see the list of all references. When you click on a specific reference, it shows the details of the reference like the file that has a method that calls the one under the lens and some of the lines of the code of that method where the method under lens is called. If you double click on that reference line, it takes you to the calling code.

calling-code

Figure 2: References details

Next detail provided by CodeLens is the history of the code. Questions answered in this history are:

· Who made the changes in the code? – Provides the persons responsible for the created and edited code. If you have Lync enabled, then you can even send an email or chat with that person to know more about the code that he / she created.

· Under which changeset was that code checked in? When were the changes made?

· Which work items like tasks, bugs or requirements were associated with those changesets? – This provides the trace to know –

  • Against which requirement was this code created?
  • Which bug was fixed by this code?
  • How much efforts were logged to create / edit this code?

Knowing answers to these questions is very important to the management and sometimes to the team members (depending upon the methodology being used).

code-history

Figure 3: Code History – Who made the changes in the code under the lens?

code-history-changeset

Figure 4: Code History – Under which changesets did the code under the lens get edited?

code-history-work-items

Figure 5: Code History – Which work items were associated with this code?

CodeLens also provides information about unit testing of the code under it. To view those details, let us now create a Unit Test. Unit testing itself is one of the most essential ways of maintaining quality of code. It is also called ‘White Box Testing’. We write code that calls the method under test with known output, for known input. If the expected value and the actual return from the method are equal, then the test passes. Since it is a code that is used for testing, the same test can be repeated many number of times. It can also be data bound so that multiple inputs and outputs can be tested. Microsoft used to provide a Unit Test Generator up to Visual Studio 2010 when only MSTest framework was supported. In Visual Studio 2012, this generator was removed from the product as it could not support all the frameworks like NUnit, XUnit etc. that were allowed in that version of Visual Studio. There is a Unit Test Generator that is available from Visual Studio Gallery – it is available from the URL http://visualstudiogallery.msdn.microsoft.com/45208924-e7b0-45df-8cff-165b505a38d7 . Once you install this tool, a context menu (right click menu) for the method appears that allows you to generate a unit test for that method. It creates a test project, gives necessary references and also creates a class with appropriate attributes. We just need to provide the code for the test method, as follows:

unit-test-code

Figure 6: Unit Test Code

Let us now see what difference this has made to CodeLens. You can see in Figure 7 that it has added information about that test too, in CodeLens.

visual-studio-codelens

Figure 7: CodeLens – Unit Test Status

Editorial Note: I am sure you are beginning to realize how powerful and useful the CodeLens feature is.

While we are discussing unit testing, we should also cover the topic of Code Coverage. Code Coverage is the number of lines of code from the target , tested by unit tests. Let us enable the code coverage for the assembly under test from the Test menu and run the test from Test Explorer. When the test passes, it reflects in the CodeLens, its code coverage results can be viewed and the code coverage is also shown with colors in the code editor; blue for tested code and pink for lines of code that were not tested.

unit-test-effect

Figure 8: Effect of Unit Tests – CodeLens is updated, code coverage results and coloring

If we expand the CodeLens for the method that is tested, it shows the test details with the test run details.

visual-studio-unit-test

Figure 9: CodeLens after unit test run for the method that is tested.

Similar CodeLens update is also available for test method too.

Code Map

From CodeLens, we now will move to another tool. It is called Code Map. In an earlier version of Visual Studio, it was available as Directed Graph and Dependency Graph under tools for architect. Some similar features with more integration with code and debugger, has been introduced for developers as Code Map. From References of CodeLens, we can move to Code Map to show relation between the referenced and referring methods.

code-map

Figure 10: Code Map opened from CodeLens of a method. Shows calling and called methods with their containing classes

We can add nodes and comment nodes to the code map. We can also view the entire assembly that contains the called method and all the calling methods.

code-map-assembly

Figure 11: Code Map of Assembly and referencing methods.

We can analyze assemblies based upon various analyzers like

  • Circular References Analyzer
  • Find Hubs Analyzer
  • Unreferenced Nodes Analyzer

code-map-analyzers

Figure 12: Code Map with analyzers

These analyzers make it possible to avoid certain errors that may have cropped in the code.

Another very important analysis that is required to be done is finding the duplicate code and then removing it from wherever possible. Visual Studio 2013 provides that as one of the tools from ‘Analyze’ menu as ‘Analyze Solution for Code Clones’.

code-clone-analysis

Figure 13: Code Clone Analysis

visual-studio-code-clone

Figure 14: Code Clone Analysis Results and Context menu to compare two code snippets that are same

Static Code Analysis

From the same menu of ‘Analyze’ we can also open a more extensive tool called Static Code Analysis. This was present in the earlier versions of Visual Studio too, but now has been extended with many more types of rules that it evaluates. Categorization and filtering of the results is also provided in Visual Studio 2013.

static-code-analysis

Figure 15: Static Code Analysis – Configuration and Results

Static Code Analysis can be evaluated from:

1. Context menu for any project or entire solution

2. As part of build within Visual Studio

3. As a Check-in policy for enforcement of quality check before allowing check-in

4. As part of build on the Team Foundation Server 2013

Visual Studio provides many rulesets that we can select to be evaluated in our code. We can also create a custom ruleset that represents the policies and standards that the organization has finalized. Consistency of code quality will be ensured because of evaluation of those rules, for all developers, at multiple places.

Code Metrics

The last productivity tool that I am going to describe is Code Metrics. It provides a maintainability index to the developers. Importance of this tool will be understood better by the teams that maintain the code. If the code we develop is well maintainable, then the efforts that the maintenance team has to take, will be reduced. Code Metrics tool evaluates a maintainability index of the code based upon four parameters:

1. Length of code – Higher the length of code, lesser is it’s maintainability

2. Depth of inheritance – Higher the depth of inheritance (from System.Object class) it reduces the maintainability

3. Cyclomatic Complexity – Higher the number of exit points in the code, lesser is the maintainability of code

4. Class Coupling – Tighter the coupling between two classes, more are the cascading code changes needed for one change in one class. Lesser the maintainability of code.

Considering these parameters and applying an algorithm that was originally developed by Carnegie Melon University, an Index of maintainability is worked out. Its value is between 1 to 100; higher the better, from the point of view of maintenance of code. When we evaluate it using the tool Code Metrics, it provides us the values for Solution, for Project, for NameSpaces, for Classes and for Methods. It provides the values of Maintainability index as well as of all those parameters. If some values are less, we can take corrective actions to make the code more maintainable.

code-metric-results

Figure 16: Results of Code Metrics

These are the various tools available in various versions of Visual Studio 2013. Following table shows the availability of tools in the minimum version:

Coding Tool / Feature

Min. Version of Visual Studio 2013 in which the feature is available

CodeLens

Visual Studio 2013 Ultimate with MSDN

Code Map - Debugger Integration

Visual Studio 2013 Ultimate with MSDN

Unit Testing

Visual Studio 2013 Professional

Code Coverage

Visual Studio 2013 Premium with MSDN

Static Code Analysis

Visual Studio 2013 Professional

Code Metrics

Visual Studio 2013 Premium with MSDN

Conclusion


In this article, we took an overview of some tools that are part of Visual Studio 2013. We have seen the following new tools:

  • CodeLens for heads up display about code and data related to it
  • CodeMap to view code and relations between parts of the code graphically
  • Unit Testing for checking the functional correctness of code
  • Code Coverage that provides the data about how many lines of code are tested
  • Code Analysis that ensures the consistency of code against organizational policies and industry best practices
  • Code Metrics computes the maintainability index of the code that you write

With the help of these tools Visual Studio 2013 enables developers to write consistently high quality code with high productivity

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.

We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).

Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.

Click here to Explore the Table of Contents or Download Sample Chapters!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Author

Subodh is a Trainer and consultant on Azure DevOps and Scrum. He has an experience of over 33 years in team management, training, consulting, sales, production, software development and deployment. He is an engineer from Pune University and has done his post-graduation from IIT, Madras. He is a Microsoft Most Valuable Professional (MVP) - Developer Technologies (Azure DevOps), Microsoft Certified Trainer (MCT), Microsoft Certified Azure DevOps Engineer Expert, Professional Scrum Developer and Professional Scrum Master (II). He has conducted more than 300 corporate trainings on Microsoft technologies in India, USA, Malaysia, Australia, New Zealand, Singapore, UAE, Philippines and Sri Lanka. He has also completed over 50 consulting assignments - some of which included entire Azure DevOps implementation for the organizations.

He has authored more than 85 tutorials on Azure DevOps, Scrum, TFS and VS ALM which are published on www.dotnetcurry.com.Subodh is a regular speaker at Microsoft events including Partner Leadership Conclave.You can connect with him on LinkedIn .


Page copy protected against web site content infringement 	by Copyscape




Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by Mike Ward on Tuesday, March 25, 2014 8:33 PM
Please indicate which features ship in which versions of Visual Studio. It's misleading to indicate these features are available in VS 2013.
Comment posted by Craig Shield on Thursday, June 5, 2014 7:01 PM
I Agree with @Mike Ward.  These features are not available in VS 2013 - they are available in VS 2013 Ultimate.  Please be clear.  It is about time Microsoft and MVP started making this clear as (apart from MVP) who is actually paying that extortion to buy Ultimate?
Comment posted by Suprotim Agarwal on Saturday, June 7, 2014 1:48 AM
Mike and Craig: Check the "Min. Version of Visual Studio 2013 in which the feature is available" of this article. It is a table that lets us know which feature is available in what version. Point noted and we have put this at the beginning of this article.