Skip Demo

Automate your C/C++ Refactoring

Klocwork refactoring simplifies the task of maintaining your C/C++ code within Visual Studio or Eclipse. Built-in C/C++ 11 refactoring provides a consistent, refactoring discipline that helps you improve your code structure. It enables you to automatically abstract code into re-usable and understandable segments, ensures things are expressed as few times as possible, saves time on future code modifications and more.

Watch the Presentation

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

Nimble, Powerful Refactoring

Automatically clean-up your code within your IDE and make it easier to understand. Built-in refactoring makes it easy, for example, to rename entities, make identifier names more meaningful, introduce variables to simplify complicated expressions, or add inline variables that replace all occurrences of a given value with its initial value.

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 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 introducing 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.

A closer look at your desktop refactoring experience:

Unlike other refactoring tools, Klocwork Refactoring leverages all the semantic knowledge about your code from our desktop analysis functions. 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. A one-click usability model enables you to quickly gauge the state of your code and improve its design without having to access separate views or dialogs. Whether you need to revisit some of your own software or code created by a colleague, we enable you to create reusable, understandable code with ease, and ensure modifications don't cause issues elsewhere in the code base. Some more refactoring highlights include:

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

Klocwork's developer tools are used in many of the largest, most demanding software development environments in the world. Try it on your code today by requesting a free product 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 [...]