Semantic Source Code Comparison


The concept of Code Orientation is what makes Code Compare stand out from an oceanful of counterpart semantic comparison and merge tools. It processes source files as code and rests on several foundations, Structure Comparison being one of them. This allows Code Compare to recognize the structure of the source code and perform text comparison accordingly. When comparing and merging code, this method can process source files written in popular programming languages, namely C#, C++, JavaScript, Java, Visual Basic and XML.

Please keep in mind that Structure Comparison for the latest versions of C# and VB languages is supported by Code Compare only in Visual Studio 2017. As for the earlier versions of Visual Studio (2010-2015), Structural Comparison will work as well - but only for the language versions supported by the specific version of Visual Studio. In the Code Compare Standalone version, C# is supported up to and including version 7.0. The Structure Comparison mode can be toggled using the eponymous button on the toolbar. With Structure Comparison turned off, the application returns to the usual comparison mode.

The following examples can help you understand the advantages of Structure Comparison better.

Moved Code Elements

The first example demonstrates how Structure Comparison can be used to trace the movement of code elements.

The following screenshot shows comparison of two files performed with the usual text comparison algorithm:

Comparison of two files

The code remained essentially the same but two strings were swapped. Default text comparison detected differences in corresponding strings and highlighted them using the preset colors.

With Structure Comparison enabled, the tool compared the source files like this:

Comparison of two files - Structure Comparison

The Structure Comparison algorithm recognized the C# syntax and highlighted no difference, since the code would run the same way as before.

Renamed Methods

When you need to merge source code, it's often vitally important to see if any methods have been renamed with no actual changes to their functionality. The Structure Comparison mode can provide such information. This is possible due to how it associates methods that are named differently but contain similar parameters. In the example, the Paint method was renamed to Render while its parameters remained unchanged.

The first screenshot depicts comparison using the usual text comparison algorithm.

Text comparison algorithm - Renamed Methods

With no code recognition algorithm enabled, the blocks were treated as totally different.

Structure comparison algorithm - Renamed Methods

The Structure Comparison algorithm detected identical parameters and treated the Render method as a renamed Paint method, highlighting the difference as a single modified block.

Similar Names

The third example describes how the Structure Comparison mode can associate methods with similar names while diffing source code.

Text Comparison - Similar Names

Just like in previous cases, when you compare source code using the standard comparison algorithm, these blocks will end up highlighted as being completely different.

Structure comparison - Similar Names

Structure Comparison detected similar method names and handled the correspondent blocks as modified. This feature can be especially useful in detecting typos.

Using the Difference Explorer

The Difference Explorer window shows changes in the class structure between code revisions. It displays the source structure tree and highlights changes in different colors. This helps you with reviewing code changes and navigating between them.

Structure comparison - Difference Explorer

Moved Blocks Detection

This feature allows you to see whether any blocks of code are exactly similar but located in different places of the document in its different versions. This feature is possible due to the fact that the application compares code on the basis of signatures, so it can detects a moved block even if it is located at the beginning of the document in the earlier version, and at the end of it in the newer version.

Structure comparison - Moved Blocks Detection

See Also

Implementation of XML structure comparison has its own peculiarities and is covered in a separate article of our blog.

You can also learn about another useful comparison mode - Lexical Comparison - which can detect programming language lexemes and compare them instead of comparing each individual symbol separately.

For details on the major feature set of our file diff and merge tool, refer to the overview page of Code Compare.