Overview
As an experienced developer you have undoubtedly encountered issues with merging your code, especially when attempting a refactor, because traditional tools are text-based and they simply do not understand the code like SemanticMerge does. Built on the premise of code semantics, SemanticMerge not only sees your code at the text level but it can actually determine what is going on within it by focusing on the structure rather than the actual location of your code. By understanding the code at this level, SemanticMerge is able to deliver a visual representation of your method at the pre-merge and post-merge states that makes this tool very powerful.
Programming Language Support
The SemanticMerge tool has been designed from the ground up to support many programming languages now and in the future. In the current release it supports C#, C, Java and VB.NET and is planned to be released for C++, Objective-C and JavaScript in the future. Additional programming language support will be determined by your feedback so please take a moment to cast your vote by clicking on this link here.
How to Change Methods using XMerge
The best way to get a real understanding of the power of the SemanticMerge tool is with an example. Let's start with a basic refactoring of a Windows Phone C# application that has the following ViewModel class structure:
- public class MainViewModel : INotifyPropertyChanged
- {
- private string message;
-
- public MainViewModel()
- {
- Message = "hello world";
- }
-
- public string Message
- {
- get { return this.message; }
- set
- {
- this.message = value;
- RaisePC("Message");
- }
- }
-
- public event PropertyChangedEventHandler PropertyChanged;
-
- private void RaisePC(string propertyName)
- {
- if (PropertyChanged != null)
- PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
- }
- }
Assuming that both developer A and developer B are working on this code at the same time and they each make the following changes (where
Developer A refactors the code to change the method RaisePC by moving it to the start of the class):
- private void RaisePC(string propertyName)
- {
- var tmpHandler = PropertyChanged;
- if (tmpHandler != null)
- tmpHandler(this, new PropertyChangedEventArgs(propertyName));
- }
Developer B refactors the code to change the method with the following result:
- private void RaisePropertyChanged([CallerMemberName]string propertyName = "")
- {
- var eventHandler = PropertyChanged;
- if (null != eventHandler)
- {
- eventHandler(this, new PropertyChangedEventArgs(propertyName));
- }
- }
As you can see in this example, both developers made changes to the method but developer A actually moved the method to a different location inside the class that will definitely create a conflict that must be resolved. By using the SemanticMerge tool this conflict can be easily managed and resolved, let's have a look at how you would accomplish this:
As you can see, SemanticMerge detected the conflict and proposed a solution to resolve it by providing information about the merge needed in the RaisePC method. By clicking the green merge button under the changes, SemanticMerge will launch the
XMerge tool that is included with SemanticMerge. Here is the new
XMerge view that will open up:
Referring to the new XMerge view, there are four code windows that are displayed as follows:
- The top left window displays the source code (theirs)
- The top right window displays the destination code (yours)
- The top middle window displays the base code
- The bottom window displays the result of the merge
For each conflicting change you want to resolve, you would select the source, destination or base code as your starting point. This flexibility enables you to select the solution that is best suited to resolving all of the file conflicts.
Referring to the example code, the first conflict concerns the method name and parameters that were changed in the destination file. Based on this information, we would select the Green destination code changes and then deselect the source and base code changes by clicking on the Blue and Yellow headers:
As you can see, the method body has been changed in both the source and destination files. Based on this information you can now proceed to the second review by selecting the source code change since it is a better version of the change. Just as we did with the first conflict, you would deselect the base and destination code changes by clicking on the Yellow and Green headers and keep only the source header selected:
At this point you are nearly done with making changes to this method. The remaining conflict concerns an extra curly bracket that was left behind at the end of the code. This conflict is easily resolved by simply deselecting the destination header so that it is not included in the final result:
With all of the conflicts resolved, you can now click on save & exit to return to the main SemanticMerge screen.
Using Explain Move
In the example, the second conflict that was detected by SemanticMerge involved moving and renaming the method in the source and destination code, this is called a divergent move. If you are interested in digging deeper to understand exactly what was done, you can click on the explain move button that will show you a summary of the change(s):
After reviewing the change summary you can now return to the SemanticMerge main screen and choose to either keep the source changes or the destination changes:
In this example, we will choose to keep the destination changes because this will preserve the move that developer B made and it will also keep the previous merge of the method. SemanticMerge not only identifies when the text is moved but actually understands the meaning of the source code and identifies that the same code is in place. This is the real power of SemanticMerge.
Using Visual Merge
If you want to have a closer look and determine exactly what the tool did to your code then you can use the Visual Merge button on the left side of the screen to see a graphical representation of the operations SemanticMerge did to resolve all the conflicts:
Referring to the Visual Merge screen, you will see that the top part of the screen displays a graphic with the source, destination and base files with arrows connecting the changes made to the code in each one of the files. By selecting any two of the changes, we can click on the diff button in the top left hand corner of the screen to display the resulting change collision code in the lower part of the screen.
Visual Merge makes it very easy to understand what changes were made as well as where and how they related to the base file.
How to Skip Format Changes Using the –merge-decl Parameter
One of the benefits of SemanticMerges understanding of the underlying code structure of your files is that SemanticMerge can detect and skip format changes that don't introduce new code and only reformats the existing ones.
Using the following method as an example, let's take a closer at how the –merge-dcl parameter will enable SemanticMerge to resolve a conflict automatically:
- public async Task<Movie> GetMovieInformation(string movieName, int year)
- {
- Movie resultMovie = new Movie();
- HttpClient client = new HttpClient();
- string url = string.Format("http://www.omdbapi.com/?t={0}&y={1}&plot=full&r=json", movieName, year);
-
- var result = await client.GetAsync(url);
- if (result != null)
- {
- string json = await result.Content.ReadAsStringAsync();
- resultMovie = JsonConvert.DeserializeObject<Movie>(json);
- }
-
- return resultMovie;
- }
This is a fairly simple code snippet that queries a REST service for movie information. Developer A introduces some changes (in Red) when working with it:
- public async Task<Movie> GetMovieInformation(string movieName,
- int year)
- {
- Movie resultMovie = new Movie();
- HttpClient client = new HttpClient();
- string url = string.Format("http://www.omdbapi.com/?t={0}&y={1}&plot=full&r=json", movieName, year);
-
- var result = await client.GetAsync(url);
- if (result != null)
- {
- string json = await result.Content.ReadAsStringAsync();
- if (!string.IsNullOrEmpty(json))
- {
- resultMovie = JsonConvert.DeserializeObject<Movie>(json);
- }
- }
-
- return resultMovie;
- }
Developer B also introduces some changes (in Green) to the GetMovieInformation method:
- public async Task<Movie> GetMovieInformation(string movieName, int year, string plot)
- {
- Movie resultMovie = new Movie();
- HttpClient client = new HttpClient();
- string url = string.Format("http://www.omdbapi.com/?t={0}&y={1}&plot={2}&r=json", movieName, year, plot);
-
- var result = await client.GetAsync(url);
- if (result != null)
- {
- string json = await result.Content.ReadAsStringAsync();
- resultMovie = JsonConvert.DeserializeObject<Movie>(json);
- }
-
- return resultMovie;
- }
In this example, both developers made non-conflicting changes. One added a parameter to the string.Format and the other added a validation to the returned JSON. Simple changes but the result is that there is a conflict change in the method declaration. So when developer A changed the format of the declaration, developer B added a new parameter. For an experienced developer this is an easy conflict to resolve but for a Traditional merge tool, resolving this conflict is a real challenge. Unlike SemanticMerge, which can use –merge-dcl to automatically resolve the conflict, Traditional merge tools cannot decide what to do automatically:
So when the declaration changes made by
developer A were merely aesthetic changes and d
eveloper B introduced a new parameter, SemanticMerge automatically discarded
developer A changes and re-executed the text merge.
Conclusion
In this article we provided a sneak peek at the power behind SemanticMerge that is designed to help you focus on development and avoid the time consuming tasks associated with merging your code. If you are looking for a way to improve upon the traditional merging and diff tools then SemanticMerge may be just the tool to make life easier for you and your team.
If you like what you see and you want to try SemanticMerge please have a look at the
website where you can download a free trial and give it a test drive with your applications.