Introducing Functions and Variables by Refactoring

When you introduce a function or variable in the IDE, you change a selected code fragment into a function or variable. Typically you do this when you want to separate a piece of code into smaller, more meaningful fragments. Creating smaller fragments can increase the reusability of your code as you can separate the parts of your code that might need to be updated more often.

For example, when you introduce a function in the IDE, you replace statements in your C/C++ program with a call to a function. Before statements are replaced, the IDE opens the Introduce Function dialog box where you specify the parameters and modifiers for the function.

To introduce a function:

  1. In the Source Editor, select the statements you want to introduce as a new function.

  2. Choose Refactor > Introduce Function (Alt+Shift+M) to open the Introduce Function dialog box.

  3. Type the name for your new function in the Name field.

  4. Specify a Return Type appropriate for the function, such as int or void.

  5. If necessary, specify if the parameters are passed by reference.

  6. Check the Function Signature Preview to make sure it is what you expect.

  7. Click Preview to view the changes that will be made when you refactor.

  8. If the code changes are okay, click Do Refactoring in the Refactoring window.


To introduce a variable:

  1. In the Source Editor, select a value that you want to replace with a reference to a new variable that you will introduce.

  2. Choose Refactor > Introduce Variable (Alt+Shift+V) to open the Introduce Variable dialog box.

  3. Type the name for your new variable in the Name field.

  4. Specify a Type appropriate for the variable. The IDE should by default suggest an appropriate type for the variable.

  5. If needed, select Replace All Occurrences to replace each occurrence of the value in the current function with the new variable.

  6. Click OK to introduce the variable.


Copyright © 2015, Oracle and/or its affiliates. All rights reserved.