Use eclipse to complete classic refactoring

Source: Internet
Author: User
I believe that many people have read the refactored classic refactoring book, which teaches us a lot of excellent refactoring methods, so that we can write our Object-oriented Programs more effectively, it is easier to reuse. That's what people call a good book that is well-known as design patterns.

In the book, the author is still feeling that there were no excellent reconstruction tools at the time, most of the reconstruction work can only be done by finding/replacing, thanks to the unit testing tools to ensure quality, otherwise, the difficulty is unimaginable.

However, we are very lucky to have the perfect refactoring function in eclipse and the latest JBuilder, next we will take a look at how to use eclipse to complete the typical program refactoring work.

First, we created a package named "refactoring" based on the example in refactoring and created classes named "movie", "Marshal", and "customer" respectively, enter the initial code.

From the figure above, we can see that we have established the initial code. Next let's use eclipse together to refactor this program.

(1)
Break down and reorganize Statement ()

The first function to be reconstructed is the statement () function. The code in it is too long to be easily understood. First, we need to extract a part of the function and then perform related processing.

Select the entire switch statement, right-click, and select "refactor-> extract method" from the pop-up menu, as shown in.

Extract will pop up.
Method window. The name of the new method to be created is amountfor. Therefore, in the "method name" text box, type a name, in the window, we can see the description of the reconstructed method.

If you click the "preview" button, we can preview the code after reconstruction. Eclipse shows us how to extract a method. Click "OK", and eclipse will automatically complete the extraction.

In this case, we do not need the thisamount parameter in the method, so we need to modify the declaration of the method. Right-click and choose "refactor-> change" from the menu.
Method signature ".

The pop-up dialog box shows the current definition of amountfor. The following lists the two parameters "thisamount" and "each". Select the first parameter and click "Remove, you can delete it.

However, if you delete this parameter directly, an error occurs. The dialog box is displayed automatically in eclipse, prompting you.

We are sure we want to delete this parameter, so click "continue" to continue modification.

Eclipse will automatically detect errors and mark them with red lines below problematic statements,

This error is prompted because the variable has not been defined in the method. We can also use the functions provided by eclipse to quickly modify the error.

Position the cursor on thisamount, and then press ctrl-1, eclipse will automatically pop up the modification recommendations, where we choose create local variable thisamount ", in this way, eclipse will automatically define this variable for us.

But at this time we will still see thisamount below the red wave line, because our variable has not been initialized after the definition, again under thisamount press "Ctrl-1 ", select "initialization variable" in the pop-up menu, and eclipse will automatically initialize this variable for us.

We can see that we didn't type a letter, but simply pressed a few keys, and then made the relevant selection in the menu, we have done a lot of processing, this not only improves the coding efficiency, but also avoids some low-level errors.

Then, we find that thisamount has some type problems. We want to return a double type of data, and the system automatically defines an integer type, this will automatically perform the round operation during processing. So we need to change the thisamount type.

Next, we found that the name of the variable we are currently using is not very good, because there is no clear meaning. In the refactoring book, there is a saying that "programmers who can compile programs that can be understood are really excellent programmers". Therefore, we need to refactored the operation-change the variable name.

Select the thisamount variable, right-click it, and select "refactor-> rename" from the context menu to rename the variable.

The pop-up dialog box for renaming is very simple. The current name is displayed on it. Next we need to change this name.

In the text box, type our new name, "result", and click "OK". The system will automatically search for and replace thisamount variables throughout the program.

Using the same method, we rename the each variable to arental, which makes the meaning clearer. As shown in the changed code, we have completed the first reconstruction step.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.