Java Code Details (Inline method)

Source: Internet
Author: User


This view comes from refactoring-----Improving the design of existing code

A method ' s just as clear as its name. 02

Put the "method" into the body of its callers and remove. 03

int getrating () {return
        (Morethanfivelatedeliveries ())? 2:1;
}
 
Boolean morethanfivelatedeliveries () {return
        _numberoflatedeliveries > 5;
}
04
int getrating () {return
        (_numberoflatedeliveries > 5)? 2:1;
}
Motivation05

A theme of this book are to-use short methods named to show their intention, because these methods leads to clearer and easi Er to read code. But Sometimes you to come across a method in which the "body is as clear as" name. Or you refactor the "code into something" is just as clear as the name. When this happens, your should then get rid of the method. Indirection can be helpful and but needless indirection is irritating. 06

Another time to use Inline the ' when ' you have a group of methods that seem badly factored. Can inline them all into one and then reextract the methods. Kent Beck finds it is often good to does this before using Replace method and method Object. You inline the "various calls made by the" method "have behavior you want to have in the" Method object. It's easier to move one of the than to move the and It called methods. 07

I commonly use Inline the When someone are using too much indirection and it seems this every method does simple Delegat Ion to another method, and I get lost the delegation. In these cases some of the indirection are worthwhile, but not all of it. By trying to inline I can flush out the useful ones and eliminate the rest. Mechanics 08

Check that this method is not polymorphic. Don ' t inline if subclasses override the method; They cannot override a method of that isn ' t there.

Find all calls to the method.

Replace each call with the ' method ' body.

Compile and test.

Remove the method definition. 09

Written this way, the Inline method was simple. In general it isn ' t. I could write pages on "How to handle recursion", multiple return points, inlining into another object while you don ' t have a Ccessors, and the like. The reason I don ' t is that if you encounter these complexities and you shouldn ' t does this refactoring.


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.