Site icon Embarcadero RAD Studio, Delphi, & C++Builder Blogs

New in C++Builder 10.2.3: C++ Rename Refactoring

Yesterday, we released RAD Studio 10.2.3!  Over the next week, we’ll be blogging about some of the new features.  Today I’d like to look at the introduction of rename refactoring for C++.

What is refactoring?

Over time, code grows and changes.  You need to maintain it: in fact, one sign of a good programmer is keeping the codebase clean: well organised, well separated with clean interfaces between segments.  Refactoring is the process of rearranging code to move or restructure it to keep it well organised.

You can do all this manually.  But much of this can be automated.

Rename Refactoring

One useful automatable refactoring is renaming elements in your code: methods, variables, types and so forth.  Perhaps you come across a misnamed variable while looking for something else: a clean coder will rename it, there and then, always keeping code accurate.  Or, perhaps your code has grown, and your Foo class needs to be split up: you might start by renaming it FooAbstract before creating a couple of subclasses and moving methods into them. For all of these, you can manually search and replace, but you may get false positives – not all Foo-s refer to the same class.

Better is to have IDE tooling that is aware of the language and rename all references. It does the work for you, and you know it is correct because the items that are identified to be renamed are done so through language- and project-aware analysis.

This is exactly what we’ve implemented in RAD Studio 10.2.3.

Rename Refactoring in 10.2.3

In 10.2.3, you can right-click on an identifier (method, type, variable, etc) in the editor and in the popup menu that appears, select Refactor > Rename.

Then, in the dialog that appears, enter a new name:

Check the ‘Preview changed before applying’ checkbox to have a look at the changed it will make before applying them. Or, if you wish, uncheck this and it will rename immediately.  If you leave it checked, you’ll see the Apply Patch pane appear:

Scroll through the list and check what it plans to change.  You can click the cube refactoring icon on the toolbar to apply the rename.

Either way, if you preview the changes first or uncheck the checkbox and apply immediately when clicking Ok, the rename will be applied!

Technical info

In 10.2.3, we have the core of a new C++ language analysis and database in the IDE. This is based around Clang, and so your project should be using the Clang compilers. This builds an AST cache for your project(s). In this release, it’s used only for rename refactoring, and is updated only when you invoke rename refactoring. That means the first time you rename something that is shard by multiple files (ie not a local variable, but a common class, say) it will build the cache, but thereafter it will only need to build the changed files since last time.

You can rename any common identifier, like class or other type names, fields, methods, and variables. There are a few caveats, such as not being able to rename inside a macro that is disabled (an #ifdef that is not defined) – this is because it uses the compiler itself. This guarantees correctness, and means that renaming only sees what the compiler sees.

Renaming works at the project level.

We’re focusing on improving the IDE’s tooling in several ways. Rename refactoring is just the first step!

 

 

Exit mobile version