Mastering Source Code Differences with CppDepend
This document explains how to use the CppDepend build comparison features, in the context of the interactive UI. |
- Getting Started Video (2:30 min)
- Introduction
- Code Diff from within VisualCppDepend Menu
- Code Change Review through Search Panel
- Code Diff from within Visual Studio
- Defining the Baseline for Comparison
Getting Started Video (2:30 min)
Introduction
Code evolution and code maintenance are some of the most prominent characteristics of software engineering.
CppDepend comes with some advanced code evolution and code diff features since mid 2009. One of the coolest results of CppDepend integrated in Visual Studio, is that now, developer benefit from several fine-grained code diff features, one click away from their natural developing environment. Let's expose 3 typical code diff scenarios.
Code Diff from within VisualCppDepend
So for example one can right-click a project in the Code Browser and ask for Select Methods (of the project) where Code was Changed.
This will result in a CQLinq query generated that Select Methods (of the project) where Code was Changed. Naturally, for each method listed, one can jump straight to source code declaration or ask for diff.
Code Change Review through Search Panel
The Search Change Panel is actually just a CQLinq query generator related to Code change. For example, in the screenshot below, we can see that asking for Method + Change + Code was Changed or was Added generates the CQLinq query:
from m in Application.Methods
where m.CodeWasChanged() || m.WasAdded()
select new { m, m.NbLinesOfCode }
The result shown is ideal to do efficient Code Change Review: not only all code changes are nicely organized at a glance, but for each methods refactored, the developer is just one click away to observe diff in source files.
The Search Change Panel's options offers various possibilities to explore diff, including searching for code elements where code and only code (not comment) was changed or where comment and only comment was changed, where visibility was changed, where was added or removed etc...
A bonus option is to search diff in tier code, like asking for which library types is not used anymore for example.
Typically, developers like to write tests for testing automatically refactored and new code. Another cool bonus option is to search for diff coupled with code coverage by tests ratio, like asking for methods where code was changed (i.e refactored methods) and where it is not actually properly covered by tests.
Code Diff from within Visual Studio
As we just saw, there is an CppDepend menu in source code right-click menu, but there is also an CppDepend menu in Visual Studio source code editor. Since CppDepend recognizes the code element currently edited in source, the user can, for instance, right click a method and see if it has been changed. If the method code has indeed been changed, several options appear on the right click CppDepend menu. One of the option is Compare Older and Newer version of source file, which leads to opening a diff textual application to see changes
Here is for example,the VS 2012 extension diff tool opened from clicking Compare Older and newer version of source file on the NUnit v2.4.8 vs. v2.5.3 method IsPlatformSupported().
Look back in the first screenshot above, and you'll see several other interesting options, including Open my declaration in older source code.
Let's precise that not only method changes are taken account but namespace, type and field changes as well. For example, one can right click a namespace source code declaration and ask for types or methods of the namespace that has been refactored or added.
At this point you might wonder how CppDepend/Visual Studio knows about the previous version of the code to compare against to (what is called the Baseline for Comparison). This concept is explained later in this article, let's jump to the second code diff scenario.
Defining the Baseline for Comparison
Earlier in the post we introduced the concept of Baseline for Comparison. This represents the previous snapshot version of the code base against which, the comparison is done. Typically, the Baseline for Comparison represents the latest version of the code in production.
The Baseline for Comparison can be specified through the menu: Diff > Define The Two Snapshots to Diff > Define the project's Baseline for Comparison. The dialog below appears and let chooses through different options to define the baseline. The Baseline for Comparison option is then persisted in the CppDepend project file.
Finally, any diff tools can be plugged through the menu Diff > Source Files Compare Tools.