Extract function
Refactor | Extract/Introduce | Method
CtrlAlt0M
The following demo illustrates the usage of the Extract function refactoring, as well as Extract parameter, Extract lambda parameter, and Live templates:
When the Extract Function refactoring is invoked, CLion analyzes the selected block of code and detects variables that are the input for the selected code fragment and the variables that are output for it.
The detected output variable is used as a return value for the extracted function.
In the editor, select a block of code to be transformed into a function.
tip
The code fragment to form the method does not necessarily have to be a set of statements. It may also be an expression used somewhere in the code.
From the main menu or from the context menu of the selection, select Refactor | Extract/Introduce | Method or press CtrlAlt0M.
In the Extract Function dialog that opens, specify the name of the new function, the return type, and other settings.
If the function has not been yet declared, select the declaration place: above or below current place.
In the parameters pane:
Add the new parameters or delete the unnecessary ones
Rename parameters or/and change their types by clicking the corresponding parameter lines and entering the new names and types
Reorder parameters in the list
Change the return type of the function
You can make a function
constexpr
ornoexcept
using the corresponding checkboxes.Check the result in the Signature Preview pane and click Extract to create the function.
The selected code fragment will be replaced with a function call.
Before | After |
---|---|
|
|
Thanks for your feedback!