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.
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 comparison is supported for the following languages:
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:
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:
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.
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.
Renamed methods in regular text comparison
With no code recognition algorithm enabled, the blocks were treated as totally different ones.
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.
The third example describes the use of structural comparison to associate methods with similar names.
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.
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.
This functionality is available only in Code Compare Pro.
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:
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.
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.
The Main method at the end of the earlier version and at the beginning of the later version