CLion 2024.3 Help

Extract function

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.

Extract a function

  1. In the editor, select a block of code to be transformed into a function.

  2. From the main menu or from the context menu of the selection, select Refactor | Extract/Introduce | Method or press Ctrl+Alt+M.

  3. In the Extract Function dialog that opens, specify the name of the new function, the return type, and other settings.

    Extract method dialog

    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 or noexcept using the corresponding checkboxes.

  4. 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

int main() { int x = 15; int y = 10; int z = x - y; return 0; }
int main() { int x = 10; int y = 15; int z = XSubY(x,y); return 0; } void XSubY(int x, int y) { return x - y; }

Code examples

Before

After

private func setupUI() { // ... // This code will be extracted to a method self.buttonLogin.layer.borderColor = UIColor.black.cgColor self.buttonLogin.layer.borderWidth = 1.0 self.buttonLogin.setTitleColor(UIColor.black, for: .normal) self.buttonLogin.setTitle("Login", for: .normal) }
private func setupUI() { // ... // Extracted method's call setupLoginButton() } // Extracted method private func setupLoginButton() { self.buttonLogin.layer.borderColor = UIColor.black.cgColor self.buttonLogin.layer.borderWidth = 1.0 self.buttonLogin.setTitleColor(UIColor.black, for: .normal) self.buttonLogin.setTitle("Login", for: .normal) }

Before

After

- (void)setupUI { // ... // This code will be extracted to a method self.buttonLogin.layer.borderColor = [[UIColor blackColor] CGColor]; self.buttonLogin.layer.borderWidth = 1.0; [self.buttonLogin setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [self.buttonLogin setTitle:@"Login" forState:UIControlStateNormal]; }
- (void)setupUI { // ... // Extracted method's call [self setupLoginButton]; } // Extracted method - (void)setupLoginButton { self.buttonLogin.layer.borderColor = [[UIColor blackColor] CGColor]; self.buttonLogin.layer.borderWidth = 1.0; [self.buttonLogin setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [self.buttonLogin setTitle:@"Login" forState:UIControlStateNormal]; }
    Last modified: 08 October 2024