C/C++ Refactoring – Clone detection

C/C++ Refactoring – Clone detection

on Nov 12, 10 • by Alen Zukich • with 1 Comment

As we have posted in the past, refactoring is a very useful technology to help developers become more productive.  I wanted to take a deeper look at how certain refactorings such as “Extract Function/Method” and “Introduce Variable” can be further enhanced with clone detection.   For the focus of this...

Home » Code Refactoring » C/C++ Refactoring – Clone detection

As we have posted in the past, refactoring is a very useful technology to help developers become more productive.  I wanted to take a deeper look at how certain refactorings such as “Extract Function/Method” and “Introduce Variable” can be further enhanced with clone detection.

 

For the focus of this post I will concentrate just on Extract Function/Method.  Say I create some code that I know I will use frequently.  It would make more sense to create a reusable function/method.  Of course I can add a function to my file then pass the proper parameters and function call, or I can click the extract function refactoring which does this automatically for me.

 

void dosomething (int a, int b) { ... int c = a + b; printf("result= %i ",c); ... }

Now if I select the two lines and run extract function, it will ask me for a name of the function, I’ll call it “add” and we end up with this:

void add (int a, int b) { int c = a + b; printf("result= %i ",c); } void dosomething (int a, int b) { add(a, b); }

Same idea for Introduce variable.  Except it works with an expression and replaces it with a variable name of your choice.

So obviously extract function and introduce variable has value,  anything you can do to make the development process more automated can only make you more productive.  But let’s take a look at how we can improve on this and show where you can really take advantage of the power of refactoring.  In most cases you are working with existing code with plenty of duplication.  This is where clone detection comes into place.  Essentially clone detection is the process of finding the same pattern you just highlighted with special attention to code semantics.  This means you now get immediate notification that there are other areas of duplication, even if variable names might be different, and replace with a click of a button.

Taking the simple extract method function, let’s say we have similar code many lines later.

void dosomething(int a, int b) { ... int c = a + b; printf("result= %i ",c); ... int d = a + b; printf("result= %i ",d); ... }

Now if we run extract method, you get notified about each clone and the choice to replace.

void add(int a, int b) { int c = a + b; printf("result= %i ",c); } void dosomething(int a, int b) { ... add(a, b); ... add(a, b); ... }

This is a file level refactoring so clone detection can even apply to multiple functions in the same file!   So hopefully with this you will have a new found love for what refactoring can do you.

Related Posts

One Response to C/C++ Refactoring – Clone detection

  1. One of the great books I read about refactoring was, “Refactoring: Improving the Design of Existing Code”, this book is unbelievable, I recommend everyone to read it.

    In addition to the manual refactoring, Visual Studio introduces new shortcuts and commands to do some major refactoring techniques like the Extract Method. Unfortunately, those techniques work only for .NET applications.

    Thanks for the great post, keep up the good work! :)

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Scroll to top