Skip Demo

Code Refactoring with Klocwork Desktop

Watch the Presentation

Code Refactoring for C/C++ (3:29 minutes)

Automated C/C++ refactoring

Klocwork Refactoring simplifies the time consuming task of code maintenance for C/C++. Automatically clean up your code and make it easier to understand within Visual Studio or Eclipse.

Benefits of Klocwork Desktop:

  • Powerful Refactorings
  • Refactorings Rely on Deep Code Semantics
  • One-Click Usability Model
  • Major Time-Saver

Read the video transcription

Todd

Hi, it's Todd here and again I'm joined by Alen. Today we're going to look at how to use Klocwork refactoring for C and C++ with your IDE, whether it's Eclipse or Visual Studio. Specifically, though, we're going to look at three different refactorings using Eclipse. We're going to look at the rename, the introduce variable, and inline variable refactorings. So Alen, give us a real quick definition of what refactoring is.

Alen

Yeah, really what it is, is modifying your source code without really changing its functionality. The goal ultimately here is to create a cleaner design, something that's more maintainable, and just easier to understand.

Todd

Okay. How about we dive right in and look at the rename refactoring first.

Alen

Okay. That's pretty simple. I mean, it's a matter of, you know, everybody does this day in and day out. It's just renaming a variable. Here I have a variable called OP. I may decide, hey, that's not very useful. Let me be more specific in this case, and again whether you're using Eclipse or Visual Studio, a simple right click brings up the Klocwork menu and then the specific rename refactoring in this case. So it's going to ask us, "What do you want to rename it to?" I'm going to say, "How about Options." I don't know if that's all that much better, but at least it shows us the example of a rename. The purpose here, as you can see, it's changed it in all its specific locations. So it's really important to understanding the semantics of the code and going deep to really do this. It's more than just your search and replace.

Todd

Are there anything else you can rename?

Alen

Yeah, you can do local variables, parameters, or static functions.

Todd

Okay, good. Let's move on to the introduce variable refactoring. So can you walk us through that a little bit?

Alen

Yeah. So the idea of that is, again, just highlighting any kind of expression and saying okay, as opposed to using this whole expression, an easier way, obviously, is to introduce a variable. So let's do that. So again, right click, Introduce Variable. I'm going to call this, how about "conflict." I'm going to hit OK.

Todd

There's our clone detection.

Alen

Absolutely. So this is where Klocwork kicks in, understands the semantics beyond just your typical, there's one location, there are multiple locations where this may exist, so I'm just going to say replace all, and you can see it finds all the places where that expression was and replaces them with "conflict."

Todd

Okay. So I think the last one we have to look at is the inline variable. Now I'm going to go out on a ledge here, Alen, and say is that the opposite of the introduce variable refactoring?

Alen

Yeah, you got it. So it's pretty simple. Highlight anywhere where you see the "conflict" in this case, and I'm just going to go inline this particular variable and boom, that disappears and changes all the other locations accordingly.

Todd

You really make that look easy.

Alen

Yeah. Well, you know what? Developers are doing this day in and day out. Just take the rename refactoring. Everybody is renaming variables, realizing well, maybe there's a better way to do this. So we're trying to eliminate a lot of the tasks that you're doing day in and day out and just make developers more productive by providing this semantic analysis with refactoring. Going to this really level of depth that you need to get valuable tools with C and C++ refactoring.

Todd

That's great. It's so important to be able to automate, as you say, those day-to-day activities of developers, make them more productive going forward. Thanks again for the demo, Alen.

Alen

You're welcome.

Todd

And for more information, visit our website at www.Klocwork.com/documentation.

Previous Image
Refactorings Integrated with Visual Studio
Integrated with Visual Studio
Extract Functions Completed in Seconds
Extract Functions Completed in Seconds
Knowledge of Dependencies Provides Flexibility
Knowledge of Dependencies
Tool Ensures Accurate Refactorings
Tool Ensures Accurate Refactorings
Next Image

Powerful Refactorings

  • Extract methods or functions from large and unwieldy methods to create smaller and more logical functions
  • Inline functions or methods to remove the overhead of an overly segmented source layout
  • Introduce variables to simplify complicated expressions
  • Inline variable replaces all occurrences of a given variable with its initial value
  • Rename entities to make your identifier names more meaningful
  • Analyze and optimize headers: missing transitive includes and unnecessary includes

Refactorings Rely on Deep Code Semantics

Unlike other refactoring tools, Klocwork Refactoring leverages all of the semantic knowledge made available by Klocwork Truepath, our advanced static analysis engine. This enables intelligent clone detection when extracting methods or introducing variables, and a thorough inspection of all invocations of a method/function when attempting to inline. With Klocwork's refactoring and static analysis engine, developers can efficiently clean-up their code's design.

One-Click Usability Model

Refactoring options are available to you right from your editor, allowing you to quickly gauge the state of your code and improve its design without having to access separate views or dialogs. Refactoring is available to C/C++ developers using IDE plug-ins for Visual Studio 2005, 2008, and Eclipse CDT 3.4 or 3.5.

Major Time-Saver

A consistent refactoring discipline ensures that everything is expressed as few times as possible, saving time on future code modifications. Using Klocwork Refactoring, C/C++ developers can automatically abstract their code into re-usable and understandable code.

Klocwork Insight has been proven in some of the largest and most demanding software development environments in the world. Try it on your code today by requesting a free product trial.

Free Trial

In this section

Relevant Resources

CASE STUDY: Stockert

As a manufacturer of medical electronics, meeting the FDA's rigorous safety and quality standards is crucial to Stockert. With the safety of their devices depending significantly on the quality of software [...]

VIDEO: Analyze and Optimize Header Refactorings for C/C++

Short demo on using Klocwork Insight to refactor C/C++ code, specifically looking at analyzing and optimizing headers [...]

VIDEO: Extract and Inline Function Refactoring for C/C++

Short demo on how to use the Klocwork Insight C/C++ refactoring tool, specifically looking at the Extract Function and Inline Function refactorings [...]