Deleting Code Safely


Over time, your code might gather elements that have limited or no usefulness. To make the code easier to maintain, it is desirable to remove as much of this code as possible. However, it might be hard to immediately determine whether you can delete such code without causing errors elsewhere.

Refactoring Commands

NetBeans IDE has special support for refactoring code. The term refactoring refers to renaming and rearranging code without changing what the code does. Reasons for refactoring include things such as the need to separate API from implementation, making code easier to read, and making code easier to reuse.

The IDE's refactoring support makes refactoring easier by enabling you to update all of the code in your project automatically to reflect changes that you make in other parts of your project.

For example, if you rename a class, references to that class in other classes are also updated.

You can access most refactoring commands from the Refactor menu on the main menu bar or by right-clicking in the Source Editor or on a class node in the Projects window and choosing from the Refactor submenu. The Find Usages command is in the Edit menu and the contextual (right-click) menu for the Source Editor and the Projects window.

Typically, the currently selected identifier is filled in as the code element to be refactored.

Table 5-10 provides a summary of the refactoring commands that are available. These commands are explained more thoroughly in task-specific topics throughout this chapter.


Table 5-10. Refactoring Commands

Command

Description

Find Usages

Displays all occurrences of the name of a given class, method, or field. See Finding Occurrences of the Currently Selected Class, Method, or Field Name earlier in this chapter.

Rename

Renames all occurrences of the selected class, interface, method, or field name. See Renaming All Occurrences of the Currently Selected Class, Method, or Field Name earlier in this chapter.

Safely Delete

Deletes a code element after making sure that no other code references that element. See Deleting Code Safely earlier in this chapter.

Change Method Parameters

Enables you to change the parameters and the access modifier for the given method. See Changing a Method's Signature later in this chapter.

Encapsulate Fields

Generates accessor methods (getters and setters) for a field and changes code that accesses the field directly so that it uses those new accessor methods instead. See Encapsulating a Field later in this chapter.

Move Class

Moves a class to a different package and updates all references to that class with the new package name. See Moving a Class to a Different Package later in this chapter.

Pull Up

Moves a method, inner class, or field to a class' superclass. You can also use this command to declare the method in the superclass and keep the method definition in the current class. See Moving Class Members to Other Classes later in this chapter.

Push Down

Moves a method, inner class, or field to a class' direct subclasses. You can also use this command to keep the method declaration in the current class and move the method definition to the subclasses. See Moving Class Members to Other Classes later in this chapter.

Extract Method

Creates a new method based on a selection of code in the selected class and replaces the extracted statements with a call to the new method. See Creating a Method from Existing Statements later in this chapter.

Extract Interface

Creates a new interface based on a selection of methods in the selected class and adds the new interface to the class' implements clause. See Creating an Interface from Existing Methods later in this chapter.

Extract Superclass

Creates a new superclass based on a selection of methods in the selected class. You can have the class created with just method declarations, or you can have whole method definitions moved into the new class. See Extracting a Superclass to Consolidate Common Methods later in this chapter.

Use Supertype Where Possible

Change code to reference objects of a superclass (or other type) instead of objects of a subclass. See Changing References to Use a Supertype later in this chapter.

Move Inner to Outer Level

Moves a class up one level. If the class is a top-level inner class, it is made into an outer class and moved into its own source file. If the class is nested within the scope of an inner class, method, or variable, it is moved up to the same level as that scope. See Unnesting Classes later in this chapter.

Convert Anonymous Class to Inner

Converts an anonymous inner class to a named inner class. See Unnesting Classes later in this chapter.


The IDE's Safely Delete command can help you with the process of removing unused code, saving you cycles of manual searches and compilation attempts. When you use this command, the IDE checks to see if the selected code element is referenced elsewhere. If the code element is not used, the IDE deletes it. If the code element is used, the IDE displays where the code is used. You can then resolve references to the code you want to delete and then try the Safely Delete operation again.

You can use the Safely Delete command on any type (such as a class, interface, and or enumeration), method, field, or local variable.

To safely delete a code element:

  1. In the Source Editor or Projects window, right-click the code element that you want to delete and choose Refactor | Safely Delete.

  2. In the Safe Delete dialog box, make sure that the item to be deleted is listed.

  3. If you want the IDE to look inside comments for mentions of the code element, select the Search in Comments checkbox.

    If this checkbox is not selected, comments referring to the code element are not affected if you delete the code element.

    If this checkbox is selected, any found references to the code element in comments will prevent the code element from being immediately deleted, even if the code element is not used.

  4. Click Next.

    If no references to the code element are found, the Safe Delete dialog box closes and the code element is deleted.

    If references to the code element are found, no code is deleted and the Safely Delete dialog box remains open. If you click the Show Usages button, the Usages window opens and displays the references to that code element. Double-click an item in the list to jump to the line of code that it represents. If you remove the cited references, you can click the Rerun Safe Delete button to repeat the attempt to safely delete the code element.

To undo the Safely Delete command, choose Refactor | Undo.




NetBeans IDE Field Guide(c) Developing Desktop, Web, Enterprise, and Mobile Applications
NetBeans IDE Field Guide(c) Developing Desktop, Web, Enterprise, and Mobile Applications
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 279

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net