Refactoring: Inline Temp

As I showed in previous post it is useful to use Inline Method when we have a lot of functionality which is on several methods and need to put this functionality to the one method.
In this post I want to show, in my point of view, subtechnique of Inline Method which is called Inline Temp.
Why do we need another technique? As I said in my point of view it is not another technique it is subtechnique of Inline Method.
Here I will use simple example, as usual

/**
 * This example shows how to use "Inline Temp" technique
 * @author The Developer's Info
 *
 */
public class InlineTempExample {

	public static void main(String[] args) {
		String s = getStringSum();
		System.out.println(s);
	}

	/**
	 * Method returns sum of 10+10 in String representation
	 * @return String representation of sum
	 */
	private static String getStringSum() {
		int value = 10 + 10;
		return Integer.toString(value);
	}
}

For this refactoring I need to remove String s from main method

/**
 * This example shows how to use "Inline Temp" technique
 * @author The Developer's Info
 *
 */
public class InlineTempExample {

	public static void main(String[] args) {
		System.out.println(getStringSum());
	}

	/**
	 * Method returns sum of 10+10 in String representation
	 * @return String representation of sum
	 */
	private static String getStringSum() {
		int value = 10 + 10;
		return Integer.toString(value);
	}
}

As you can see it is very simple.
Pay attention: in real-world application you can skip this technique for local variables, but if these local variables don’t allow you to do other type of refactoring then you can use Inline Temp technique.

 

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

Share

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.