Refactor Code

What is Refactoring?

Refactoring may be a new term to many MPLAB® users since many PIC® microcontroller programs are very short. However, as applications grow more complex, IDE support for refactoring becomes very useful.

Code Refactoring is the process of changing a computer program's source code without modifying its external functional behavior in order to improve some of the nonfunctional attributes of the software. Advantages include improved code readability and reduced complexity to improve the maintainability of the source code, as well as a more expressive internal architecture or object model to improve extensibility. (Source: Wikipedia)

In a sense, refactoring is a fancy form of "search and replace" where the IDE is language-aware and can make more intelligent decisions about how and when an item should be modified. While "search and replace" might be adequate for renaming a variable when you only have one or a very small number of source files, it becomes more of a challenge if you have a large program and want to move an item to a new file or rename something throughout your entire project. Refactoring is immensely helpful in situations like that.

Rename Operations

The simplest form of refactoring is to rename a variable or function. This may be accomplished by highlighting the variable or function in your code and then selecting from the main menu Refactor > Rename…. This will open a dialog box prompting you for a new name. At this point, you may accept the new name by clicking on the Refactor button and the IDE will rename every instance of the variable or function in all files of the project. Alternatively, you may choose to preview the refactoring by clicking on the Preview button and control individual items as to whether or not they should be renamed.

RefactorRenameDialog.png
Click image to enlarge.

The Preview window will open as a new tab in the output area. Here you can see each file in which the variable or function exists and will be renamed, with a before and after view of the code on the right side. A checkmark indicates that a specific instance will be renamed. Unchecking it will keep the original name for that specific instance. Click on the Do Refactoring button to accept the rename operation and any modifications to individual instances you have made.

RefactorRename.png
Click image to enlarge.

Move Operations

Move refactoring is only applicable to object-oriented languages and is used to move a class from one package to another. This is not usable in C or assembly at the present time.

Copy Operations

Copy refactoring is only applicable to object-oriented languages and is used to copy the contents of a class from one package to another. This is not usable in C or assembly at the present time.

Safely Delete Operations

Safely Delete refactoring is only applicable to object-oriented languages and is used to delete a class member variable while checking to ensure that it is not used by your code. This is not usable in C or assembly at the present time.


Reference: Refactoring

© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.