Tips & Tricks

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

image description