JBuilder2005 to achieve the internal refinement such as refactoring

Source: Internet
Author: User

1. Refining method

For a large method, you can use Extract method to refine part of its code into a small method and add a call to the extraction method in place. If there is a duplicate code snippet in the method, the code snippet has to be refined into a separate method for reuse.

To refine a piece of code into a single method, you only need to select this piece of code and use the Ctrl+shift+r->extract method to call up the refactoring dialog for the refinement methods. JBuilder analyzes the context variables referenced in the selected block of code, the target method defines the corresponding parameters, and passes these variables through the method entry parameters. As the following drive () method is defined in Horse.java, its code is as follows:

Code Listing 3 The drive () method before the method is refined

1. public void printQuotation(int amount)
2. {
3. printBanner();
4. System.out.println("单价:"+price);
5. System.out.println("购买只数:"+amount);
6. System.out.println("总价:"+(price * amount));
7. }

We want to extract the 4th to 6th line of code blocks into a method called Printdetail (): Select Line 4th to 6th's code->ctrl+shift+r->extract method to call the extraction dialog, as shown in the following illustration:

Figure 12 Refining Method dialog box

After you type Printdetail in method name, after you press OK, JBuilder extracts the selected code block into the new methods and inserts a call to the extraction method at the original method:

Code Listing 4 the code after the method is refined

1. public void printQuotation(int amount)
2. {
3. printBanner();
4. printDetail(amount);//在源处插入对抽取出方法的引用
5. }
6. //抽取出来的方法
7. private void printDetail(int amount) {
8. System.out.println("单价:"+price);
9. System.out.println("购买只数:"+amount);
10. System.out.println("总价:"+(price * amount));
11. }

The Printdetail () method has one entry to pass the context variable referenced by the code block, and the variable is passed through the amount of printdetail () at the call at line 4th.

2. Inline method

The inline method is the inverse of the refinement method, which replaces the method invocation with the method body, and when the method is private and there is no other place to invoke the method, JBuilder deletes the inline method after the inline method. If we place the cursor in line 4th of Listing 4 Printdetail (): Ctrl+shift+r->inline Method,jbuilder The Printdetail () method directly to line 4th. Because there is no other place in Horse.java to invoke Printdetail (), the function is inline and will be removed from the Horse.java. Inline code is shown in Listing 3.

If all functions in the system appear to be simply delegates to another function, forming too many indirect layers, and too many delegates make the program's relationship difficult to understand, the inline method can be used to remove these useless delegate methods.

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.