Tips & Tricks

Code generation with CLion

When you have a brilliant idea in mind, you don’t want to be slowed down by something routine and mundane. Wouldn’t it be great if your IDE got those boring and repetitive tasks out of the way so you could develop at the speed of thought?

Well, that is exactly what CLion does for C and C++ developers, with its vast code generation options. Let’s see what they are.

Create from usage

This concept is so simple that you may not believe it’s true when you see it for the first time. Suppose you’ve used a function that is not implemented or not even declared yet. Your code looks red, sure, but what if you press Alt+Enter?
no_method
CLion suggests creating a new function! Press Enter and voila:
method_created

Going forward, let’s assume you’ve started using a variable name, and, for some reason, it doesn’t exist either. Press Alt+Enter to see what happens:
no_var
Plenty of options here! Select between introducing a new local/global variable, a field, a macro or a function parameter. Let’s go with the first option in this example. Now you have a variable declaration and can proceed with coding:
var_created
The same naturally works for class names.

Intentions

The Alt+Enter shortcut helps you create declarations and definitions for symbols that are already in use. Actually, it can do even more! Intentions let you apply automated changes to code that is correct, to improve it or to make your coding routine easier. To invoke an intention action, use Alt+Enter. The list of all available intention actions (for C and C++, as well as for JavaScript, XML and CSS) can be found in Settings/Preferences in Editor | Intentions.
For example, one intention adds parameters and field initializers to the constructor. While introducing a new member, you can add it to the constructor automatically. A signature preview will be shown so you can reorder parameters or add some extras:
add_param_constructor

There are more intention actions which generate any missing switch case label, or extract the boolean condition from expression and put it to if statement, or apply De Morgan’s laws to a logical expression, or… okay, I’m getting carried away here. The point is, there really are a lot of them, and each can be invoked easily with the simple Alt+Enter shortcut.

Generate getters/setters, constructors/destructors, operators

An intention action can help you add a parameter to a constructor, but what if no constructor is available yet? Or maybe you want your private/protected member to have a getter or a setter. Then what you need is the Generate menu. Press Alt+Insert (or Cmd-N on OS X) and get them all in no time:
generate
Select the fields to generate getters/setters in this dialog. For class constructors, CLion suggests class fields that should be initialized:
generate_constructor

Learn how to generate comparison, relational and stream output operators in CLion (starting from version 2016.2).

Implement/Override/Generate Definitions

Did you notice “Override Functions…” and “Implement Functions…” in the Generate menu above? Choose this menu item, or use Ctrl+I to implement and Ctrl+O to override, and CLion will generate stubs for all the selected functions. In C++11, when overriding functions you can use an override specifier:
override_specifier

Learn also about Generate Definitions possibility and its adaptive behavior introduced in CLion 2016.2.

Templates

If you’ve ever wanted to create many lines of code from just a few symbols, you’ll love CLion’s Live Template mechanism. Type for and press Tab to get a loop; or type iter or itit and then press Tab to iterate over a range:
live_template

Even better, create your own Live Templates in Settings/Preferences Editor | Live Templates! The templates can then immediately be used, shared with your team, or placed in a VCS. To apply a Live Template in code, simply type its abbreviation and press Tab. If the template includes variables, press Enter or Tab to navigate between them.

To quickly wrap code with a construct, like while, if or #ifdef, use ‘Surround With’ templates by pressing Ctrl+Alt+T (on Linux/Windows) or Cmd+Alt+T (on OS X). An expression or a set of statements will be placed inside the selected construct.

As with Live Templates, you can create your own ‘Surround With’ templates. To do that, add a new Live Template that includes a $SELECTION$ variable, and it will be added to the ‘Surround With’ list:
surround_with

Thanks to these powerful code generation features in CLion, you can craft code with much less effort.

If you then want to restructure and clean up your code without changing its external behavior, consider using CLion’s code refactorings.

That’s it for today! Browse the Tips’n’Tricks section of this blog to discover other interesting and useful features of CLion.

image description