NetBeans-SCSNI

5.1-Describe the purpose and uses of refactoring and demonstrate the ability to perform basic refactoring on Java source code.

About 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 orginal 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

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


Refactoring: Quick Reference

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.

This topic provides short descriptions of the refactoring operations that are available in the IDE. Click the hyperlinked name of the refactoring operation for a full description on how to use the operation.

 

Refactoring Operation

Description

Rename

Enables you to change the name of a class, variable, or method to something more meaningful. In addition, it updates all source code in your project to reference the element by its new name.

Introduce Variable, Constant, Field, or Method

Enables you to generate a statement based on the selected code and replace that block of code with a call to the statement.

Change Method Parameters

Enables you to add parameters to a method and change the access modifier.

Encapsulate Fields

Generates a getter method and and a setter method for a field and optionally updates all referencing code to access the field using the getter and setter methods.

Pull Up

Moves methods and fields to a class that their current class inherits from.

Push Down

Moves inner classes, methods, and fields to all subclasses of their current class.

Move Class

Moves a class to another package or into another class. In addition, all source code in your project is updated to reference the class in its new location.

Copy Class

Copies a class to the same or a different package.

Move Inner to Outer Level

Moves an inner class one level up in hierarchy.

Convert Anonymous Class to Inner

Converts an anonymous class to an inner class that contains a name and constructor. The anonymous inner class is replaced with a call to the new inner class.

Extract Interface

Creates a new interface from the selected public non-static methods in a class or interface.

Extract Superclass

Creates a new abstract class, changes the current class to extend the new class, and moves the selected methods and fields to the new class.

Use Supertype Where Possible

Changes code that references the selected class (or other type) to instead use a supertype of that type.

Safely Delete

Checks for references to a code element and then automatically deletes that element if no other code references it.


How to Refactor code

Place the caret on the symbol you want to refactor. Pick the refactoring either from the main menu or from the editor popup menu.

RF_Menu.png

Fill in the necessary data in the refactoring dialog and choose either to refactor directly or show a preview of the refactoring.

RF_Dialog.png

In the refactoring preview you can look at the diffs to make sure that it will make the desired change to your code.

RF_Preview.png



-- Wagner R. Santos