Refactoring:Inline Method

In this post I want to show other practice which works in other way then Extract Method technique.
It is called Inline Method.
As usual I will show example for understanding.
This is simple example which has several simple methods:

public class InlineMethodExample{public static void main(String[] args){String sum = getTextSum();System.out.println(sum)}private static String getTextSum(){int value = getSum();String sumText = getString(value);return sumText}private static int getSum(){int sum = 10 + 10;return sum}private static String getString(int value){return Integer.toString(value)}}

As you can see it is very simple example. Okay.
Now let’s look to these methods. As you can see all these methods we can move to one instead of using several small or as you want short methods.
At this point we can use Inline Method technique

public class InlineMethodExample{public static void main(String[] args){System.out.println(getStringSum())}private static String getStringSum(){int value = 10 + 10;return Integer.toString(value)}}

What I did?! I put(inline) in getStringSum bodies of getSum() and getString() methods.
Sometimes need to inline some functionality in one method or block of code instead of using external methods.
It is very great that we can use Extract Method technique for extracting functionality to external methods,but sometimes you can find or write code with a lot of methods which need to be on the one method and here you will find useful Inline Method technique.

 

If you’d like to get the latest posts as soon as they’re published,subscribe to website’s feed!

Share

No related posts.

2 comments to Refactoring:Inline Method

Leave a Reply

  

  

  

You can use these HTML tags

<a href=""title=""><abbr title=""><acronym title=""><b><blockquote cite=""><cite><code><del datetime=""><em><i><q cite=""><strike><strong>

A sample text widget

Etiam pulvinar consectetur dolor sed malesuada. Ut convallis euismod dolor nec pretium. Nunc ut tristique massa.

Nam sodales mi vitae dolor ullamcorper et vulputate enim accumsan. Morbi orci magna,tincidunt vitae molestie nec,molestie at mi. Nulla nulla lorem,suscipit in posuere in,interdum non magna.