Refactorings in CLion: Quick and Safe

Code refactorings are used to restructure your existing code without changing its external behavior. The most popular refactoring is probably Rename, which helps you change the name of a variable, method, class, and so on. Code refactoring is an essential technique to maintain the quality of your code and make it more readable and easier to maintain and reuse.

CLion offers you a set of code refactorings, which track down and correct the affected code references automatically. This means you can always be sure that your project will work correctly, even after large-scale changes. The list of refactorings available in CLion includes:

  • Rename (Shift+F6) renames symbols, automatically correcting all references in the code for you.
  • Change Signature (Ctrl+F6 on Windows/Linux, Cmd+F6 on OS X) helps you add/remove/reorder function parameters, change the result type or update the name of the function, all usages will be fixed as well.
  • Move (F6) moves files or directories, as well as methods, variables or constants.
  • Copy (F5) allows you to create a copy of file or directory.
  • Safe Delete (Alt+Delete on Windows/Linux, Cmd+Delete Forward on OS X) safely removes files and symbols from your code.
  • Inline (Ctrl+Alt+N on Windows/Linux, Alt+Cmd+N on OS X) replaces redundant variable usage/method calls with its initializer/declaration.
  • Extract refactoring – CLion analyzes the block of code where the refactoring was invoked, detects input and output variables, together with the usages of the selected expression to replace them with the newly created:
    • Variable (Ctrl+Alt+V on Windows/Linux, Alt+Cmd+V on OS X)
    • Constant (Ctrl+Alt+C on Windows/Linux, Alt+Cmd+C on OS X)
    • Parameter (Ctrl+Alt+P on Windows/Linux, Alt+Cmd+P on OS X)
    • Typedef (Ctrl+Alt+K on Windows/Linux, Alt+Cmd+K on OS X)
    • Define (Ctrl+Alt+D on Windows/Linux, Alt+Cmd+D on OS X)
    • Method (Ctrl+Alt+M on Windows/Linux, Alt+Cmd+M on OS X)
    • Superclass
    • Subclass
  • Pull Members Up safely moves class members to a superclass.
  • Push Members Down safely moves class members to a subclass.

You can use Refactor This (Ctrl+Alt+Shift+T on Windows/Linux, Ctrl+T on OS X) to get the list of the refactorings available in the current scope.

Watch CLion’s refactorings in action:

Happy developing!
The CLion Team

This entry was posted in Tips'n'Tricks and tagged . Bookmark the permalink.

6 Responses to Refactorings in CLion: Quick and Safe

  1. Alexandru Pana says:

    When can we expect a new release :D?

    • Anastasia Kazakova says:

      We hope to publish a new EAP build asap. The 1.0 release is planned for Q1 2015. Still the dates are not final.

  2. Jacob Rohde says:

    Why do we need to use Java6 for CLion? I’d rather not install such an old JRE.

  3. Xiongyi says:

    Hi when CLion sourcing the CMakeList, can I stop it from souce every library headers, which are several big libraries and make the IDE really slow. Slow is unbearable, and I have to turn to sublime now. Especially I don’t need auto completion in those libraries.
    I only need intelligence work within my project folders. And turn off the resolvable error as well.

    • Anastasia Kazakova says:

      It’s now not possible to turn off the resolve, if the headers are included from your code base. To parse your code correctly and to provide the correct intellisense in your code base, IDE needs this information. But it’s actually done once, so maybe you can give the IDE time/memory to resolve the current library headers and then it become more responsive.

Leave a Reply to Xiongyi Cancel reply

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