Structural Code Comparison

What Structural Comparison Is

Structural code comparison is an approach that allows you to compare sources via their signature and not only by their location. Code Compare parses file into its basic structural units, i.e. methods, then tries to determine similar lines and after that finds moved blocks of code.

Structural comparison involves the features of moved blocks detection and Difference Explorer that allows you to view differences in class structure and navigate through them.

How to Enable Structural Comparison

This option is turned on by default, but you can turn it off. For this, in the Comparison menu select the Structural Code Comparison option, or click the corresponding button on the toolbar. After this the comparison map will be changed.

Structural Code Comparison Implementation for Programming Languages

Structural comparison is supported for the following languages:

  • C#
  • C++
  • Visual Basic
  • Java
  • JavaScript
  • XML

Example 1: Moved Code Elements

The first example demonstrates how structural comparison can be used to trace code elements movement.

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

Structural comparison 1

Moved code elements in regular comparison

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

With structural code comparison enabled, the tool compared the source files like this:

Structural comparison 2

Moved code elements in structural comparison

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

Example 2: Renamed Methods

The second example demonstrates another practical feature of structural comparison, which is association of renamed methods that contain similar parameters. The method Paint was renamed to Render while its parameters remained unchanged.

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

Structural comparison 3

Renamed methods in regular text comparison

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

Structural comparison 4

Renamed methods in structural comparison

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

Example 3: Similar Names

The third example describes the use of structural comparison to associate methods with similar names.

Structural comparison 5

Similar names in regular text comparison

Just like in the previous cases, when you compare source code using regular comparison algorithm, the blocks are considered absolutely different.

Structural comparison 6

Similar names in structural comparison

Structural comparison detected similar method names and handled the blocks as modified. This feature can be especially useful for mistype detection.

Note

This functionality is available only in Code Compare Pro.

Using Difference Explorer

The Difference Explorer window shows changes in the class structure between revisions. It displays the source structure tree and marks changes with different colors. This helps you to review changes in the code and navigate between them.

To open the Difference Explorer window:

  1. Enable structural code comparison, for this, in the Comparison menu, select the Structural Code Comparison option, or click the corresponding button in the Code Comparison toolbar.

Structural comparison button

  1. In the View menu, select the Difference Explorer menu item.

Difference explorer

The Hide Identical Members and Sort by Member Type and Alphabetically buttons help you to review changes. The first one hides identical members of a class and the second one allows you to sort members by their type and alphabetically.

Note

This option is available only in Code Compare Pro for C#, C++, Visual Basic, Java, JavaScript and XML languages.

Moved Blocks Detection

Moved block is a block of code that exists, but has different position within the compared documents. Owing to the fact that the application compares code on the basis of signatures, it detects a moved block even if it is located at the beginning of the earlier version, and at the end of the later version.

Moved blocks detection

The Main method at the end of the earlier version and at the beginning of the later version