Compilers, IDE, Programmimg Languages, Softwares, Tips, windows

10 NetBeans Shortcut Keys for Code Refactoring

Today I’m going to share with you some shortcut keys for refactoring Java code in NetBeans IDE. Use these shortcuts to boost your coding productivity.

1. Alt + Enter:

When the cursor is at the line marked as erroneous or problematic, press this shortcut will show up the suggestions (hints) to fix the error/problem:Show SuggestionThis shortcut is very useful as it allows you to quickly access the options without moving the mouse to the line number column on the left and then click the hint icon. 2. Ctrl + R:Renames (with refactor) a variable, method, class or a file. If applied for a variable, the variable is marked in a red rectangle. You type the new name and hit Enter, all references to that variable are updated:rename variablePress Esc to cancel renaming.

If the object being renamed is a method or class, then the following dialog gets displayed:Rename classEnter a new name, and then click Refactor.You can also preview the changes by clicking the Preview button, and then a refactoring view gets displayed like this:Refactoring viewHere you can compare the two versions: the current one versus the refactored one. Then click Do Refactoring to apply the changes. 3. Alt + Delete:Use this shortcut to safely delete a variable, method or class. That means NetBeans will check for any references before deleting.For example, press Alt + Delete would bring the following dialog:Safely deleteClick Refactor. If references were found, that means you cannot safely delete the item, as shown in the following warning dialog:Cannot safely deleteIf there is no warning, NetBeans will delete the item because it’s safe. 4. Ctrl + M: Moves members from one class to another, or moves a class from one package to another.Here’s the Move Members dialog:Move members dialogAnd here’s the dialog if the object being moved is a class:Move class dialog 5. Alt + Shift + M: Extracts a selection of a code block to a new method. The Introduce Method appears as shown in the following screenshot:Introduce Method dialog 6. Alt + Shift + V: Extracts an existing expression or statement into a new variable. For example, the statementtextEmail.getText() appears multiple times so it should be introduced to a variable:Select statementThen the Introduce Variable dialog appears like this:Introduce Variable dialogEnter the variable name and click OK. Note that the scope of the newly introduced variable is local. 7. Alt + Shift + C: Introduces a new constant. This shortcut is useful in case you want to convert a String literal to a constant. For example:Select StringThen the Introduce Constant dialog appears:Introduce ConstantYou can update the name, and then click OK8. Alt + Shift + P: introduces a new parameter. This shortcut is useful in case you want to introduce a String literal, numeric literal, boolean literal or an expression to a method parameter. For example, introduce the integer number 500 in the following code to a method parameter:select numeric literalThe Introduce Parameter dialog appears:Introduce ParameterUpdate the name and click Refactor. Here’s the result: 9. Alt + Shift + E: Introduces a new member/field. Use this shortcut if you want to change scope of a local variable to a class member, or to convert an expression to a field. The Introduce Field dialog looks like the following:Introduce field 10. Alt + Shift + X: Introduces local extension. Perhaps this is one of the most complex refactoring features in NetBeans. In short, this creates a subclass or a wrapper class of a selected type in order to have additional methods for that type. Read the following post for more details: Generating (Introduce) Local Extensions.Here’s a screenshot of the Introduce Local Extension dialog:Introduce Local Extension

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s