7.7 C Code Refactoring
Refactoring is the use of small transformations to restructure code without changing any program behavior. Just as you factor an expression to make it easier to understand or modify, you refactor code to make it easier to read, simpler to understand, and faster to update. And just as a refactored expression must produce the same result, the refactored program must be functionally equivalent with the original source.
Some common motivations for refactoring code include:
- Making the code easier to change or easier to add a new feature
- Reducing complexity for better understanding
- Removing unnecessary repetition
- Enabling use of the code for other needs or more general needs
- Improving the performance of your code
MPLAB X IDE’s refactoring features simplify code restructuring by evaluating the changes that you want to make, showing you the parts of your application that are affected, and making all necessary changes to your code. For example, if you use the Rename operation to change a class name, the IDE finds every usage of that name in your code and offers to change each occurrence of that name for you.
Refactor Menu
When you use the IDE’s refactoring operations, you can change the structure of your code and have the rest of your code updated to reflect the changes you have made.
Refactoring operations are available on the Refactor menu (see 13.1.6 Refactor Menu).
Undoing/Redoing Refactoring Changes
Undo - The IDE rolls back all the changes in all the files that were affected by the refactoring. | |
Redo - The IDE repeats the refactoring changes in all the files that were affected. |
If any of the affected files have been modified since the refactoring took place, the refactoring Undo/Redo is not available.
Finding Function Usages
You can use the Find Usages command to determine each place a function is used in your project’s source code.
To find where a function is used in your project:
- In the Navigator window or the Source Editor window, right click the function name and choose Find Usages - Alt + F7 (or Ctrl + F7 for macOS).
- The Find Usages command displays the code
lines that call the function. In the Find Usages dialog box,
click Find.
The Usages window displays the file name and the line of code for each usage found in that file.
To jump to a specific occurrence of the function, do one of the following actions:
- In the Usages window, use the up and down arrow buttons in the left pane to navigate from one occurrence of the function to the next.
- Double click a line of code to open the file and position the cursor on that particular line of code.
Additional IDE Find Mechanisms
The other IDE tools that enable you to search for all the places where specific text is used in a project include:
- Finding and Replacing Text. Searches for all the places where specific text is used in a source file that is open in the Editor. Choose to open the Find dialog box, or choose to open the Replace dialog box. These commands finds all matching strings, regardless of whether the string is a C code element.
- Find in Projects. As with the Find command, the Find in Projects command searches for matching strings, regardless of whether the string is a function name. Choose EditFind in Projects to open the Find in Projects dialog box and then type the string of text that you are looking for.
To find where a function is declared in a source file, you can double click the function in the Navigator window. If the function is declared in a different source file, right click the function and choose from the contextual menu.
Renaming a Function or Parameter
To rename a function or parameter and update references to it throughout your project:
- In the Source Editor, right click the function or parameter and choose from the context menu.
- In the Rename dialog box, type the New Name.
- Optionally, click Preview. In the Refactoring window, at the bottom of the Source Editor, review the lines of code that will be changed and clear the check box of any code that you do not want changed.
- Click Do Refactoring to apply the selected changes.
For quick in-place renaming, place the cursor in the item that you want to rename and press Ctrl + R. Then type the new name. To finish renaming, press Escape.
Moving, Copying and Safely Deleting C Code
These functions are specific to C++ code. See 13.1.6 Refactor Menu.